Skip to content

Commit

Permalink
fix: bug when individual number of D number was more than 499
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaello committed Aug 20, 2020
1 parent c6d3657 commit 7e4f153
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
22 changes: 18 additions & 4 deletions __test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { set, reset } from 'mockdate';
import { set as setDate, reset as resetDate } from 'mockdate';
import { register, unregister } from 'timezone-mock';

import { validNumbers } from './listOfPersonalIdNumbers';
Expand Down Expand Up @@ -51,9 +51,18 @@ describe('Norwegian ID number validation', () => {
}
});

it('does not accept D numbers from future (born 2063)', () => {
it('does not accept D numbers from future', () => {
// born in 2063
expect(isValidCheckDigits('71106177273')).toBeTruthy();
expect(NorwegianId('71106177273').isDNumber()).toBeFalsy();

// born in 2097
expect(isValidCheckDigits('71019762194')).toBeTruthy();
expect(NorwegianId('71019762194').isDNumber()).toBeFalsy();

// born in 2097
expect(isValidCheckDigits('71019799918')).toBeTruthy();
expect(NorwegianId('71019799918').isDNumber()).toBeFalsy();
});

it('does not accept non D numbers when checking for D number', () => {
Expand All @@ -79,10 +88,15 @@ describe('Norwegian ID number validation', () => {

describe('A Norwegian person number (last 5 digits of ID number)', () => {
beforeEach(() => {
set('06/18/2017');
setDate('06/18/2017');
});
afterEach(() => {
reset();
resetDate();
});

it('D numbers belongs to persons in the 2000s if the individual numbers are in the [500, 1000) range', () => {
expect(possibleAgeOfPersonWithIdNumber('71010073132')).toBe(17);
expect(possibleAgeOfPersonWithIdNumber('50101099943')).toBe(6);
});

it('belongs to a person born in the 1900s if the three first digits are in the [0, 500) range', () => {
Expand Down
2 changes: 1 addition & 1 deletion __test__/listOfPersonalIdNumbers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const validNumbers = {
DNumbers: [
'42059199212',
'42051199116',
'67047000642',
'65038300827',
'42012070068',
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function idNumberType(elevenDigits: string): IDNumberType {
function possibleBirthDateOfBirthNumber(
elevenDigits: string,
): Date | undefined {
return getBirthDate(elevenDigits);
return getBirthDate(elevenDigits, IDNumberType.BirthNumber);
}

/**
Expand All @@ -243,6 +243,7 @@ function possibleBirthDateOfHNumber(elevenDigits: string): Date | undefined {
const correctedThirdDigit = (parseInt(elevenDigits[2]) - 4).toString();
return getBirthDate(
elevenDigits.slice(0, 2) + correctedThirdDigit + elevenDigits.slice(3, 11),
IDNumberType.HNumber,
);
}

Expand All @@ -252,13 +253,19 @@ function possibleBirthDateOfHNumber(elevenDigits: string): Date | undefined {
*/
function possibleBirthDateOfDNumber(elevenDigits: string): Date | undefined {
const correctedFirstDigit = (parseInt(elevenDigits[0]) - 4).toString();
return getBirthDate(correctedFirstDigit + elevenDigits.slice(1, 11));
return getBirthDate(
correctedFirstDigit + elevenDigits.slice(1, 11),
IDNumberType.DNumber,
);
}

/**
* @private
*/
function getBirthDate(elevenDigitsWithDDMMYY: string): Date | undefined {
function getBirthDate(
elevenDigitsWithDDMMYY: string,
idNumberType: IDNumberType,
): Date | undefined {
const DD = elevenDigitsWithDDMMYY.slice(0, 2);
const MM = elevenDigitsWithDDMMYY.slice(2, 4);
const YY = elevenDigitsWithDDMMYY.slice(4, 6);
Expand All @@ -268,6 +275,8 @@ function getBirthDate(elevenDigitsWithDDMMYY: string): Date | undefined {
let centuryPrefix = '20';
if (ageGroupNumber >= 0 && ageGroupNumber < 500) {
centuryPrefix = '19';
} else if (idNumberType === IDNumberType.DNumber) {
centuryPrefix = '20';
} else if (ageGroupNumber >= 500 && ageGroupNumber < 750 && YY_int >= 54) {
centuryPrefix = '18';
} else if (ageGroupNumber >= 900 && ageGroupNumber < 1000 && YY_int >= 40) {
Expand Down

0 comments on commit 7e4f153

Please sign in to comment.