CNAE

Classificação Nacional de Atividades Econômicas: the economic activity subclass codes of the CNAE-Subclasses 2.3 table.

  • Parity matrix

Validate

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 the NNNN-N/NN mask. 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//01 is valid. Whitespace around the value is ignored. Until 2.4.0 a single separator was accepted and 6201--5//01 was rejected.
  • A number is read only when it is a safe non-negative integer, and is left-padded to 7 digits like bare digits (111301 is 0111301). A negative, fractional or unsafe number is rejected.
ParameterTypeRequired
valuestring | numberyes
returnsboolean

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 the NNNN-N/NN mask.
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/javascript
Try it with JavaScript isValidCnae
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 (23) and the result in each library cnae.isValid

Format

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 (default false) 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 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. Until 2.4.0 pad returned 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).
ParameterTypeRequired
valuestring | numberyes
optionsFormatCnaeOptionsno
options.padbooleanno
returnsstring

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 (default false) 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 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 '', 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)
Code: brazilian-utils/javascript
Try it with JavaScript formatCnae
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 (32) and the result in each library cnae.format

Parse

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 (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 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)
Code: brazilian-utils/javascript
Try it with JavaScript parseCnae
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 (10) and the result in each library cnae.parse

Look up

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 the NNNN-N/NN mask, including a run of separators between the groups (6201--5//01 gives the entry).
  • Returns null for an unknown code or a value not in an accepted form.
  • It returns null exactly when cnae.isValid returns false.
ParameterTypeRequired
valuestring | numberyes
returnsCnae | 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 the NNNN-N/NN mask.
  • code comes back as the 7 bare digits; pass it to formatCnae for the NNNN-N/NN form.
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/javascript
Try it with JavaScript getCnae
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 (13) and the result in each library cnae.get

Official sources

See also CNPJ

Last updated on

On this page