Skip to content

Commit

Permalink
commonjs => es6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jksolbakken committed Mar 7, 2024
1 parent 5e9c584 commit a54d00d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@navikt/fnrvalidator",
"version": "2.0.0",
"type": "module",
"description": "Validation for Norwegian national id numbers (\"fødselsnummer\" and \"d-nummer\")",
"scripts": {
"build": "babel src -d dist",
Expand Down
18 changes: 6 additions & 12 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

const elevenDigits = new RegExp('^\\d{11}$')

const fnr = (digits) => {
export const fnr = (digits) => {
return idnr(digits)
}

const dnr = (digits) => {
export const dnr = (digits) => {
return idnr(digits)
}

const hnr = (digits) => {
export const hnr = (digits) => {
return idnr(digits)
}

const tnr = (digits) => {
export const tnr = (digits) => {
return idnr(digits)
}

const dnrAndHnr = (digits) => {
export const dnrAndHnr = (digits) => {
return idnr(digits)
}

const getType = (digits) => {
export const getType = (digits) => {
if (digits.substring(0, 1) >= 4 && digits.substring(2, 3) >= 4) {
return 'dnr-and-hnr'
}
Expand Down Expand Up @@ -94,9 +94,3 @@ const birthdate = (digits, type) => {
[] : ["invalid date"]
}

exports.fnr = fnr
exports.dnr = dnr
exports.hnr = hnr
exports.tnr = tnr
exports.dnrAndHnr = dnrAndHnr
exports.idnr = idnr
38 changes: 19 additions & 19 deletions tests/fnr.spec.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
'use strict'

const validator = require('../src/validator')
const chai = require('chai')
const expect = chai.expect
import { fnr, dnr, hnr, tnr, dnrAndHnr } from '../src/validator.js'

import { expect } from 'chai'

describe("fnr", function () {

it("should accept a valid one", function () {
const result = validator.fnr("13097248022")
const result = fnr("13097248022")
return expect(result).to.deep.equal({
status: "valid",
type: "fnr"
})
})

it("should accept a standard leap year", function () {
const result = validator.fnr("29029648784")
const result = fnr("29029648784")
return expect(result).to.deep.equal({
status: "valid",
type: "fnr"
})
})

it("should accept year 00 as valid leap year", function () {
const result = validator.fnr("29020075838")
const result = fnr("29020075838")
return expect(result).to.deep.equal({
status: "valid",
type: "fnr"
})
})

it("should reject if date is > 28 feb in a non leap year", function () {
const result = validator.fnr("29020112345")
const result = fnr("29020112345")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["checksums don't match", "invalid date"]
})
})

it("should compensate for checksum digits that are 11", function () {
const result = validator.fnr("15021951940")
const result = fnr("15021951940")
return expect(result).to.deep.equal({
status: "valid",
type: "fnr"
})
})

it("should reject if less than 11 digits", function () {
const result = validator.fnr("1234567890")
const result = fnr("1234567890")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["fnr, dnr or hnr must consist of 11 digits"]
})
})

it("should reject if more than 11 digits", function () {
const result = validator.fnr("123456789101")
const result = fnr("123456789101")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["fnr, dnr or hnr must consist of 11 digits"]
})
})

it("should reject if non-digits are present", function () {
const result = validator.fnr("1234567891A")
const result = fnr("1234567891A")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["fnr, dnr or hnr must consist of 11 digits"]
})
})

it("should reject if checksum 1 is invalid", function () {
const result = validator.fnr("13097248032")
const result = fnr("13097248032")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["checksums don't match"]
})
})

it("should reject if checksum 2 is invalid", function () {
const result = validator.fnr("13097248023")
const result = fnr("13097248023")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["checksums don't match"]
})
})

it("should reject if day is invalid", function () {
const result = validator.fnr("32127248022")
const result = fnr("32127248022")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["checksums don't match", "invalid date"]
})
})

it("should reject if month is invalid", function () {
const result = validator.fnr("13137248022")
const result = fnr("13137248022")
return expect(result).to.deep.equal({
status: "invalid",
reasons: ["checksums don't match", "invalid date"]
Expand All @@ -107,7 +107,7 @@ describe("fnr", function () {
describe("dnr", function () {
// dnr is identical to fnr except for the first digit
it("should accept a valid one", function () {
const result = validator.dnr("53097248016")
const result = dnr("53097248016")
return expect(result).to.deep.equal({
status: "valid",
type: "dnr"
Expand All @@ -118,7 +118,7 @@ describe("dnr", function () {
describe("hnr", function () {
// hnr is identical to fnr except for the third digit
it("should accept a valid one", function () {
const result = validator.hnr("13527248013")
const result = hnr("13527248013")
return expect(result).to.deep.equal({
status: "valid",
type: "hnr"
Expand All @@ -128,7 +128,7 @@ describe("hnr", function () {
describe("tnr", function () {
// tnr is identical to fnr except for the third digit which is increased with 8
it("should accept a valid one", function () {
const result = validator.tnr("10915596784");
const result = tnr("10915596784");
return expect(result).to.deep.equal({
status: "valid",
type: "tnr",
Expand All @@ -140,7 +140,7 @@ describe("tnr", function () {
describe("dnr-and-hnr", function () {
// combined dnr and hnr - so both first and third digit is increased with 4
it("should accept a valid one", function () {
const result = validator.dnrAndHnr("68467038838");
const result = dnrAndHnr("68467038838");
return expect(result).to.deep.equal({
status: "valid",
type: "dnr-and-hnr",
Expand Down

0 comments on commit a54d00d

Please sign in to comment.