Skip to content

Commit

Permalink
Merge pull request #114 from rajithaeyee/feature/lk
Browse files Browse the repository at this point in the history
Sri Lanka NIC number validation
  • Loading branch information
koblas authored Mar 11, 2024
2 parents 1be5f06 + 21e0b7c commit 05eb9c3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ How you can help! This library currently support about half the countries in the
| Spain | ES | NIE | Person | Identity code foreigner (Número de Identificación de Extranjero) |
| Spain | ES | NIF | Tax | Tax Identifier (Número de Identificación Fiscal) |
| Spain | ES | NSS | Person | El número de Seguridad Social, Social Security Number |
| Sri Lanka | LK | NIC | Person | NIC Number |
| Uruguay | UY | RUT | Tax/Vat | Tax Identifier (Registro Único Tributario) |
| Uruguay | UY | CEDULA | Person | Person Identifier (Cédula de Residencia) |
| Uruguay | UY | NIE | Person | ForeignersI identification Number |
Expand Down
1 change: 1 addition & 0 deletions src/lk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as nic from './nic';
40 changes: 40 additions & 0 deletions src/lk/nic.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { validate, format } from './nic';
import { InvalidLength, InvalidFormat } from '../exceptions';

describe('lk/nic', () => {
it('format:199439848733', () => {
const result = format('199439848733');

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

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

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

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

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

it('validate:942R81632b', () => {
const result = validate('942281632b');

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

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

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

it('validate:23417', () => {
const result = validate('23417');
expect(result.error).toBeInstanceOf(InvalidLength);
});

});
65 changes: 65 additions & 0 deletions src/lk/nic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* The National Identity Card (abbreviation: NIC) is the identity document in use in Sri Lanka.
* It is compulsory for all Sri Lankan citizens who are fifteen years of age and older to have their NICs.
* NICs are issued by the Department for Registration of Persons.
* The Registration of Persons Act No.32 of 1968 as amended by Act Nos 28 and 37 of 1971 and Act No.11 of 1981 legislates the issuance and usage of NICs.
*
*
* source:
* https://en.wikipedia.org/wiki/National_identity_card_(Sri_Lanka)
*/


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 impl: Validator = {
name: 'Sri Lankan National Identity Card Number',
localName: 'NIC Number',
compact(input: string): string {
const [value, err] = clean(input);

if (err) {
throw err;
}

return value;
},

format(input: string): string {
const [value] = clean(input);
// NIC has better readability without any additional formattings
return value.toLowerCase();
},

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

if (error) {
return { isValid: false, error };
}

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

// Check if NIC matches the specified patterns (check for both old format & new format )
if (!/^[\d]{9}[vVxX]$|^[\d]{12}$/.test(value)) {
return { isValid: false, error: new exceptions.InvalidFormat('Invalid NIC format') };
}

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

export const { name, localName, validate, format, compact } = impl;

0 comments on commit 05eb9c3

Please sign in to comment.