UNPKG

polish-validators

Version:

A set of validator functions that check common polish numbers.

86 lines (85 loc) 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const pesel_1 = require("./pesel"); describe('PESEL', () => { it('should return true for a valid PESEL number', () => { expect((0, pesel_1.isPeselValid)('44051401359')).toBe(true); }); it('should return false for a PESEL with incorrect length', () => { expect((0, pesel_1.isPeselValid)('1234567890')).toBe(false); // 10 digits expect((0, pesel_1.isPeselValid)('123456789012')).toBe(false); // 12 digits }); it('should return false for a PESEL containing non-numeric characters', () => { expect((0, pesel_1.isPeselValid)('4405140135A')).toBe(false); expect((0, pesel_1.isPeselValid)('1234567A901')).toBe(false); }); it('should return false for an all-zero PESEL', () => { expect((0, pesel_1.isPeselValid)('00000000000')).toBe(false); }); it('should return false if control number is incorrect', () => { expect((0, pesel_1.isPeselValid)('44051401358')).toBe(false); // wrong control number }); it('should return false for a PESEL with an invalid date', () => { expect((0, pesel_1.isPeselValid)('44043201359')).toBe(false); // invalid day (32) expect((0, pesel_1.isPeselValid)('44990501359')).toBe(false); // invalid month (99) }); it('should return true for a PESEL with a valid date in the 1800s', () => { expect((0, pesel_1.isPeselValid)('80123101354')).toBe(true); // PESEL with 23 Sept 1880 }); it('should return true for a PESEL with a valid leap year date', () => { expect((0, pesel_1.isPeselValid)('00222901352')).toBe(true); // PESEL with 29 Feb 2000 (leap year) }); it('should return false for a PESEL with an invalid leap year date', () => { expect((0, pesel_1.isPeselValid)('01222901359')).toBe(false); // PESEL with 29 Feb 2001 (non-leap year) }); it('should return false for an empty string', () => { expect((0, pesel_1.isPeselValid)('')).toBe(false); // empty string }); }); describe('getBirthdateFromPesel', () => { it('should extract the correct birthdate from a valid PESEL - years 1900-2000', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('44051401359'); expect(birthdate?.getFullYear()).toBe(1944); expect(birthdate?.getMonth()).toBe(4); // 0-indexed expect(birthdate?.getDate()).toBe(14); }); it('should extract the correct birthdate from a valid PESEL - years 2000-2100', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('03251401352'); expect(birthdate?.getFullYear()).toBe(2003); expect(birthdate?.getMonth()).toBe(4); // 0-indexed expect(birthdate?.getDate()).toBe(14); }); it('should extract the correct birthdate from a valid PESEL - years 2100-2200', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('03451401358'); expect(birthdate?.getFullYear()).toBe(2103); expect(birthdate?.getMonth()).toBe(4); // 0-indexed expect(birthdate?.getDate()).toBe(14); }); it('should extract the correct birthdate from a valid PESEL - years 2200-2300', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('03651401354'); expect(birthdate?.getFullYear()).toBe(2203); expect(birthdate?.getMonth()).toBe(4); // 0-indexed expect(birthdate?.getDate()).toBe(14); }); it('should extract the correct birthdate from a valid PESEL - years 1800-1900', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('99851401353'); expect(birthdate?.getFullYear()).toBe(1899); expect(birthdate?.getMonth()).toBe(4); // 0-indexed expect(birthdate?.getDate()).toBe(14); }); it('should throw an error for an invalid PESEL', () => { const birthdate = (0, pesel_1.getBirthdateFromPesel)('00000000000'); expect(birthdate).toBe(null); }); }); describe('extractSexFromPesel', () => { it('should extract "male" for a PESEL with an odd tenth digit', () => { expect((0, pesel_1.extractSexFromPesel)('44051401359')).toBe(pesel_1.PeselSex.Male); }); it('should extract "female" for a PESEL with an even tenth digit', () => { expect((0, pesel_1.extractSexFromPesel)('44051401366')).toBe(pesel_1.PeselSex.Female); }); it('should return null for an invalid PESEL', () => { expect((0, pesel_1.extractSexFromPesel)('00000000000')).toBe(null); }); });