Skip to content

Instantly share code, notes, and snippets.

@ErgEnn
Created September 2, 2022 13:31
Show Gist options
  • Save ErgEnn/17ca8a0a75627e7ca9af4d7210c3d04b to your computer and use it in GitHub Desktop.
Save ErgEnn/17ca8a0a75627e7ca9af4d7210c3d04b to your computer and use it in GitHub Desktop.
Personal code generators
function getControlNumber(code) {
var multiplier1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1],
multiplier2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3],
mod,
total = 0;
for (var i = 0; i < 10; i++) {
total += code.charAt(i) * multiplier1[i];
}
mod = total % 11;
total = 0;
if (10 === mod) {
for (i = 0; i < 10; i++) {
total += code.charAt(i) * multiplier2[i];
}
mod = total % 11;
if (10 === mod) {
mod = 0;
}
}
return mod;
};
function generate(params) {
params = params || {};
var y, m, d,
gender = params.gender || ((Math.round(Math.random()) === 0) ? 'male' : 'female'),
personalId = '',
// Places of brith (Estonian Hospitals)
hospitals = [
'00', // Kuressaare Haigla (järjekorranumbrid 001 kuni 020)
'01', // Tartu Ülikooli Naistekliinik, Tartumaa, Tartu (011...019)
'02', // Ida-Tallinna Keskhaigla, Hiiumaa, Keila, Rapla haigla (021...220)
'22', // Ida-Viru Keskhaigla (Kohtla-Järve, endine Jõhvi) (221...270)
'27', // Maarjamõisa Kliinikum (Tartu), Jõgeva Haigla (271...370)
'37', // Narva Haigla (371...420)
'42', // Pärnu Haigla (421...470)
'47', // Pelgulinna Sünnitusmaja (Tallinn), Haapsalu haigla (471...490)
'49', // Järvamaa Haigla (Paide) (491...520)
'52', // Rakvere, Tapa haigla (521...570)
'57', // Valga Haigla (571...600)
'60', // Viljandi Haigla (601...650)
'65', // Lõuna-Eesti Haigla (Võru), Pälva Haigla (651...710?)
'70', // All other hospitals
'95' // Foreigners who are born in Estonia
];
if (params.birthYear) {
y = params.birthYear;
} else {
y = Math.round(Math.random() * 100 + 1900 + (new Date().getFullYear() - 2000));
}
if (params.birthMonth) {
m = params.birthMonth;
} else {
m = Math.floor(Math.random() * 12) + 1;
}
if (params.birthDay) {
d = params.birthDay;
} else {
var daysInMonth = new Date(y, m, 0).getDate();
d = Math.floor(Math.random() * daysInMonth) + 1;
}
// Set the gender
if (gender === 'male' && y >= 1800 && y <= 1899) {
personalId += '1';
} else if (gender === 'female' && y >= 1800 && y <= 1899) {
personalId += '2';
} else if (gender === 'male' && y >= 1900 && y <= 1999) {
personalId += '3';
} else if (gender === 'female' && y >= 1900 && y <= 1999) {
personalId += '4';
} else if (gender === 'male' && y >= 2000 && y <= 2099) {
personalId += '5';
} else if (gender === 'female' && y >= 2000 && y <= 2099) {
personalId += '6';
} else if (gender === 'male' && y >= 2100 && y <= 2199) {
personalId += '7';
} else if (gender === 'female' && y >= 2100 && y <= 2199) {
personalId += '8';
}
// Set the year
personalId += parseInt(y, 0).toString().substring(2, 4);
// Set the month
personalId += m.toString().length === 1 ? '0' + m : m;
// Set the day
personalId += d.toString().length === 1 ? '0' + d : d;
// Set the hospital
personalId += hospitals[Math.floor(Math.random() * hospitals.length)];
// Set the number of birth
personalId += Math.floor(Math.random() * 10);
// Set the control number
personalId += getControlNumber(personalId);
return personalId;
};
return generate()
function getRandomPersonalId(min, max) {
var currentDate = new Date();
var maxAgeInMilis = max * 365 * 24 * 60 * 60 * 1000;
var minAgeInMilis = min * 365 * 24 * 60 * 60 * 1000;
var randomMilis = Math.floor((Math.random() * (maxAgeInMilis-minAgeInMilis)) + minAgeInMilis);
var time = currentDate.setTime(currentDate.getTime() - randomMilis);
var date = new Date(time);
var day = padWithLeadingZeros(date.getDate());
var month = padWithLeadingZeros(date.getMonth() + 1);
var shortYear = date.getFullYear().toString().substring(2);
var firstPart = day + "" + month + "" + shortYear;
var checkDigit = "00";
while (checkDigit.toString().length == 2) {
pk = firstPart + getSecondPart();
checkDigit = getCheckDigit(pk);
};
pk = pk + checkDigit;
pk = pk.substring(0, 6) + "-" + pk.substring(6);
return pk;
};
function padWithLeadingZeros(value) {
value = value.toString();
return value.length == 1 ? "0".concat(value) : value;
};
function getCheckDigit(pk) {
var factor = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
pk = pk.split("").map(i => parseInt(i));
var reduce = pk.reduce((prev, cur, i) => cur * factor[i] + prev);
return (1101 - reduce) % 11;
};
function getSecondPart() {
return "1" + Math.floor((Math.random() * 899) + 100);
};
return getRandomPersonalId(21,45);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment