@devmehq/phone-number-validator-js
Version:
Verify phone number, validate format, checking carrier name, geo and timezone infos.
120 lines (117 loc) • 4.47 kB
JavaScript
export * from 'libphonenumber-js';
import { readFileSync } from 'fs';
import { deserialize } from 'bson';
import { join } from 'path';
var codeData = {};
/**
* Maps the dataPath and prefix to geocode, carrier, timezones or null if this info could not be extracted
*
* **Note:** Timezones are returned as single string joined with `&`
*
* @param dataPath Path of the metadata bson file to use
* @param nationalNumber The national (significant) number without whitespaces e.g. `2133734253`
*/
function getCode(dataPath, nationalNumber) {
try {
//
if (!codeData[dataPath]) {
var bData = readFileSync(dataPath);
codeData[dataPath] = deserialize(bData);
}
//
var data = codeData[dataPath];
var prefix = nationalNumber;
// Find the longest match
while (prefix.length > 0) {
var description = data[prefix];
if (description) {
return description;
}
// Remove a character from the end
prefix = prefix.substring(0, prefix.length - 1);
}
}
catch (err) {
// console.log('Could not parse bson', err)
}
return null;
}
/**
* Provides geographical information related to the phone number
*
* @param phonenumber The phone number
* @param locale The preferred locale to use (falls back to `en` if there are no localized carrier infos for the given locale)
*/
function geocoder(phonenumber, locale) {
if (locale === void 0) { locale = 'en'; }
var nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString();
var countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString();
if (!nationalNumber || !countryCallingCode) {
return null;
}
var dataPath = join(__dirname, '../resources/geocodes/', locale, "".concat(countryCallingCode, ".bson"));
// const code = await getCode(dataPath, prefix)
var code = getCode(dataPath, nationalNumber);
if (code) {
return code;
}
if (locale !== 'en') {
// Try fallback to english
dataPath = join(__dirname, '../resources/geocodes/', 'en', "".concat(countryCallingCode, ".bson"));
// return await getCode(dataPath, prefix)
return getCode(dataPath, nationalNumber);
}
return null;
}
/**
* Maps the phone number to the original carrier
*
* **Note:** This method cannot provide data about the current carrier of the phone number,
* only the original carrier who is assigned to the corresponding range.
* @see https://github.com/google/libphonenumber#mapping-phone-numbers-to-original-carriers
*
* @param phonenumber The phone number
* @param locale The preferred locale to use (falls back to `en` if there are no localized carrier infos for the given locale)
*/
function carrier(phonenumber, locale) {
if (locale === void 0) { locale = 'en'; }
if (!phonenumber) {
return null;
}
var nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString();
var countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString();
if (!nationalNumber || !countryCallingCode) {
return null;
}
var dataPath = join(__dirname, '../resources/carrier/', locale, "".concat(countryCallingCode, ".bson"));
// const code = await getCode(dataPath, prefix)
var code = getCode(dataPath, nationalNumber);
if (code) {
return code;
}
if (locale !== 'en') {
// Try fallback to english
dataPath = join(__dirname, '../resources/carrier/', 'en', "".concat(countryCallingCode, ".bson"));
// return await getCode(dataPath, prefix)
return getCode(dataPath, nationalNumber);
}
return null;
}
/**
* Provides all timezones related to the phone number
* @param phonenumber The phone number
*/
function timezones(phonenumber) {
var nr = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.number.toString();
if (!nr) {
return null;
}
nr = nr.replace(/^\+/, '');
var dataPath = join(__dirname, '../resources/timezones.bson');
var zones = getCode(dataPath, nr);
if (typeof zones === 'string') {
return zones.split('&');
}
return null;
}
export { carrier, geocoder, timezones };