sri-lanka-nic@sri-lanka/nic

Getting Started

Installation

Install the package using your preferred package manager:

# npm
npm install @sri-lanka/nic
 
# pnpm
pnpm add @sri-lanka/nic
 
# yarn
yarn add @sri-lanka/nic

Your First Parse

Import NIC and call .parse() with any Sri Lankan NIC string:

import { NIC } from "@sri-lanka/nic";
 
const result = NIC.parse("200001501234");
 
console.log(result.type); // "NEW"
console.log(result.gender); // "MALE"
console.log(result.birthday); // { year: 2000, month: 1, day: 15 }
console.log(result.age); // 25 (depends on current date)

That's it. The library auto-detects the NIC format (old or new), validates it, and returns the parsed details.

What You Get Back

The .parse() method returns an object with these properties:

PropertyTypeDescription
valuestringThe sanitized NIC string
typeNICType"NEW" (12-digit) or "OLD" (9+letter)
genderGender"MALE" or "FEMALE"
birthdayBirthday{ year, month, day }
agenumberCurrent age (Sri Lanka time)
partsRawNICPartsRaw NIC string slices
configResolvedNICConfigThe resolved validation boundaries
convertfunctionConvert to the other format

Quick Validation

If you just want to check whether a NIC is valid:

// Throws NICError if invalid
NIC.validate("901404567V");
 
// Returns true/false, never throws
NIC.valid("901404567V"); // true
NIC.valid("invalid"); // false

TypeScript

The library is written in TypeScript. All types are exported:

import type { NICInstance, NICConfig, NICValidatorConfig, ResolvedNICConfig, NICOptions, NICType, Gender } from "@sri-lanka/nic";