From 6364416eb2c15090f6f05fd578d7b7d83d27e86a Mon Sep 17 00:00:00 2001 From: David Koblas Date: Sun, 9 Jul 2023 05:52:48 -0400 Subject: [PATCH] fix: added kenya pin --- README.md | 1 + src/index.ts | 2 ++ src/ke/index.ts | 1 + src/ke/pin.spec.ts | 34 ++++++++++++++++++++ src/ke/pin.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 src/ke/index.ts create mode 100644 src/ke/pin.spec.ts create mode 100644 src/ke/pin.ts diff --git a/README.md b/README.md index e21dae7..1623782 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/src/index.ts b/src/index.ts index 6cb4507..a4a1e16 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -140,6 +141,7 @@ export const stdnum: Record> = { LU, LV, JP, + KE, KR, MA, MC, diff --git a/src/ke/index.ts b/src/ke/index.ts new file mode 100644 index 0000000..70b8861 --- /dev/null +++ b/src/ke/index.ts @@ -0,0 +1 @@ +export * as pin from './pin'; diff --git a/src/ke/pin.spec.ts b/src/ke/pin.spec.ts new file mode 100644 index 0000000..ed63028 --- /dev/null +++ b/src/ke/pin.spec.ts @@ -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); + }); +}); diff --git a/src/ke/pin.ts b/src/ke/pin.ts new file mode 100644 index 0000000..8ab65f6 --- /dev/null +++ b/src/ke/pin.ts @@ -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 { + 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;