Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kennitala

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kennitala - npm Package Compare versions

Comparing version 2.0.0-beta.3 to 2.0.0-beta.4

dist/esm/index.js

48

package.json
{
"name": "kennitala",
"version": "2.0.0-beta.3",
"version": "2.0.0-beta.4",
"description": "Icelandic social security number (kennitölur) utilities for servers and clients",
"main": "dist/kennitala.min.js",
"types": "./dist/kennitala.min.d.ts",
"author": "Hermann Björgvin Haraldsson <hermann@hermann.is>",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
"files": [
"dist"
],
"exports": {
".": {
"require": "./dist/umd/index.js",
"import": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts"
}
},
"type": "module",
"license": "MIT",

@@ -14,11 +26,29 @@ "repository": {

"scripts": {
"test": "node_modules/.bin/mocha",
"uglify": "node_modules/.bin/uglifyjs kennitala.js --compress --mangle --source-map dist/kennitala.min.js.map --source-map-url kennitala.min.js.map --output dist/kennitala.min.js",
"dist": "npm test && npm run uglify"
"build": "rimraf dist && rollup -c",
"lint": "eslint 'src/**/*.{ts,tsx}' 'tests/**/*.{ts,tsx}'",
"test": "jest",
"prepublishOnly": "npm run build"
},
"keywords": [
"kennitala",
"iceland",
"national-id",
"validation",
"typescript"
],
"devDependencies": {
"chai": "*",
"mocha": "*",
"uglify-js": "2.8.29"
"@rollup/plugin-commonjs": "^28.0.1",
"@rollup/plugin-node-resolve": "^15.3.0",
"@types/jest": "^29.5.14",
"@typescript-eslint/eslint-plugin": "^8.14.0",
"@typescript-eslint/parser": "^8.14.0",
"eslint": "^9.14.0",
"jest": "^29.7.0",
"rimraf": "^6.0.1",
"rollup": "^4.25.0",
"rollup-plugin-typescript2": "^0.36.0",
"ts-jest": "^29.2.5",
"typescript": "^5.6.3",
"typescript-eslint": "^8.14.0"
}
}

251

README.md

@@ -1,8 +0,6 @@

Kennitala
# Kennitala
---
Icelandic national ID (kennitölur) utilities for servers and clients. Now with TypeScript support!
[![Build Status](https://travis-ci.org/HermannBjorgvin/Kennitala.svg?branch=master)](https://travis-ci.org/HermannBjorgvin/Kennitala)
[![Build Status](https://github.com/HermannBjorgvin/Kennitala/actions/workflows/ci.yml/badge.svg)](https://github.com/HermannBjorgvin/Kennitala/actions)
[![npm](https://img.shields.io/npm/v/kennitala.svg)](https://www.npmjs.com/package/kennitala)

@@ -18,135 +16,208 @@ [![npm](https://img.shields.io/npm/dm/kennitala.svg)](https://www.npmjs.com/package/kennitala)

## Version 2.0.0-beta.4
A beta version of v2.0.0 is available and includes support for the new temporary IDs.
**Breaking Change:** The `clean` method is now called `sanitize` to clarify its purpose. It's recommended to sanitize an ID before storing it since this library validates IDs both with and without `-` spacers.
To try the beta version:
```bash
npm install kennitala@2.0.0-beta.4
```
Please report any issues you encounter. Note that the code may still undergo refactoring.
### Examples
```Javascript
const kennitala = require('kennitala');
```javascript
import { isValid } from "kennitala";
// Check if kennitala is valid for either a company or individual
kennitala.isValid('3108962099'); // returns True
kennitala.isValid('8281249124'); // returns False
// 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
### Heads Up for Storing in Databases
This library will validate kennitölur with `-` spacers and no spacer, so remember to sanitize your kennitala before storing in a database!
This library validates kennitölur both with `-` spacers and without, so remember to **sanitize** your kennitala before storing it in a database!
This can be done with the included `.sanitize` function like so:
You can use the included `sanitize` function:
```Javascript
const kennitala = require('kennitala');
```javascript
import { sanitize } from "kennitala";
kennitala.sanitize('310896-2099');
const sanitizedKennitala = sanitize("310896-2099");
// returns '3108962099'
```
### More examples
### More Examples
```Javascript
const kennitala = require('kennitala');
```javascript
import {
isValid,
isPersonKennitala,
isCompanyKennitala,
formatKennitala,
info,
generatePerson,
generateCompany,
} from 'kennitala';
// the .info() method returns an object with useful information
kennitala.info('3108962099');
// returns
// Get detailed information about a kennitala
const kennitalaInfo = info('3108962099');
// kennitalaInfo contains:
{
kt: '3108962099',
valid: true,
type: 'person',
birthday: 1996-08-31T00:00:00.000Z,
birthdayReadable: 'Sat Aug 31 1996',
age: 27
kt: '3108962099',
valid: true,
type: 'person',
birthday: new Date('1996-08-31T00:00:00.000Z'),
birthdayReadable: 'Sat Aug 31 1996',
age: 27,
}
// Check if kennitala is valid for a person (returns false for companies)
kennitala.isPerson('3108962099'); // returns True
kennitala.isPerson('601010-0890'); // returns False because of invalid date
kennitala.isPerson(3108962099); // returns False because of numeric type
kennitala.isPerson('31^_^08!!96LOL20T_T99'); // returns False because... well, just no
// 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)
// Checks if kennitala is valid for a company (returns false for persons)
kennitala.isCompany('6010100890'); // True
kennitala.isCompany('601010-0890'); // True
kennitala.isCompany('3108962099'); // False
// 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
// the .format() method formats a kennitala and adds a traditional - spacer
// takes an optional parameter for the spacer between the 6th and 7th digit
// defaults to '-' but can be customized with an optional parameter
// Format a kennitala by adding a traditional '-' spacer
// You can customize the spacer character (defaults to '-')
formatKennitala('31089620'); // returns '310896-20'
formatKennitala('3108962099', ''); // returns '3108962099'
```
// Great for using with getters/setters in forms where you want to show the traditional format to users
### API Documentation
kennitala.format('31089620');
// returns '310896-20'
Below is the API based on the type definitions from the refactored TypeScript library.
kennitala.format('3108962099', '');
// returns '3108962099'
#### `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.
### API documentation
- **Parameters:**
kennitala.isValid(string, ?options: { allowTestDataset: false });
returns boolean
- `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.
Checks if kennitala checksum is correct for either a person or company
If passed a string with non-digit characters included it removes them before validating
- **Returns:** `true` if the kennitala is valid, `false` otherwise.
Allows passing optional options object to enable the test dataset
https://www.skra.is/um-okkur/frettir/frett/2020/10/13/Ny-utgafa-af-gervigognum-thjodskrar/
#### `isPersonKennitala(kennitala: string, options?: { allowTestKennitala?: boolean }): boolean`
kennitala.info(string);
returns object with relevant information about this kennitala
{
kt: char(10),
valid: boolean,
type: enum("company", "person")
age: int,
birthday: Date object,
birthdayReadable: Human readable Date String
}
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.
kennitala.isPerson(string, ?options: { allowTestDataset: false });
returns boolean
- **Parameters:**
Checks if kennitala checksum is correct and if day of birth is between 1-31
If passed a string with non-digit characters included it removes them before validating
- `kennitala`: The kennitala string to validate.
- `options` (optional): Same as in `isValid`.
Allows passing optional options object to enable the test dataset
https://www.skra.is/um-okkur/frettir/frett/2020/10/13/Ny-utgafa-af-gervigognum-thjodskrar/
- **Returns:** `true` if the kennitala is valid for a person, `false` otherwise.
kennitala.isCompany(string);
returns boolean
#### `isCompanyKennitala(kennitala: string): boolean`
Checks if kennitala checksum is correct and if day of birth is between 41-71
If passed a string with non-digit characters included it removes them before validating
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.
kennitala.format(string, ?[string]);
returns string
- **Parameters:**
Ensures datatype is string, then matches and removes all non-digit characters
and adds a traditional '-' spacer between 6th and 7th digit. This can be customized
with an optional 2nd parameter.
- `kennitala`: The kennitala string to validate.
kennitala.sanitize(string);
returns string or undefined
- **Returns:** `true` if the kennitala is valid for a company, `false` otherwise.
Ensures datatype is string, then matches and removes all non-digit characters.
#### `isTemporaryKennitala(kennitala: string): boolean`
Does not ensure a valid kennitala, only used for sanitizing input.
Checks if the kennitala is a valid temporary ID.
kennitala.generatePerson([date]);
returns string
- **Parameters:**
Takes Date object as a parameter and returns a new kennitala for a person
- `kennitala`: The kennitala string to validate.
kennitala.generateCompany([date]);
returns string
- **Returns:** `true` if the kennitala is a valid temporary ID, `false` otherwise.
Takes Date object as a parameter and returns a new kennitala for a company
#### `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?: string): string`
Formats the kennitala by adding a spacer between the 6th and 7th digits. The spacer defaults to `'-'`.
- **Parameters:**
- `kennitala`: The kennitala string to format.
- `spacer` (optional): The spacer character to use.
- **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:**
```typescript
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
Uses [Mocha](https://mochajs.org/) for testing. In order to execute the tests, you need to run `npm install -g mocha` first. Once you've done that
you can open up a command line and point it to the root directory of the project. From there you should be able to type either `npm test` or simply `mocha` to run the tests.
The library uses [Jest](https://jestjs.io/) for testing. To run the tests, use:
```bash
npm test
```
### Building
To build the project, you can type `npm run dist`, which minifies the script and generates a source map, and places both in the `dist/` folder.
To build the project, run:
```bash
npm run build
```
This will compile the TypeScript code and place the output in the `dist/` folder.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc