-
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.
- Loading branch information
Showing
5 changed files
with
115 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
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 pin from './pin'; |
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,34 @@ | ||
import { validate, format } from './pin'; | ||
import { InvalidLength, InvalidFormat } from '../exceptions'; | ||
|
||
describe('ke/pin', () => { | ||
it('format:P051365947M', () => { | ||
const result = format('P051365947M'); | ||
|
||
expect(result).toEqual('P051365947M'); | ||
}); | ||
|
||
it('fvalidate:P051365947M', () => { | ||
const result = validate('P051365947M'); | ||
|
||
expect(result.isValid && result.compact).toEqual('P051365947M'); | ||
}); | ||
|
||
test.each(['P051365947M', 'P051365947M'])('validate:%s', value => { | ||
const result = validate(value); | ||
|
||
expect(result.isValid).toEqual(true); | ||
}); | ||
|
||
it('validate:P051365947', () => { | ||
const result = validate('P051365947'); | ||
|
||
expect(result.error).toBeInstanceOf(InvalidLength); | ||
}); | ||
|
||
it('validate:P05136594M7', () => { | ||
const result = validate('P05136594M7'); | ||
|
||
expect(result.error).toBeInstanceOf(InvalidFormat); | ||
}); | ||
}); |
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,77 @@ | ||
/** | ||
* PIN (Personal Identification Number, Kenya tax number). | ||
* | ||
* The Personal Identification Number (KRA PIN) is an 11 digit unique number that | ||
* is issued by Kenya Revenue Authority (KRA) for purposes of transacting business | ||
* with KRA, other Government agencies and service providers. It can be issued for | ||
* individuals and non-individuals like companies, schools, organisations, etc. | ||
* | ||
* The number consists of 11 characters, where the first one is an A (for | ||
* individuals) or a P (for non-individuals), the last one is a letter, and the | ||
* rest are digits. | ||
* | ||
* DESCRIPTION | ||
* | ||
* Source | ||
* https://www.kra.go.ke/individual/individual-pin-registration/learn-about-pin/about-pin | ||
* https://itax.kra.go.ke/KRA-Portal/pinChecker.htm | ||
* | ||
* 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 = /^[A|P]{1}[0-9]{9}[A-Z]{1}$/i; | ||
|
||
const impl: Validator = { | ||
name: 'Personal Identification Number', | ||
localName: '', | ||
abbreviation: 'PIN', | ||
|
||
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 !== 11) { | ||
return { isValid: false, error: new exceptions.InvalidLength() }; | ||
} | ||
|
||
if (!validRe.test(value)) { | ||
return { isValid: false, error: new exceptions.InvalidFormat() }; | ||
} | ||
|
||
return { | ||
isValid: true, | ||
compact: value, | ||
isIndividual: value[0] === 'A', | ||
isCompany: value[0] === 'P', | ||
}; | ||
}, | ||
}; | ||
|
||
export const { name, localName, abbreviation, validate, format, compact } = | ||
impl; |