Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ How you can help! This library currently support about half the countries in the
| Country | Code | Name | Group | Meaning |
| ---------------------- | ---- | --------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
| Andorra | AD | NRT | Tax | Tax Register Identifier (Número de Registre Tributari) |
| Angola | AO | BI | Person | Angolan Identity Card (Bilhete de Identidade) |
| Angola | AO | NIF | Tax | Angolan Tax Identification Number (Número de Identificação Fiscal) |
Comment thread
frankdors marked this conversation as resolved.
| Albania | AL | NIPT | Vat | Albanian Vat Identifier (Numri i Identifikimit për Personin e Tatueshëm) |
| Anguilla | AI | TIN | Tax | Tax Identification Number |
| Argentina | AR | CBU | Bank | Single Banking Code (Clave Bancaria Uniforme) |
Expand Down Expand Up @@ -152,6 +154,8 @@ How you can help! This library currently support about half the countries in the
| Mexico | MX | CLABE | Bank | Bank Account (Clave Bancaria Estandarizada) |
| Montenegro | ME | JMBG | Person | Unique Master Citizen Number |
| Montenegro | ME | PIB | Tax/Vat | Poreski Identifikacioni Broj, Montenegro tax number |
| Mozambique | MZ | BI | Person | Bilhete de Identidade, Mozambican national identification number |
| Mozambique | MZ | NUIT | Tax/Vat | Número Único de Identificação Tributária, Mozambican tax identifier number |
| Malaysia | MY | NRIC | Person | Malaysian National Registration Identity Card Number |
| Netherlands | NL | BSN | Person | Burgerservicenummer, the Dutch citizen identification number |
| Netherlands | NL | BTW | Vat | Btw-identificatienummer (Omzetbelastingnummer, the Dutch VAT number) |
Expand Down
55 changes: 55 additions & 0 deletions src/ao/bi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { validate, format, compact } from './bi';
import { InvalidLength, InvalidFormat } from '../exceptions';

describe('ao/bi', () => {
it('format:000204688CA010', () => {
const result = format('000204688CA010');

expect(result).toEqual('000204688CA010');
});

it('compact:000204688CA010', () => {
const result = compact('000204688CA010');

expect(result).toEqual('000204688CA010');
});

it('validate:007128828LA043', () => {
const result = validate('007128828LA043');

expect(result.isValid && result.compact).toEqual('007128828LA043');
});

it('validate:0000100441CA037 (invalid length)', () => {
const result = validate('0000100441CA037');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:123456789 (invalid length)', () => {
const result = validate('123456789');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:123456789CA12 (invalid length)', () => {
const result = validate('123456789CA12');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:000204688cA010 (case normalization)', () => {
const result = validate('000204688cA010');

if (!result.isValid) {
throw new Error('Expected valid');
}
expect(result.compact).toEqual('000204688CA010');
});

it('validate:00020468800010 (invalid pattern - no letters)', () => {
const result = validate('00020468800010');

expect(result.error).toBeInstanceOf(InvalidFormat);
});
});
67 changes: 67 additions & 0 deletions src/ao/bi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* BI (Bilhete de Identidade, Angola Identity Card).
*
* The Angolan BI is a national identification document.
* The number consists of 14 characters:
* - 9 digits
* - 2 letters
* - 3 digits
*
* Example: 000204688CA010
*/

import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input.toUpperCase(), ' -.');
}

const BIO_REGEX = /^\d{9}[A-Z]{2}\d{3}$/;

const impl: Validator = {
name: 'Angola Identity Card',
localName: 'Bilhete de Identidade',
abbreviation: 'BI',

compact(input: string): string {
const [value, err] = clean(input);

if (err) {
throw err;
}

return value;
},

format(input: string): string {
const [value] = clean(input);

return value;
},

validate(input: string): ValidateReturn {
const [value, error] = clean(input);

if (error) {
return { isValid: false, error };
}
if (value.length !== 14) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!BIO_REGEX.test(value)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}

return {
isValid: true,
compact: value,
isIndividual: true,
isCompany: false,
};
},
};

export const { name, localName, abbreviation, validate, format, compact } =
impl;
2 changes: 2 additions & 0 deletions src/ao/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as nif from './nif';
export * as bi from './bi';
52 changes: 52 additions & 0 deletions src/ao/nif.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { validate, format, compact } from './nif';
import { InvalidLength, InvalidFormat } from '../exceptions';

