random-names-hebrew
Version:
Geneare random names in the Hebrew language
95 lines (94 loc) • 5.03 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
exports.__esModule = true;
var etthArr = ['jewish', 'christian', 'muslims', 'druze'];
var ethnics = Object.keys(etthArr).reduce(function (p, key) {
var ethnic = etthArr[key];
p[ethnic] = {
firstName: {
male: require('./../../data/ethnic/firstName/male/' + ethnic + '.json'),
female: require('./../../data/ethnic/firstName/female/' + ethnic + '.json'),
unisex: require('./../../data/ethnic/firstName/unisex/' + ethnic + '.json')
},
lastNames: require('./../../data/ethnic/lastName/' + ethnic + '.json')
};
return p;
}, {});
var merged = {
lastName: etthArr.map(function (ethnic) { return ethnics[ethnic].lastNames; }).reduce(function (o, v) { return __spreadArray(__spreadArray([], o, true), v, true); }, []),
firstName: {
male: etthArr.map(function (ethnic) { return ethnics[ethnic].firstName.male; }).reduce(function (o, v) { return __spreadArray(__spreadArray([], o, true), v, true); }, []),
female: etthArr.map(function (ethnic) { return ethnics[ethnic].firstName.male; }).reduce(function (o, v) { return __spreadArray(__spreadArray([], o, true), v, true); }, []),
unisex: etthArr.map(function (ethnic) { return ethnics[ethnic].firstName.unisex; }).reduce(function (o, v) { return __spreadArray(__spreadArray([], o, true), v, true); }, [])
}
};
var mapGender = {
'male': 'male',
'female': 'female',
'boys': 'male',
'girls': 'female',
'unisex': 'unisex'
};
var create = function (options) {
options = options || {};
var lastName, firstName;
var gender = (options.gender && Object.keys(mapGender).indexOf(options.gender) != -1) ? mapGender[options.gender] : ['male', 'female', 'unisex'][Math.floor(Math.random() * ['male', 'female', 'unisex'].length)];
if (options.ethnic && etthArr.indexOf(options.ethnic) !== -1) {
lastName = ethnics[options.ethnic].lastNames[getRandom(ethnics[options.ethnic].lastNames.length)];
firstName = (function (names) { return names[getRandom(names.length)]; })(options.gender ?
ethnics[options.ethnic].firstName[options.gender] :
(function (ethnic) {
var d = ethnics[ethnic].firstName;
return Object.keys(d).reduce(function (p, c) { return p.concat(d[c]); }, []);
})(options.ethnic));
}
else {
lastName = merged.lastName[getRandom(merged.lastName.length)];
firstName = merged.firstName[gender][getRandom(merged.firstName[gender].length)];
}
var name = { firstName: firstName, lastName: lastName, fullName: "".concat(firstName, " ").concat(lastName), phone: null };
if (typeof options.phone != 'undefined' && options.phone) {
options.phone = (Array.isArray(options.phone)) ? options.phone : [];
var phone = generatePhone(options.phone);
switch (options.phoneFormat) {
case 'e164':
name.phone = phone.replace(/(\d{1})(\d{2})(\d{3})(\d{4})/, "+972$2$3$4");
break;
case 'object':
name.phone = { number: phone, formatted: phone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"), e164: phone.replace(/(\d{1})(\d{2})(\d{3})(\d{4})/, "+972$2$3$4") };
break;
case 'formatted':
name.phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
break;
default:
name.phone = phone;
break;
}
}
if (options.names && options.names.length && options.names.indexOf(name.fullName) != -1)
return create(options);
return name;
};
var areaCodes = ['050', '051', '052', '053', '054', '055', '058']; // https://he.wikipedia.org/wiki/%D7%A7%D7%99%D7%93%D7%95%D7%9E%D7%AA_%D7%98%D7%9C%D7%A4%D7%95%D7%9F_%D7%91%D7%99%D7%A9%D7%A8%D7%90%D7%9C#.D7.A7.D7.99.D7.93.D7.95.D7.9E.D7.95.D7.AA_.D7.91.D7.90.D7.96.D7.95.D7.A8_.D7.94.D7.97.D7.99.D7.95.D7.92_.D7.9C.D7.98.D7.9C.D7.A4.D7.95.D7.A0.D7.99.D7.9D_.D7.A0.D7.99.D7.99.D7.93.D7.99.D7.9D_05
var generatePhone = function (phones) {
phones = phones || [];
var randomAreaCode = areaCodes[Math.floor(Math.random() * areaCodes.length)];
var randomNumber = Math.floor(Math.pow(10, (10 - randomAreaCode.length) - 1) + Math.random() * 9 * Math.pow(10, (10 - randomAreaCode.length) - 1));
var randomPhone = "".concat(randomAreaCode).concat(randomNumber);
if (phones.indexOf(randomPhone) == -1)
return randomPhone;
return generatePhone(phones);
};
var getRandom = function (num) {
var rnd = Math.floor(Math.random() * num);
return rnd;
};
exports["default"] = create;