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

kennitala

v2.0.6

Published

Icelandic social security number (kennitölur) utilities for servers and clients

Downloads

17,543

Readme

Kennitala

Icelandic national ID (kennitölur) utilities for servers and clients. Now with TypeScript support!

Build Status npm npm npm bundle size

Install with npm:

npm install kennitala

Examples

import { isValid } from "kennitala";

// Check if a kennitala is valid for either a person or a company
isValid("3108962099"); // returns true
isValid("8281249124"); // returns false

Heads Up for Storing in Databases

This library validates kennitölur both with - spacers and without, so remember to sanitize your kennitala before storing it in a database!

You can use the included sanitize function:

import { sanitize } from "kennitala";

const sanitizedKennitala = sanitize("310896-2099");
// returns '3108962099'

More Examples

import {
  isValid,
  isPersonKennitala,
  isCompanyKennitala,
  formatKennitala,
  info,
  generatePerson,
  generateCompany,
} from 'kennitala';

// Get detailed information about a kennitala
const kennitalaInfo = info('3108962099');
// kennitalaInfo contains:
{
  kt: '3108962099',
  valid: true,
  type: 'person',
  birthday: new Date('1996-08-31T00:00:00.000Z'),
  birthdayReadable: 'Sat Aug 31 1996',
  age: 27,
}

// Check if a kennitala is valid for a person (returns false for companies)
isPersonKennitala('3108962099');            // returns true
isPersonKennitala('601010-0890');           // returns false (invalid date)
isPersonKennitala(3108962099);              // returns false (invalid input type)
isPersonKennitala('31^_^08!!96LOL20T_T99'); // returns false (invalid format)

// Check if a kennitala is valid for a company (returns false for persons)
isCompanyKennitala('6010100890');  // returns true
isCompanyKennitala('601010-0890'); // returns true
isCompanyKennitala('3108962099');  // returns false

// Format a kennitala by adding a traditional '-' spacer
formatKennitala('3108962099');       // returns '310896-2099'
formatKennitala('3108962099', false); // returns '3108962099'

API Documentation

Below is the API based on the type definitions from the refactored TypeScript library.

isValid(kennitala: string, options?: { allowTestKennitala?: boolean }): boolean

Checks if the kennitala checksum is correct for either a person or company. Non-digit characters are removed before validation.

  • Parameters:

    • kennitala: The kennitala string to validate.
    • options (optional): An object with the following property:
      • allowTestKennitala (default: false): Set to true to allow validation of test kennitala numbers.
  • Returns: true if the kennitala is valid, false otherwise.

isPersonKennitala(kennitala: string, options?: { allowTestKennitala?: boolean }): boolean

Checks if the kennitala is valid for a person. The day of birth must be between 1-31. Non-digit characters are removed before validation.

  • Parameters:

    • kennitala: The kennitala string to validate.
    • options (optional): Same as in isValid.
  • Returns: true if the kennitala is valid for a person, false otherwise.

isCompanyKennitala(kennitala: string): boolean

Checks if the kennitala is valid for a company. The day of birth must be between 41-71. Non-digit characters are removed before validation.

  • Parameters:

    • kennitala: The kennitala string to validate.
  • Returns: true if the kennitala is valid for a company, false otherwise.

isTemporaryKennitala(kennitala: string): boolean

Checks if the kennitala is a valid temporary ID.

  • Parameters:

    • kennitala: The kennitala string to validate.
  • Returns: true if the kennitala is a valid temporary ID, false otherwise.

sanitize(kennitala: string): string | undefined

Sanitizes the input by removing all non-digit characters.

  • Parameters:

    • kennitala: The kennitala string to sanitize.
  • Returns: The sanitized kennitala string if input is valid, undefined otherwise.

formatKennitala(kennitala: string, spacer?: boolean): string

Formats the kennitala by adding a - spacer between the 6th and 7th digits.

  • Parameters:

    • kennitala: The kennitala string to format.
    • spacer (optional): Includes a - spacer character by default.
  • Returns: The formatted kennitala string.

info(kennitala: string): KennitalaInfo | undefined

Returns an object containing information about the kennitala.

  • Parameters:

    • kennitala: The kennitala string to analyze.
  • Returns: An object of type KennitalaInfo if valid, undefined otherwise.

KennitalaInfo Type Definition:

interface KennitalaInfo {
  kt: string; // The sanitized kennitala
  valid: boolean; // Whether the kennitala is valid
  type: "person" | "company" | "temporary" | "unknown"; // Type of kennitala
  age?: number; // Age calculated from the birthday (if applicable)
  birthday?: Date; // Date object representing the birthday (if applicable)
  birthdayReadable?: string; // Human-readable date string (if applicable)
}

generatePerson(date?: Date): string | undefined

Generates a valid kennitala for a person. Optionally accepts a Date object to specify the birth date.

  • Parameters:

    • date (optional): The birth date to use for generating the kennitala.
  • Returns: A valid kennitala string if generation is successful, undefined otherwise.

generateCompany(date?: Date): string | undefined

Generates a valid kennitala for a company. Optionally accepts a Date object to specify the registration date.

  • Parameters:

    • date (optional): The date to use for generating the company kennitala.
  • Returns: A valid kennitala string if generation is successful, undefined otherwise.

Testing

The library uses Jest for testing. To run the tests, use:

npm test

Building

To build the project, run:

npm run build

This will compile the TypeScript code and place the output in the dist/ folder.