phone-number-generator-js
Version:
Generates a random phone number following the E.164 international standard
86 lines (85 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPhoneNumberValid = exports.generatePhoneNumbers = void 0;
var libphonenumber_js_1 = require("libphonenumber-js");
var class_validator_1 = require("class-validator");
var lodash_1 = require("lodash");
var countryPhoneData_js_1 = require("./countryPhoneData.js");
function generatePhoneNumber(config) {
for (var i = 0; i < 100; i++) {
var countryPhoneData = getCountryPhoneDataByConfig(config);
for (var j = 0; j < 1000; j++) {
var phoneNumber = getRandomPhoneNumber(countryPhoneData, config === null || config === void 0 ? void 0 : config.withoutCountryCode);
if (isPhoneNumberValid(phoneNumber, countryPhoneData, config === null || config === void 0 ? void 0 : config.withoutCountryCode)) {
return phoneNumber;
}
}
if (config) {
break;
}
}
throw new Error("Failed to generate phone number");
}
exports.default = generatePhoneNumber;
function generatePhoneNumbers(count, config) {
if (count <= 0) {
throw new Error("Count must be greater than 0");
}
if (count > 10000) {
throw new Error("Count exceeds maximum allowed (10000)");
}
var phoneNumbers = [];
for (var i = 0; i < count; i++) {
phoneNumbers.push(generatePhoneNumber(config));
}
return phoneNumbers;
}
exports.generatePhoneNumbers = generatePhoneNumbers;
function isPhoneNumberValid(phoneNumber, countryPhoneData, withoutCountryCode) {
if (withoutCountryCode) {
return countryPhoneData !== undefined && (0, libphonenumber_js_1.isValidNumberForRegion)(phoneNumber, countryPhoneData.alpha2);
}
if (countryPhoneData) {
var nationalNumber = phoneNumber.slice(1 + countryPhoneData.country_code.length);
if (!countryPhoneData.phone_number_lengths.includes(nationalNumber.length)) {
return false;
}
}
return (0, libphonenumber_js_1.isValidPhoneNumber)(phoneNumber) && (0, class_validator_1.isPhoneNumber)(phoneNumber);
}
exports.isPhoneNumberValid = isPhoneNumberValid;
function getRandomPhoneNumber(countryPhoneData, withoutCountryCode) {
var _a, _b;
if (withoutCountryCode === void 0) { withoutCountryCode = false; }
var randomMobileBeginWith = (_a = (0, lodash_1.sample)(countryPhoneData.mobile_begin_with)) !== null && _a !== void 0 ? _a : "";
var selectedLength = (_b = (0, lodash_1.sample)(countryPhoneData.phone_number_lengths)) !== null && _b !== void 0 ? _b : countryPhoneData.phone_number_lengths[0];
var suffixLength = selectedLength - randomMobileBeginWith.length;
if (suffixLength <= 0) {
return '';
}
var randomPhoneNumberSuffix = getRandomPhoneNumberSuffix(suffixLength);
return "" + (withoutCountryCode ? '' : '+' + countryPhoneData.country_code) + randomMobileBeginWith + randomPhoneNumberSuffix;
}
function getRandomPhoneNumberSuffix(phoneNumberLength) {
return "" + (0, lodash_1.random)(2, 9) + getRandomNumberInLength(phoneNumberLength - 1);
}
function getRandomNumberInLength(length) {
var min = Math.pow(10, length - 1);
var max = Math.pow(10, length) - 1;
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getCountryPhoneDataByConfig(config) {
var _a;
if (config === void 0) { config = {}; }
var countryPhoneData;
if (!config || !config.countryName) {
countryPhoneData = (0, lodash_1.sample)(countryPhoneData_js_1.countryPhoneDataArray);
}
else {
countryPhoneData =
(_a = countryPhoneData_js_1.countryPhoneDataArray.find(function (countryPhoneData) { return countryPhoneData.country_name === config.countryName; })) !== null && _a !== void 0 ? _a : (function () {
throw new Error("Invalid country name");
})();
}
return countryPhoneData;
}