npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@digitalarkivet/nin-validator

v0.2.1

Published

Validating and parsing norwegian national identity numbers in Typescript

Downloads

26

Readme

nin-validator: Norwegian national identity numbers

This is a validator/parser for norwegian national identity numbers, written in Typescript.

Basic usage is very simple:

import validateNin from "@digitalarkivet/nin-validator"
// The default export accepts birth numbers and D-numbers
if(!validateNin(someNin)) {
    throw "Invalid NIN"
}

To extract more information, use parseNin. Then, you have the option to specify which number types to accept. The export ALL_NIN_TYPES include rarely-used types like H-numbers and FH-numbers, while DEFAULT_NIN_TYPES include birth number and D number. You can also mix-and-match by using NinNumberType directly.

import {parseNin, ALL_NIN_TYPES, DEFAULT_NIN_TYPES, NinNumberType} from "@digitalarkivet/nin-validator"

const {info, error} = parseNin(someNin)
// Is the same as
const {info, error} = parseNin(someNin, ...DEFAULT_NIN_TYPES)
// Or, to can accept all types (very rarely what you want)
const {info, error} = parseNin(someNin, ...ALL_NIN_TYPES)
// Or, to accept specific types
const {info, error} = parseNin(someNin, NinNumberType.BirthNumber, NinNumberType.HNumber)

The info key will be undefined on error. On success, it contains an object with gender, dateOfBirth, numberType, and isTestNumber keys.

The error key will contain a NinValidationError on error, and be undefined on success.

The NinInfo type

Gender

The Gender enum uses the ISO/IEC 5218 codes:

  • 0 = Not known;
  • 1 = Male;
  • 2 = Female;
  • 9 = Not applicable.

parseNin will return Not known for FH-numbers, and in the future for "new" national identification numbers that do not carry gender information.

Date of birth

A JS date, midnight on the date of birth, in local time. Contains undefined for FH numbers.

Number type

One of the NinNumberType values. This is a Typescript enum, backed by a string.

Test numbers

This flag is set to true if the number originates from "Tenor", the test utility of the norwegian tax authority.

Since Arkivverket has no reason to exclude test numbers from production use (they will not occur in real archives, and if they do, we want to display them), test numbers can not be rejected when validating a nin with nin-validator.

The NinValidationError type

A NinValidationError is one of a predefined set of error codes, saying which check failed.

  • FORMAT means the input string is not 11 digits.
  • CHECKSUM means the checksum validation failed
  • DATE means the date does not exist (leap year?), or the century code did not match the individual part of the nin.
  • INVALID_TYPE means the nin is valid, but the type is not one of the allowed types passed to parseNin()

About the test data

The test data for birth numbers, D numbers and H-numbers is synthetically generated.

The test data for "tenor test numbers" is generated by Tenor.