-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from koblas/dz_nif
fix: add algerian NIF
- Loading branch information
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as nif from './nif'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters