UNPKG

phone-number-generator-js

Version:

Generates a random phone number following the E.164 international standard

80 lines (79 loc) 3.5 kB
import { isValidNumberForRegion, isValidPhoneNumber } from "libphonenumber-js"; import { isPhoneNumber } from 'class-validator'; import { random, sample } from "lodash"; import { countryPhoneDataArray } from "./countryPhoneData.js"; export default 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"); } export 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; } export function isPhoneNumberValid(phoneNumber, countryPhoneData, withoutCountryCode) { if (withoutCountryCode) { return countryPhoneData !== undefined && 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 isValidPhoneNumber(phoneNumber) && isPhoneNumber(phoneNumber); } function getRandomPhoneNumber(countryPhoneData, withoutCountryCode) { var _a, _b; if (withoutCountryCode === void 0) { withoutCountryCode = false; } var randomMobileBeginWith = (_a = sample(countryPhoneData.mobile_begin_with)) !== null && _a !== void 0 ? _a : ""; var selectedLength = (_b = 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 "" + 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 = sample(countryPhoneDataArray); } else { countryPhoneData = (_a = countryPhoneDataArray.find(function (countryPhoneData) { return countryPhoneData.country_name === config.countryName; })) !== null && _a !== void 0 ? _a : (function () { throw new Error("Invalid country name"); })(); } return countryPhoneData; }