CFOP

Código Fiscal de Operações e Prestações: the fiscal operation codes of Anexo II of Convênio SINIEF s/nº 1970.

  • Parity matrix

Validate

Checks whether a CFOP code exists in the consolidated Anexo II of Convênio SINIEF s/nº 1970 in force.

  • Only operable codes count: the function rejects group and subgroup headings (codes ending in 00 and 50).
  • Accepts the 4 digits, the N.NNN form or a safe non-negative integer. A masked string may have any run of separators (space, ., - or /) between the groups: 5..102 is valid. Until 2.4.0 the form had a single separator and 5..102 was rejected. Any other string is rejected.
  • Whitespace around the value is ignored.
  • No CFOP starts with a zero, so the function pads nothing.
ParameterTypeRequired
valuestring | numberyes
returnsboolean

Check if a CFOP (Código Fiscal de Operações e Prestações) code exists in the official table, the consolidated Anexo II of Convênio SINIEF s/nº 1970 in force.

  • Only operable codes count: the group and subgroup headings, the codes ending in 00 and 50, are rejected.
  • Accepts a string with the 4 digits or with the N.NNN form, with any run of separators (space, ., - or /) between the groups, or a number. Any other string is rejected.
  • No CFOP code starts with a zero, so nothing is padded.
import { isValidCfop } from '@brazilian-utils/brazilian-utils';

isValidCfop('5102'); // true
isValidCfop('1.101'); // true
isValidCfop('7504'); // true (added by the 2022 rewrite of the annex)
isValidCfop('0000'); // false
isValidCfop('1150'); // false (a subgroup heading, not an operable code)
isValidCfop('abc5102'); // false (not a documented form)
isValidCfop(-5102); // false (not a non-negative safe integer)

Source: consolidated Anexo II of Convênio SINIEF s/nº 1970, last amended by Ajuste SINIEF 39/25.

Code: brazilian-utils/javascript
Try it with JavaScript isValidCfop
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (26) and the result in each library cfop.isValid

Format

Formats a CFOP code with the mask N.NNN. Only the structure changes (use cfop.isValid to check the code against the table).

  • The mask is applied as far as the digits go, so a value being typed is masked progressively.
  • options.pad (default false) first left-pads the value with zeros to 4 digits. A number is treated as the string of its digits, so it is padded only with pad.
  • A string is read for its digits: other characters are dropped and digits after the last one of the mask are ignored.
  • An empty value, or one without digits, returns an empty string even with pad. null and undefined return an empty string too.
  • A number is read only when it is a safe non-negative integer. A negative, fractional, non-finite or unsafe number returns an empty string.
  • No CFOP starts with a zero, so pad only serves a caller that wants a fixed width.
  • New in 2.5.0 (#615), so every masked classification code has its formatter.
ParameterTypeRequired
valuestring | numberyes
optionsFormatCfopOptionsno
options.padbooleanno
returnsstring

Format a CFOP (Código Fiscal de Operações e Prestações) code into the N.NNN form the annex prints. Only the structure changes; use isValidCfop to check a code against the table.

  • Options (FormatCfopOptions): pad (default false) first left pads the value with zeros to the 4 digits of a complete code (no CFOP starts with a zero, so it only serves a fixed width). Without it the mask is applied as far as the value goes. An empty value, or one without digits, gives '' even with pad.
  • Characters outside the mask are dropped, and a number is read as the string of its digits only when it is a non-negative safe integer: a negative, fractional or unsafe number returns ''. Returns '' when there is no digit at all.
import { formatCfop } from '@brazilian-utils/brazilian-utils';

formatCfop('5102'); // 5.102
formatCfop('51'); // 5.1 (masked as far as it goes)
formatCfop('102', { pad: true }); // 0.102 (padded to 4 digits first)
formatCfop('abc5102'); // 5.102 (only the digits are read)
formatCfop(-5102); // '' (not a non-negative safe integer)

Source: Convênio SINIEF s/nº 1970, Anexo II, which prints the codes as N.NNN.

Code: brazilian-utils/javascript
Try it with JavaScript formatCfop
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (19) and the result in each library cfop.format

Parse

Removes CFOP formatting and keeps only digits, capped at 4 digits.

  • The function does not pad the result.
  • Returns an empty string when there is no digit at all (null and undefined included).
  • A number is read only when it is a safe non-negative integer. A negative, fractional, non-finite or unsafe number returns an empty string (2.4.0 read the digits of any number, sign and decimal point dropped).
ParameterTypeRequired
valuestring | numberyes
returnsstring

Remove CFOP (Código Fiscal de Operações e Prestações) formatting, keep only digits, and cap the result to 4 digits.

  • No CFOP code starts with a zero, so nothing is padded here.
import { parseCfop } from '@brazilian-utils/brazilian-utils';

parseCfop('5.102'); // '5102'
Code: brazilian-utils/javascript
Try it with JavaScript parseCfop
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (9) and the result in each library cfop.parse

Look up

Looks up a CFOP code in the official table and returns its code and description.

  • Same input rules as cfop.isValid, including a run of separators between the groups (5..102 gives the entry; until 2.4.0 it gave null). Returns null for a heading, an unknown code or a value not in an accepted form.
  • It returns null exactly when cfop.isValid returns false.
ParameterTypeRequired
valuestring | numberyes
returnsCfop | null

Look a CFOP (Código Fiscal de Operações e Prestações) code up and get its code and official description. The result is a Cfop record: { code, description }.

  • Same rules as isValidCfop. Returns null for a heading, an unknown code or a value not in a documented form.
import { getCfop } from '@brazilian-utils/brazilian-utils';

getCfop('1101'); // { code: '1101', description: 'Compra para industrialização ou produção rural' }
getCfop('7504'); // { code: '7504', description: 'Exportação de mercadoria que foi objeto de formação de lote de exportação' }
getCfop('0000'); // null
getCfop('5350'); // null (a subgroup heading, not an operable code)
getCfop('abc5102'); // null (not a documented form)

Source: consolidated Anexo II of Convênio SINIEF s/nº 1970, last amended by Ajuste SINIEF 39/25.

Code: brazilian-utils/javascript
Try it with JavaScript getCfop
The inputs start with the first shared case. Change one to see the new result.

Runs @brazilian-utils/brazilian-utils 2.5.0 in your browser.

Shared test cases (22) and the result in each library cfop.get

Official sources

Last updated on

On this page