UNPKG

@devmehq/phone-number-validator-js

Version:

Verify phone number, validate format, checking carrier name, geo and timezone infos.

124 lines (120 loc) 3.43 kB
'use strict'; var libphonenumberJs = require('libphonenumber-js'); var node_fs = require('node:fs'); var node_path = require('node:path'); var bson = require('bson'); var tinyLru = require('tiny-lru'); const DEFAULT_CACHE_SIZE = 100; let codeDataCache = tinyLru.lru(DEFAULT_CACHE_SIZE); function getCode(dataPath, nationalNumber) { if (!dataPath || !nationalNumber) { return null; } try { let data = codeDataCache.get(dataPath); if (!data) { const bData = node_fs.readFileSync(dataPath); data = bson.deserialize(bData); codeDataCache.set(dataPath, data); } let prefix = nationalNumber; while (prefix.length > 0) { const description = data[prefix]; if (description) { return description; } prefix = prefix.substring(0, prefix.length - 1); } } catch (err) { if (process.env.NODE_ENV !== "production") { console.error(`Error loading data from ${dataPath}:`, err); } } return null; } function getLocalizedData(resourceType, phonenumber, locale, fallbackLocale = "en") { var _a, _b; if (!phonenumber) { return null; } const nationalNumber = (_a = phonenumber.nationalNumber) == null ? void 0 : _a.toString(); const countryCallingCode = (_b = phonenumber.countryCallingCode) == null ? void 0 : _b.toString(); if (!nationalNumber || !countryCallingCode) { return null; } let dataPath = node_path.join( __dirname, "../resources/", resourceType, locale, `${countryCallingCode}.bson` ); const code = getCode(dataPath, nationalNumber); if (code) { return code; } if (locale !== fallbackLocale) { dataPath = node_path.join( __dirname, "../resources/", resourceType, fallbackLocale, `${countryCallingCode}.bson` ); return getCode(dataPath, nationalNumber); } return null; } function geocoder(phonenumber, locale = "en") { return getLocalizedData("geocodes", phonenumber, locale, "en"); } function carrier(phonenumber, locale = "en") { return getLocalizedData("carrier", phonenumber, locale, "en"); } function timezones(phonenumber) { if (!phonenumber || !phonenumber.number) { return null; } let nr = phonenumber.number.toString(); if (!nr) { return null; } nr = nr.replace(/^\+/, ""); const dataPath = node_path.join(__dirname, "../resources/timezones.bson"); const zones = getCode(dataPath, nr); if (typeof zones === "string" && zones.length > 0) { return zones.split("&").filter((zone) => zone.length > 0); } return null; } function clearCache() { codeDataCache.clear(); } function getCacheSize() { return codeDataCache.size; } function setCacheSize(size) { const oldCache = codeDataCache; codeDataCache = tinyLru.lru(size); const entries = oldCache.entries(); entries.reverse(); for (const [key, value] of entries) { if (codeDataCache.size < size) { codeDataCache.set(key, value); } else { break; } } } exports.carrier = carrier; exports.clearCache = clearCache; exports.geocoder = geocoder; exports.getCacheSize = getCacheSize; exports.setCacheSize = setCacheSize; exports.timezones = timezones; Object.keys(libphonenumberJs).forEach(function (k) { if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, { enumerable: true, get: function () { return libphonenumberJs[k]; } }); });