CFOP
Código Fiscal de Operações e Prestações: the fiscal operation codes of Anexo II of Convênio SINIEF s/nº 1970.
Validate
- JavaScript library
- Python library
- Go library2 cases fail
- Ruby library1 case fails
- Rust library2 cases fail
- .NET library1 case fails
- Erlang library
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
00and50). - Accepts the 4 digits, the
N.NNNform or a safe non-negative integer. A masked string may have any run of separators (space,.,-or/) between the groups:5..102is valid. Until 2.4.0 the form had a single separator and5..102was rejected. Any other string is rejected. - Whitespace around the value is ignored.
- No CFOP starts with a zero, so the function pads nothing.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | boolean |
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
00and50, are rejected. - Accepts a string with the 4 digits or with the
N.NNNform, 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/javascriptTry it with JavaScript isValidCfop
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(defaultfalse) 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 withpad.- 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.nullandundefinedreturn 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
padonly serves a caller that wants a fixed width. - New in 2.5.0 (#615), so every masked classification code has its formatter.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
options | FormatCfopOptions | no |
options.pad | boolean | no |
| returns | string |
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(defaultfalse) 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 withpad. - 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.
Try it with JavaScript formatCfop
Shared test cases (19) and the result in each library cfop.format
Parse
- JavaScript library
- Python library
- Go library2 cases fail
- Ruby library2 cases fail
- Rust library
- .NET library1 case fails
- Erlang library
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 (
nullandundefinedincluded). - 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).
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | string |
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'Try it with JavaScript parseCfop
Shared test cases (9) and the result in each library cfop.parse
Look up
- JavaScript library
- Python library
- Go library1 case fails
- Ruby library1 case fails
- Rust library
- .NET library1 case fails
- Erlang library
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..102gives the entry; until 2.4.0 it gavenull). Returnsnullfor a heading, an unknown code or a value not in an accepted form. - It returns
nullexactly whencfop.isValidreturnsfalse.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | Cfop | 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. Returnsnullfor 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/javascriptTry it with JavaScript getCfop
Shared test cases (22) and the result in each library cfop.get
Official sources
Last updated on
