-
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 #114 from rajithaeyee/feature/lk
Sri Lanka NIC number validation
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
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 nic from './nic'; |
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,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); | ||
}); | ||
|
||
}); |
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,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; |