From b2012e5ddc0570a225c652202d956dd57d37b7a3 Mon Sep 17 00:00:00 2001 From: Kristoffer Larsen Hopland Date: Tue, 9 Apr 2024 14:40:09 +0200 Subject: [PATCH] Added support to 'dnr-and-tin'. fixes #469 --- package-lock.json | 4 ++-- src/validator.ts | 21 ++++++++++++++++----- tests/fnr.spec.ts | 14 +++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index e852e60..76f81c4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@navikt/fnrvalidator", - "version": "2.0.6", + "version": "2.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@navikt/fnrvalidator", - "version": "2.0.6", + "version": "2.0.7", "license": "MIT", "dependencies": { "typescript": "^5.4.2" diff --git a/src/validator.ts b/src/validator.ts index b97c972..204a7dc 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -5,7 +5,7 @@ type CHECKSUM_ERROR = "checksums don't match"; type DATE_ERROR = 'invalid date'; type ErrorReason = LENGTH_ERROR | CHECKSUM_ERROR | DATE_ERROR; type OkResult = { status: 'valid'; type: NrType }; -type NrType = 'dnr' | 'fnr' | 'hnr' | 'tnr' | 'dnr-and-hnr' +type NrType = 'dnr' | 'fnr' | 'hnr' | 'tnr' | 'dnr-and-hnr' | 'dnr-and-tnr'; type ErrorResult = { status: 'invalid'; reasons: ErrorReason[] }; type ValidationResult = OkResult | ErrorResult; @@ -31,16 +31,25 @@ export const dnrAndHnr = (digits: string): ValidationResult => { return idnr(digits) } +export const dnrAndTnr = (digits: string): ValidationResult => { + return idnr(digits) +} + export const getType = (digits: string): NrType => { let nrs = digits.split("").map(Number) + if (nrs[0] >= 4 && nrs[2] >= 8) { + return 'dnr-and-tnr' + } if (nrs[0] >= 4 && nrs[2] >= 4) { return 'dnr-and-hnr' } - else if (nrs[0] >= 4) { + if (nrs[0] >= 4) { return 'dnr' - } else if (nrs[2] >= 8) { + } + if (nrs[2] >= 8) { return "tnr" - } else if (nrs[2] >= 4) { + } + if (nrs[2] >= 4) { return 'hnr' } return 'fnr' @@ -83,7 +92,7 @@ const checksums = (digits: string): Array => { } // copied from https://stackoverflow.com/questions/5812220/how-to-validate-a-date -const birthdate = (digits: string, type: string): Array => { +const birthdate = (digits: string, type: NrType): Array => { if (type === 'dnr') { digits = (Number(digits.substring(0, 1)) - 4) + digits.substring(1) } else if (type === 'hnr') { @@ -92,6 +101,8 @@ const birthdate = (digits: string, type: string): Array => { digits = digits.substring(0, 2) + (Number(digits.substring(2, 3)) - 8) + digits.substring(3) } else if (type === 'dnr-and-hnr') { digits = (Number(digits.substring(0, 1)) - 4) + digits.substring(1, 2) + (Number(digits.substring(2, 3)) - 4) + digits.substring(3) + } else if (type === 'dnr-and-tnr') { + digits = Number(digits.substring(0, 1)) -4 + digits.substring(1, 2) + (Number(digits.substring(2, 3)) - 8) + digits.substring(3) } const day = Number(digits.substring(0, 2)) diff --git a/tests/fnr.spec.ts b/tests/fnr.spec.ts index 4a9b6fd..16e763e 100644 --- a/tests/fnr.spec.ts +++ b/tests/fnr.spec.ts @@ -1,6 +1,6 @@ 'use strict' -import { fnr, dnr, hnr, tnr, dnrAndHnr } from '../src/validator' +import { fnr, dnr, hnr, tnr, dnrAndHnr, dnrAndTnr } from '../src/validator' describe("fnr", function () { @@ -146,3 +146,15 @@ describe("dnr-and-hnr", function () { }); }) ; + +describe("dnr-and-tnr", function () { + // combined dnr and tnr - so first digit is increased with 4 and third digit is increased with 8 + it("should accept a valid one", function () { + const result = dnrAndTnr("50846202355"); + return expect(result).toEqual({ + status: "valid", + type: "dnr-and-tnr", + }); + }); +}) +;