Kennitala
Icelandic national ID (kennitölur) utilities for servers and clients. Now with TypeScript support!
Install with npm:
npm install kennitala
Examples
import { isValid } from "kennitala";
isValid("3108962099");
isValid("8281249124");
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");
More Examples
import {
isValid,
isPersonKennitala,
isCompanyKennitala,
formatKennitala,
info,
generatePerson,
generateCompany,
} from 'kennitala';
const kennitalaInfo = info('3108962099');
{
kt: '3108962099',
valid: true,
type: 'person',
birthday: new Date('1996-08-31T00:00:00.000Z'),
birthdayReadable: 'Sat Aug 31 1996',
age: 27,
}
isPersonKennitala('3108962099');
isPersonKennitala('601010-0890');
isPersonKennitala(3108962099);
isPersonKennitala('31^_^08!!96LOL20T_T99');
isCompanyKennitala('6010100890');
isCompanyKennitala('601010-0890');
isCompanyKennitala('3108962099');
formatKennitala('31089620');
formatKennitala('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.
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.
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.
isTemporaryKennitala(kennitala: string): boolean
Checks if the kennitala is a valid temporary ID.
sanitize(kennitala: string): string | undefined
Sanitizes the input by removing all non-digit characters.
formatKennitala(kennitala: string, spacer?: string): string
Formats the kennitala by adding a spacer between the 6th and 7th digits. The spacer defaults to '-'
.
info(kennitala: string): KennitalaInfo | undefined
Returns an object containing information about the kennitala.
KennitalaInfo
Type Definition:
interface KennitalaInfo {
kt: string;
valid: boolean;
type: "person" | "company" | "temporary" | "unknown";
age?: number;
birthday?: Date;
birthdayReadable?: string;
}
generatePerson(date?: Date): string | undefined
Generates a valid kennitala for a person. Optionally accepts a Date
object to specify the birth date.
generateCompany(date?: Date): string | undefined
Generates a valid kennitala for a company. Optionally accepts a Date
object to specify the registration date.
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.