Skip to content

Commit

Permalink
feat: add support for NIT without the hyphen
Browse files Browse the repository at this point in the history
* feat: add support for NIT without the hyphen

* style: add example of NIT without hyphen in README

* style: remove blank spaces by formatting

* style: remove blank spaces by formatting

* style: remove blank spaces by formatting
  • Loading branch information
alepaz authored Oct 5, 2020
1 parent 51f3d2d commit 3f08aab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ import { isNIT } from 'sivar-utils';
const str = 'test';
const fakeNIT = '0000-000000-000-0';
const validNIT = '0614-051286-129-4'; // DISCLAIMER: taken from Google
const alsoValidNIT = '06140512861294';

isNIT(str); // false
isNIT(fakeNIT); // false
isNIT(validNIT); // true
isNIT(alsoValidNIT); // true
```

- #### isMobilePhoneNumber
Expand Down
9 changes: 9 additions & 0 deletions src/lib/__tests__/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ describe('documents', () => {
['0614-051286-129-4', true],
['0614-110790-113-7', true],
['0614-080803-111-4', true],
['06140512861294', true],
['06142902031111', false],
['0614080803111', false],
['06141107901137', true],
['06140808031114', true],
['0614-1107901137', false],
['0614080803-1114', false],
['0614-110790-1137', false],
['0614080803111-4', false],
];
test.each(cases)('given %s should return %s', (arg, expected) => {
const result = isNIT(arg);
Expand Down
18 changes: 16 additions & 2 deletions src/lib/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isMunicipalityCode } from './municipalities';
import { isDate } from './utils';

export const duiRegExp = /(^\d{8})-(\d$)/;
export const nitRegExp = /(^\d{4})-(\d{6})-(\d{3})-(\d$)/;
export const nitRegExp = /((^\d{4})-(\d{6})-(\d{3})-(\d$))|(^\d{14}$)/;
export const passportRegExp = /^[a-zA-Z]\d{8}$/;

/**
Expand Down Expand Up @@ -54,6 +54,18 @@ function calculateNitVerification(digits: string): number {
return sum > 1 ? 11 - sum : 0;
}

/**
* @param {string} str A string representing NIT digits (with hyphen)
* @returns {string[]} NIT compounds
*/
function splitNIT(str: string): string[] {
const municipality = str.substring(0, 4);
const birthdate = str.substring(4, 10);
const correlative = str.substring(10, 13);
const verifier = str.substring(13);
return [municipality, birthdate, correlative, verifier];
}

/**
* Verifies that given NIT format is valid
*
Expand All @@ -63,7 +75,9 @@ function calculateNitVerification(digits: string): number {
export function isNIT(str: string): boolean {
if (!nitRegExp.test(str)) return false;

const [municipality, birthdate, correlative, verifier] = str.split('-');
const [municipality, birthdate, correlative, verifier] = str.includes('-')
? str.split('-')
: splitNIT(str);
if (!isMunicipalityCode(municipality) || !isDate(birthdate)) return false;

const digits = municipality + birthdate + correlative;
Expand Down

0 comments on commit 3f08aab

Please sign in to comment.