Skip to content

Instantly share code, notes, and snippets.

@amadeuszblanik
Last active November 15, 2023 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amadeuszblanik/d76b029b2b16e44e507c555dbc8edaf5 to your computer and use it in GitHub Desktop.
Save amadeuszblanik/d76b029b2b16e44e507c555dbc8edaf5 to your computer and use it in GitHub Desktop.
Validate polish PESEL / NIP / REGON number — TypeScript — JavaScript ES6 — MIT
const checkSum = (digits: number[]) => {
const digit11 = digits[10];
digits.pop();
const times = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
const reducer = (accumulator, currentValue, index) => accumulator + (currentValue * times[index]);
let sum = digits.reduce(reducer);
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (sum == digit11) {
return true;
} else {
return false;
}
}
const peselToDigits = (value: string): number[] => value.split("").map(item => parseInt(item))
export const validatePesel = (value: string): boolean => {
const digits = peselToDigits(value);
if (digits.length !== 11 && digits.every(item => !isNaN(item))) {
return false;
}
return checkSum(digits);
}
export const getYearFromPesel = (value: string) => {
const digits = peselToDigits(value);
let year, month;
year = 10 * digits[0];
year += digits[1];
month = 10 * digits[2];
month += digits[3];
if (month > 80 && month < 93) {
year += 1800;
} else if (month > 0 && month < 13) {
year += 1900;
} else if (month > 20 && month < 33) {
year += 2000;
} else if (month > 40 && month < 53) {
year += 2100;
} else if (month > 60 && month < 73) {
year += 2200;
}
return year;
}
export const getMonthFromPesel = (value: string) => {
const digits = peselToDigits(value);
let month;
month = 10 * digits[2];
month += digits[3];
if (month > 80 && month < 93) {
month -= 80;
} else if (month > 20 && month < 33) {
month -= 20;
} else if (month > 40 && month < 53) {
month -= 40;
} else if (month > 60 && month < 73) {
month -= 60;
}
return month;
}
export const getDayFromPesel = (value: string) => {
const digits = peselToDigits(value);
let day;
day = 10 * digits[4];
day += digits[5];
return day;
}
export const getBirthDayFromPesel = (value: string) => `${getYearFromPesel(value)}-${getMonthFromPesel(value)}-${getDayFromPesel(value)}`
const peselValidation = (pesel: number) => {
const peselString = pesel.toFixed()
const weights = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7];
let sum = 0;
for (let index = 0; index < weights.length; index++) {
sum += parseInt(peselString.substring(index, index + 1), 10) * weights[index];
}
sum = sum % 10;
const response = (sum === parseInt(peselString.substring(10, 11), 10));
return response;
}
const peselDate = (pesel: number) => {
const peselString = pesel.toFixed()
let year: number = parseInt(peselString.substring(0, 2)),
month: number | string = parseInt(peselString.substring(2, 4), 10) - 1,
day: number = parseInt(peselString.substring(4, 6), 10)
if (month > 80) {
year = year + 1800
month = month - 80
} else if (month > 60) {
year = year + 2200
month = month - 60
} else if (month > 40) {
year = year + 2100
month = month - 40
} else if (month > 20) {
year = year + 2200
month = month - 80
} else {
year = year + 1900
}
month = month + 1;
if (month == 1) {
month = 'stycznia';
} else if (month == 2) {
month = 'lutego';
} else if (month == 3) {
month = 'marca';
} else if (month == 4) {
month = 'kwietnia';
} else if (month == 5) {
month = 'maja';
} else if (month == 6) {
month = 'czerwca';
} else if (month == 7) {
month = 'lipca';
} else if (month == 8) {
month = 'sierpnia';
} else if (month == 9) {
month = 'września';
} else if (month == 10) {
month = 'października';
} else if (month == 11) {
month = 'listopada';
} else if (month == 12) {
month = 'grudnia';
};
return `${day} ${month} ${year}`;
}
// This is randomly generated PESEL.
// Don't worry. I don't even born in 70's.
console.log(new PeselValidation(73072255843).getBornDate());
// This one is correct.
console.log(new PeselValidation(73072255843).validate());
// This one is not.
console.log(new PeselValidation(73072255842).validate());
const validatenip = (nip: string | number) => {
if (typeof nip === "number") {
nip = nip.toString();
} else {
nip = nip.replace(/-/g, "");
}
if (nip.length !== 10) {
return false;
}
const nipArray: number[] = nip.split("").map(value => parseInt(value));
const checkSum = (6 * nipArray[0] + 5 * nipArray[1] + 7 * nipArray[2] + 2 * nipArray[3] + 3 * nipArray[4] + 4 * nipArray[5] + 5 * nipArray[6] + 6 * nipArray[7] + 7 * nipArray[8])%11;
return nipArray[9] == checkSum;
}
const validateregon = (regon: string | number) => {
if (typeof regon === "number") {
regon = regon.toString();
} else {
regon = regon.replace(/-/g, "");
}
if (regon.length !== 9) {
return false;
}
const regonArray: number[] = regon.split("").map(value => parseInt(value));
let checkSum = (8 * regonArray[0] + 9 * regonArray[1] + 2 * regonArray[2] + 3 * regonArray[3] + 4 * regonArray[4] + 5 * regonArray[5] + 6 * regonArray[6] + 7 * regonArray[7])%11;
if (checkSum == 10) {
checkSum = 0;
}
return regonArray[8] == checkSum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment