Skip to content

A TypeScript class for validating PESEL numbers / klasa TypeScript do sprawdzania poprawności numeru PESEL

License

Notifications You must be signed in to change notification settings

barabasz/typescript-pesel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PESEL

A very simple TypeScript class for validating PESEL numbers - national identification numbers used in Poland since 1979. This class has no external dependencies.

The PESEL number supports birthdays from 1800-01-01 to 2299-12-31 (YYYY-MM-DD), contains a serial number, information on the gender of the born person and a checksum.

🇵🇱 Zobacz readme w języku polskim.

Usage

import {Pesel} from './Pesel.ts';
let language = 'en';      // supported languages: 'en' (English) and 'pl' (Polish)

// valid PESEL
let pesel = '29511300014';
const p1 = new Pesel.Check(pesel, language);

console.log(p1.valid());   // true
console.log(p1.verdict()); // 'the PESEL number is valid ✅....'
console.log(p1.info());    // 'It is a man born on Sunday, November 13, 2129'
console.log(p1.date());    // '2129-11-13'
console.log(p1.error());   // null

// invalid PESEL
pesel = '29513300014';
const p2 = new Pesel.Check(pesel, language);

console.log(p2.valid());   // false
console.log(p1.verdict()); // 'the PESEL number is invalid ⛔'
console.log(p2.info());    // null
console.log(p2.date());    // null
console.log(p2.error());   // 'incorrect date (2129-11-33)'

You can also:

console.log(p1.json());    // {get string as JSON with all properties}
p1.print();                // print all properties to the console

Methods

The Pesel class has the following public methods:

  • date() - string in 'YYYY-MM-DD' format on valid PESEL or null otherwise,
  • error() - null on valid PESEL, string with explanation of invalidity otherwise,
  • verdict() - string: human-readable message whether the PESEL is valid,
  • info() - string: human-readable message on person with this PESEL,
  • json() - string as JSON with all properties on valid PESEL or null otherwise,
  • print() - void print all properties to the console,
  • valid() - boolean: whether the PESEL is valid.

Properties

The PeselProperties class has the following properties:

Properties always present

  • icon - string: ✅ or ⛔,
  • isValid - boolean: whether the PESEL is valid,
  • lang - string: two-letter language,
  • value - string: trimmed input value,
  • verdict - string: info on whether the PESEL is valid.

Properties additionally present on invalid PESELs only

  • error - string: Validation error cause

Properties additionally present on valid PESELs only

  • century - integer: first two digits from year,
  • checksum - integer: checksum (last digit),
  • dateLong - string: birthdate in long date format,
  • dateObj - Date object,
  • date - string: birthdate in YYYY-MM-DD format,
  • dayInt - integer: day from birthdate,
  • day - string: two-digits day from birthdate,
  • dowName - string: day of week from birthdate,
  • dow - integer: day of week from birthdate (0 = Sunday),
  • icon - string: ✅ or ⛔whether the PESEL is valid,
  • info - string: human-readable info on PESEL,
  • monthInt - integer: month from birthdate,
  • monthName - string: name of the month from birthdate,
  • month - , string: month from birthdate,
  • serial - string: serial number (including sex value),
  • sexName - string: sex (male/female),
  • sex - string: sex (male/female),
  • yearShort - string: last two digits from year,
  • year - integer: year from birthdate.

Properties printed to the console

Valid PESEL

let PeselProperties =  {
    century: 21,
    checksum: 4,
    date: '2129-11-13',
    dateLong: 'November 13, 2129',
    dateObj: '2129-11-13T00:00:00.000Z',
    day: '13',
    dayInt: 13,
    dow: 0,
    dowName: 'Sunday',
    icon: '✅',
    info: 'It is a man born on Sunday, November 13, 2129',
    isValid: true,
    lang: 'en',
    month: '11',
    monthInt: 11,
    monthName: 'November',
    reason: undefined,
    serial: '0001',
    sex: 'male',
    sexName: 'man',
    value: '29511300014',
    verdict: 'the PESEL number is correct',
    year: 2129,
    yearShort: '29'
}

Invalid PESEL

PeselProperties =  {
    icon: '⛔',
    isValid: false,
    lang: 'en',
    error: 'incorrect date (2129-11-33)',
    value: '29513300014',
    verdict: 'the PESEL number is invalid'
}

About

A TypeScript class for validating PESEL numbers / klasa TypeScript do sprawdzania poprawności numeru PESEL

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published