describe('ao/nif', () => {
it('format:0000000001', () => {
const result = format('0000000001');

expect(result).toEqual('0000000001');
});

it('compact:0000000001', () => {
const result = compact('0000000001');

expect(result).toEqual('0000000001');
});

it('validate:5001191020 (company)', () => {
const result = validate('5001191020');

if (!result.isValid) throw new Error('Expected valid');
expect(result.compact).toEqual('5001191020');
expect(result.isCompany).toEqual(true);
expect(result.isIndividual).toEqual(false);
});

it('validate:003534962LA033 (individual)', () => {
const result = validate('003534962LA033');

if (!result.isValid) throw new Error('Expected valid');
expect(result.compact).toEqual('003534962LA033');
expect(result.isCompany).toEqual(false);
expect(result.isIndividual).toEqual(true);
});

it('validate:123456789 (invalid length)', () => {
const result = validate('123456789');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:12345678901 (invalid length)', () => {
const result = validate('12345678901');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:123456789A (invalid format)', () => {
const result = validate('123456789A');

expect(result.error).toBeInstanceOf(InvalidFormat);
});
});
72 changes: 72 additions & 0 deletions src/ao/nif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* NIF (Número de Identificação Fiscal, Angola Tax Identification Number).
*
* The Angolan NIF is a 10-digit or 14-character number used for tax purposes.
*
* Source:
* https://validarnif.pt/pt/validar-nif-angola/
*/

import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input.toUpperCase(), ' -.');
}

const impl: Validator = {
name: 'Angola Tax Identification Number',
localName: 'Número de Identificação Fiscal',
abbreviation: 'NIF',

compact(input: string): string {
const [value, err] = clean(input);

if (err) {
throw err;
}

return value;
},

format(input: string): string {
const [value] = clean(input);

return value;
},

validate(input: string): ValidateReturn {
const [value, error] = clean(input);

if (error) {
return { isValid: false, error };
}
if (value.length === 10 && strings.isdigits(value)) {
return {
isValid: true,
compact: value,
isIndividual: false,
isCompany: true,
};
}

if (value.length === 14 && /^\d{9}[A-Z]{2}\d{3}$/.test(value)) {
return {
isValid: true,
compact: value,
isIndividual: true,
isCompany: false,
};
}

if (value.length !== 10 && value.length !== 14) {
return { isValid: false, error: new exceptions.InvalidLength() };
}

return { isValid: false, error: new exceptions.InvalidFormat() };
},
};

export const { name, localName, abbreviation, validate, format, compact } =
impl;
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as AD from './ad';
import * as AI from './ai';
import * as AL from './al';
import * as AO from './ao';
import * as AR from './ar';
import * as AT from './at';
import * as AU from './au';
Expand Down Expand Up @@ -61,6 +62,7 @@ import * as MT from './mt';
import * as MU from './mu';
import * as MX from './mx';
import * as MY from './my';
import * as MZ from './mz';
import * as NL from './nl';
import * as NO from './no';
import * as NZ from './nz';
Expand Down Expand Up @@ -88,14 +90,15 @@ import * as UY from './uy';
import * as VE from './ve';
import * as VN from './vn';
import * as ZA from './za';
import { Validator } from './types';
import type { Validator } from './types';

export { Validator } from './types';
export type { Validator } from './types';

// Live an uppercase world, to prevent keyword collisions
export const stdnum: Record<string, Record<string, Validator>> = {
AD,
AL,
AO,
AR,
AT,
AU,
Expand Down Expand Up @@ -156,6 +159,7 @@ export const stdnum: Record<string, Record<string, Validator>> = {
MU,
MX,
MY,
MZ,
NL,
NO,
NZ,
Expand Down Expand Up @@ -189,6 +193,7 @@ export const personValidators: Record<string, Validator[]> = {
AD: [AD.nrt],
AI: [AI.tin],
AL: [AL.nipt],
AO: [AO.nif, AO.bi],
AR: [AR.cuit, AR.dni],
AT: [AT.vnr],
AU: [AU.tfn],
Expand Down Expand Up @@ -237,6 +242,7 @@ export const personValidators: Record<string, Validator[]> = {
MU: [MU.nid],
MX: [MX.curp, MX.rfc],
MY: [MY.nric],
MZ: [MZ.bi, MZ.nuit],
NL: [NL.onderwijsnummer, NL.bsn],
NO: [NO.fodselsnummer],
NZ: [NZ.ird],
Expand Down Expand Up @@ -266,6 +272,7 @@ export const entityValidators: Record<string, Validator[]> = {
AD: [AD.nrt],
AI: [AI.tin],
AL: [AL.nipt],
AO: [AO.nif],
AR: [AR.cuit],
AT: [AT.businessid, AT.tin, AT.uid],
AU: [AU.abn, AU.acn, AU.tfn],
Expand Down Expand Up @@ -312,6 +319,7 @@ export const entityValidators: Record<string, Validator[]> = {
MA: [MA.ice, MA.ice9],
MT: [MT.vat],
MX: [MX.rfc],
MZ: [MZ.nuit],
NL: [NL.btw],
NO: [NO.mva, NO.orgnr],
NZ: [NZ.ird],
Expand Down
Loading