Skip to content

Commit

Permalink
Merge pull request #490 from khopland/fix/469
Browse files Browse the repository at this point in the history
Added support to 'dnr-and-tin'
  • Loading branch information
vebnor authored Jun 5, 2024
2 parents a4510c6 + 7b27c48 commit 03b0d37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'
Expand Down Expand Up @@ -83,7 +92,7 @@ const checksums = (digits: string): Array<CHECKSUM_ERROR> => {
}

// copied from https://stackoverflow.com/questions/5812220/how-to-validate-a-date
const birthdate = (digits: string, type: string): Array<DATE_ERROR> => {
const birthdate = (digits: string, type: NrType): Array<DATE_ERROR> => {
if (type === 'dnr') {
digits = (Number(digits.substring(0, 1)) - 4) + digits.substring(1)
} else if (type === 'hnr') {
Expand All @@ -92,6 +101,8 @@ const birthdate = (digits: string, type: string): Array<DATE_ERROR> => {
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))
Expand Down
14 changes: 13 additions & 1 deletion tests/fnr.spec.ts
Original file line number Diff line number Diff line change
@@ -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 () {

Expand Down Expand Up @@ -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",
});
});
})
;

0 comments on commit 03b0d37

Please sign in to comment.