Skip to content

Commit

Permalink
fix: added kenya pin
Browse files Browse the repository at this point in the history
  • Loading branch information
koblas committed Jul 9, 2023
1 parent f350bf0 commit 6364416
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ All country validators are in the "namespace" of the ISO country code.
| Malta | MT | VAT | Vat | Maltese VAT number |
| Mauritius | MU | NID | Person | ID number (Mauritian national identifier) |
| Japan | JP | CN | Company | 法人番号, hōjin bangō, Japanese Corporate Number |
| Kenya | KE | PIN | Person/Company | Personal Identification Number, Kenya tax number |
| South Korea | KR | BRN | Company | 사업자 등록 번호, South Korea Business Registration Number) |
| South Korea | KR | RRN | Person | South Korean resident registration number |
| Mexico | MX | RFC | Tax/Vat | Tax Identifier (Registro Federal de Contribuyentes) |
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import * as IN from './in';
import * as IS from './is';
import * as IT from './it';
import * as JP from './jp';
import * as KE from './ke';
import * as KR from './kr';
import * as LI from './li';
import * as LT from './lt';
Expand Down Expand Up @@ -140,6 +141,7 @@ export const stdnum: Record<string, Record<string, Validator>> = {
LU,
LV,
JP,
KE,
KR,
MA,
MC,
Expand Down
1 change: 1 addition & 0 deletions src/ke/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as pin from './pin';
34 changes: 34 additions & 0 deletions src/ke/pin.spec.ts
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);
});
});
77 changes: 77 additions & 0 deletions src/ke/pin.ts
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;

0 comments on commit 6364416

Please sign in to comment.