sri-lanka-nic@sri-lanka/nic

Types

All types are exported from the package and can be imported directly:

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

Enums

NICType

The NIC format type:

enum NICType {
  OLD = "OLD", // 9 digits + V/X
  NEW = "NEW", // 12 digits
}

Gender

enum Gender {
  MALE = "MALE",
  FEMALE = "FEMALE",
}

Interfaces

NICInstance

The object returned by NIC.parse():

interface NICInstance {
  value: string; // Sanitized NIC string
  type: NICType; // "NEW" or "OLD"
  gender: Gender; // "MALE" or "FEMALE"
  birthday: { year: number; month: number; day: number };
  age: number; // Current age
  parts: RawNICParts; // Raw string slices
  config: ResolvedNICConfig; // Resolved validation boundaries
  convert(options?: { letter?: "V" | "X" }): string;
}

RawNICParts

The raw string slices from the NIC:

interface RawNICParts {
  year: string; // "2000" or "1990"
  days: string; // "015" or "140"
  serial: string; // "0123" or "456"
  checkdigit: string; // "4" or "7"
  letter: string | null; // "V", "X", or null
}

Birthday

interface Birthday {
  year: number;
  month: number; // 1-indexed (1 = January, 12 = December)
  day: number;
}

NICConfig

Optional overrides for the default validation boundaries:

interface NICConfig {
  minimumAge?: number;
  maximumAge?: number;
  minimumBirthYear?: number;
  maximumBirthYear?: number;
}

NICValidatorConfig

Options for extending validation with custom logic and format restrictions:

interface NICValidatorConfig {
  onlyNew?: boolean;
  onlyOld?: boolean;
  check?: (nic: NICInstance, NICError: typeof NICError) => void;
}

NICOptions

Combined type used by parse(), validate(), and valid():

type NICOptions = NICConfig & NICValidatorConfig;

ResolvedNICConfig

The fully resolved validation boundaries (all fields required and readonly):

interface ResolvedNICConfig {
  readonly minimumAge: number;
  readonly maximumAge: number;
  readonly minimumBirthYear: number;
  readonly maximumBirthYear: number;
}

NICAPI

The shape of the main NIC object:

interface NICAPI {
  parse(nic: string, options?: NICOptions): NICInstance;
  validate(nic: string, options?: NICOptions): void;
  valid(nic: string, options?: NICOptions): boolean;
  sanitize(nic: string): string;
  getType(nic: string): NICType;
  defaultConfig: {
    new: ResolvedNICConfig;
    old: ResolvedNICConfig;
  };
  builder: {
    new: (options?: NICConfig) => NewNICBuilder;
    old: (options?: NICConfig) => OldNICBuilder;
  };
  random(): string;
}