Skip to content

Commit

Permalink
Merge pull request #87 from koblas/dz_nif
Browse files Browse the repository at this point in the history
fix: add algerian NIF
  • Loading branch information
koblas committed Jul 9, 2023
2 parents 1088d3a + d1231fd commit 20a759f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ All country validators are in the "namespace" of the ISO country code.
| Dominican Republic | DO | CEDULA | Person | Person Identifier (Cédula de Residencia) |
| Dominican Republic | DO | NCF | Vat | Tax Receipt Number (Números de Comprobante Fiscal) |
| Dominican Republic | DO | RNC | Tax | Person Identifier (Registro Nacional del Contribuyente) |
| Algeria | DZ | NIF | Person/Company | Numéro d'Identification Fiscale, Algeria tax number |
| Ecuador | EC | CI | Person | Ecuadorian person identifier (Cédula de identidad) |
| Estonia | EE | IK | Person | Isikukood (Estonian Personcal ID number). |
| Estonia | EE | KMKR | Company | KMKR (Käibemaksukohuslase, Estonian VAT number) |
Expand Down
1 change: 1 addition & 0 deletions src/dz/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as nif from './nif';
39 changes: 39 additions & 0 deletions src/dz/nif.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { validate, format } from './nif';
import { InvalidLength, InvalidFormat } from '../exceptions';

describe('dz/nif', () => {
it('format:000 216 001 808 337 13010', () => {
const result = format('000 216 001 808 337 13010');

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

it('fvalidate:000 216 001 808 337 13010', () => {
const result = validate('000 216 001 808 337 13010');

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

test.each([
'416001000000007',
'408 020 000 150 039',
'41201600000606600001',
'000 216 001 808 337 13010',
])('validate:%s', value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
});

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

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

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

expect(result.error).toBeInstanceOf(InvalidLength);
});
});
80 changes: 80 additions & 0 deletions src/dz/nif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* NIF, sometimes N.I.F. (Numéro d'Identification Fiscale, Algeria tax number).
*
* The NIF was adopted by the Algerian tax authorities on 2006, replacing the NIS
* number.
*
* The NIF applies to physical persons, legal persons, legal entities,
* administrative entities, local branches for foreign companies, associations,
* professional organisations, etc.
*
* The NIF consists of 15 digits, but sometimes it can be 20 digits long in order
* to represent branches or secondary establishments.
*
* Source
* http://www.jecreemonentreprise.dz/index.php?option=com_content&view=article&id=612&Itemid=463&lang=fr
* https://www.mf.gov.dz/index.php/fr/fiscalite
* https://cnrcinfo.cnrc.dz/numero-didentification-fiscale-nif/
* https://nifenligne.mfdgi.gov.dz/
* http://nif.mfdgi.gov.dz/nif.asp
*
* ENTITY/PERSON
*/

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, ' -');
}

// const validRe = /^[PCGQV]{1}00[A-Z0-9]{8}$/;

// const ALPHABET = '0123456789X';

const impl: Validator = {
name: 'Algeria tax number',
localName: "Numéro d'Identification Fiscale",
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 !== 15 && value.length !== 20) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!strings.isdigits(value)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}

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

export const { name, localName, abbreviation, validate, format, compact } =
impl;
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as CZ from './cz';
import * as DE from './de';
import * as DK from './dk';
import * as DO from './do';
import * as DZ from './dz';
import * as EC from './ec';
import * as EE from './ee';
import * as EG from './eg';
Expand Down Expand Up @@ -113,8 +114,9 @@ export const stdnum: Record<string, Record<string, Validator>> = {
CY,
CZ,
DE,
DO,
DK,
DO,
DZ,
EC,
EE,
EG,
Expand Down

0 comments on commit 20a759f

Please sign in to comment.