CNAE
Classificação Nacional de Atividades Econômicas: the economic activity subclass codes of the CNAE-Subclasses 2.3 table.
Validate
- JavaScript library
- Python library
- Go library3 cases fail
- Ruby library3 cases fail
- Rust library2 cases fail
- .NET library1 case fails
- Erlang library
Checks whether a CNAE subclass code exists in the CNAE-Subclasses 2.3 table (the current subclass revision of CNAE 2.0).
- Same input rules as
cbo.isValid, with 7 digits and theNNNN-N/NNmask. Bare digits are left-padded to 7, a masked value is read as written, and other strings are rejected. - Any run of separators (space,
.,-or/) is accepted at each boundary between the groups, or none:6201--5//01is valid. Whitespace around the value is ignored. Until 2.4.0 a single separator was accepted and6201--5//01was rejected. - A number is read only when it is a safe non-negative integer, and is left-padded to 7 digits like bare digits (
111301is0111301). A negative, fractional or unsafe number is rejected.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | boolean |
Check if a CNAE (Classificação Nacional de Atividades Econômicas) subclass code exists in the CNAE-Subclasses 2.3 table, the current subclass revision of CNAE 2.0.
- Same rules as
isValidCbo, with 7 digits and theNNNN-N/NNmask.
import { isValidCnae } from '@brazilian-utils/brazilian-utils';
isValidCnae('6201-5/01'); // true
isValidCnae('6201501'); // true
isValidCnae(111301); // true (padded to 7 digits, so this is '0111301')
isValidCnae('111301'); // true (padded the same way a number is)
isValidCnae('0000000'); // false
isValidCnae('0111abc301'); // false (not a documented form)
isValidCnae(-111301); // false (not a non-negative safe integer)Source: CNAE-Subclasses 2.3 at CONCLA/IBGE and the IBGE subclasses API.
Code: brazilian-utils/javascriptTry it with JavaScript isValidCnae
Shared test cases (23) and the result in each library cnae.isValid
Format
- JavaScript library
- Python library
- Go library2 cases fail
- Ruby library7 cases fail
- Rust library
- .NET library2 cases fail
- Erlang library
Formats a CNAE subclass code with the mask NNNN-N/NN. Only the structure changes (use cnae.isValid to check the code).
- 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 7 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. Until 2.4.0padreturned the whole zero mask (0000-0/00). - 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 |
options | FormatCnaeOptions | no |
options.pad | boolean | no |
| returns | string |
Format a CNAE (Classificação Nacional de Atividades Econômicas) subclass code. Only the structure changes; use isValidCnae to check a code against the table.
- Options (
FormatCnaeOptions):pad(defaultfalse) first left pads the value with zeros to the 7 digits of a complete code. 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
'', since its sign and decimal point are not mask characters. Returns''when there is no digit at all.
import { formatCnae } from '@brazilian-utils/brazilian-utils';
formatCnae('6201501'); // 6201-5/01
formatCnae('62'); // 62 (masked as far as it goes)
formatCnae('62015'); // 6201-5
formatCnae('62', { pad: true }); // 0000-0/62 (padded to 7 digits first)
formatCnae(111301, { pad: true }); // 0111-3/01
formatCnae('abc6201501'); // 6201-5/01 (only the digits are read)
formatCnae(-6201501); // '' (not a non-negative safe integer)Try it with JavaScript formatCnae
Shared test cases (32) and the result in each library cnae.format
Parse
- JavaScript library
- Python library
- Go library2 cases fail
- Ruby library2 cases fail
- Rust library
- .NET library1 case fails
- Erlang library
Removes CNAE formatting and keeps only digits, capped at 7 digits.
- It does not left-pad anything. A partial code stays as written.
- 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 CNAE (Classificação Nacional de Atividades Econômicas) formatting, keep only digits, and cap the result to the 7 digits of a complete subclass code.
- Same rules as
parseCbo: nothing is left padded here.
import { parseCnae } from '@brazilian-utils/brazilian-utils';
parseCnae('6201-5/01'); // '6201501'
parseCnae('62'); // '62' (a partial code is kept as written)Try it with JavaScript parseCnae
Shared test cases (10) and the result in each library cnae.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 CNAE subclass code in the CNAE-Subclasses 2.3 table and returns its code (7 bare digits) and official description.
This table is the current subclass revision of CNAE 2.0.
- Same input rules as
cbo.get, with 7 digits and theNNNN-N/NNmask, including a run of separators between the groups (6201--5//01gives the entry). - Returns
nullfor an unknown code or a value not in an accepted form. - It returns
nullexactly whencnae.isValidreturnsfalse.
| Parameter | Type | Required |
|---|---|---|
value | string | number | yes |
| returns | Cnae | null |
Look a CNAE (Classificação Nacional de Atividades Econômicas) subclass code up and get its code and official description. The result is a Cnae record: { code, description }.
- Same rules as
getCbo, with 7 digits and theNNNN-N/NNmask. codecomes back as the 7 bare digits; pass it toformatCnaefor theNNNN-N/NNform.
import { formatCnae, getCnae } from '@brazilian-utils/brazilian-utils';
getCnae('6201-5/01'); // { code: '6201501', description: 'DESENVOLVIMENTO DE PROGRAMAS DE COMPUTADOR SOB ENCOMENDA' }
getCnae(111301); // { code: '0111301', description: 'CULTIVO DE ARROZ' } (padded to 7 digits)
getCnae('111301'); // { code: '0111301', description: 'CULTIVO DE ARROZ' } (padded the same way)
getCnae('0000000'); // null
getCnae('0111abc301'); // null (not a documented form)
formatCnae(getCnae('6201501')?.code); // 6201-5/01 (the mask is the formatter's job)Source: CNAE-Subclasses 2.3 at CONCLA/IBGE and the IBGE subclasses API.
Code: brazilian-utils/javascriptTry it with JavaScript getCnae
Shared test cases (13) and the result in each library cnae.get
Official sources
See also CNPJ
Last updated on
