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.
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
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.
The PeselProperties class has the following properties:
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.
error
- string: Validation error cause
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 inYYYY-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.
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'
}
PeselProperties = {
icon: '⛔',
isValid: false,
lang: 'en',
error: 'incorrect date (2129-11-33)',
value: '29513300014',
verdict: 'the PESEL number is invalid'
}