libphonenumber-geo-carrier
Advanced tools
Comparing version 1.2.4 to 1.2.5
import { PhoneNumber } from 'libphonenumber-js'; | ||
import { GeocoderLocale, CarrierLocale } from './locales'; | ||
/** | ||
* 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) | ||
*/ | ||
export declare const geocoder: (phonenumber: PhoneNumber | undefined, locale?: GeocoderLocale) => Promise<string | 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) | ||
*/ | ||
export declare const carrier: (phonenumber: PhoneNumber | undefined, locale?: CarrierLocale) => Promise<string | null>; | ||
/** | ||
* Provides all timezones related to the phone number | ||
* @param phonenumber The phone number | ||
*/ | ||
export declare const timezones: (phonenumber: PhoneNumber | undefined) => Promise<string[] | null>; |
@@ -61,7 +61,12 @@ import fs from 'fs'; | ||
var readFile = promisify(fs.readFile); | ||
var getPrefix = function (phonenumber) { | ||
return phonenumber.formatInternational().replace(/^\+[0-9]+ ([0-9]+) .*/, '$1'); | ||
}; | ||
var getCode = function (dataPath, prefix) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bData, data, description; | ||
/** | ||
* Maps the dataPath and prefix to geocode, carrier or timezones of 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` | ||
*/ | ||
var getCode = function (dataPath, nationalNumber) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bData, data, prefix, description; | ||
return __generator(this, function (_a) { | ||
@@ -78,4 +83,13 @@ switch (_a.label) { | ||
data = deserialize(bData); | ||
description = data[prefix]; | ||
return [2 /*return*/, description]; | ||
prefix = nationalNumber; | ||
// Find the longest match | ||
while (prefix.length > 0) { | ||
description = data[prefix]; | ||
if (description) { | ||
return [2 /*return*/, description]; | ||
} | ||
// Remove a character from the end | ||
prefix = prefix.substring(0, prefix.length - 1); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
@@ -88,16 +102,22 @@ _a.sent(); | ||
}); }; | ||
/** | ||
* 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) | ||
*/ | ||
var geocoder = function (phonenumber, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, prefix, dataPath, code; | ||
var nationalNumber, countryCallingCode, dataPath, code; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!phonenumber) { | ||
nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString(); | ||
countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString(); | ||
if (!nationalNumber || !countryCallingCode) { | ||
return [2 /*return*/, null]; | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
prefix = getPrefix(phonenumber); | ||
dataPath = path.join(__dirname, '../resources/geocodes/', locale, countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 1: | ||
@@ -111,4 +131,6 @@ code = _a.sent(); | ||
dataPath = path.join(__dirname, '../resources/geocodes/', 'en', countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 2: | ||
// return await getCode(dataPath, prefix) | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/, null]; | ||
@@ -119,6 +141,16 @@ } | ||
}; | ||
/** | ||
* 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) | ||
*/ | ||
var carrier = function (phonenumber, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, prefix, dataPath, code; | ||
var nationalNumber, countryCallingCode, dataPath, code; | ||
return __generator(this, function (_a) { | ||
@@ -130,6 +162,9 @@ switch (_a.label) { | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
prefix = getPrefix(phonenumber); | ||
nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString(); | ||
countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString(); | ||
if (!nationalNumber || !countryCallingCode) { | ||
return [2 /*return*/, null]; | ||
} | ||
dataPath = path.join(__dirname, '../resources/carrier/', locale, countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 1: | ||
@@ -143,4 +178,6 @@ code = _a.sent(); | ||
dataPath = path.join(__dirname, '../resources/carrier/', 'en', countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 2: | ||
// return await getCode(dataPath, prefix) | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/, null]; | ||
@@ -151,13 +188,18 @@ } | ||
}; | ||
/** | ||
* Provides all timezones related to the phone number | ||
* @param phonenumber The phone number | ||
*/ | ||
var timezones = function (phonenumber) { return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, dataPath, zones; | ||
var nr, dataPath, zones; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!phonenumber) { | ||
nr = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.number.toString(); | ||
if (!nr) { | ||
return [2 /*return*/, null]; | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
nr = nr.replace(/^\+/, ''); | ||
dataPath = path.join(__dirname, '../resources/timezones.bson'); | ||
return [4 /*yield*/, getCode(dataPath, countryCallingCode)]; | ||
return [4 /*yield*/, getCode(dataPath, nr)]; | ||
case 1: | ||
@@ -164,0 +206,0 @@ zones = _a.sent(); |
@@ -70,7 +70,12 @@ 'use strict'; | ||
var readFile = util.promisify(fs__default['default'].readFile); | ||
var getPrefix = function (phonenumber) { | ||
return phonenumber.formatInternational().replace(/^\+[0-9]+ ([0-9]+) .*/, '$1'); | ||
}; | ||
var getCode = function (dataPath, prefix) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bData, data, description; | ||
/** | ||
* Maps the dataPath and prefix to geocode, carrier or timezones of 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` | ||
*/ | ||
var getCode = function (dataPath, nationalNumber) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bData, data, prefix, description; | ||
return __generator(this, function (_a) { | ||
@@ -87,4 +92,13 @@ switch (_a.label) { | ||
data = bson.deserialize(bData); | ||
description = data[prefix]; | ||
return [2 /*return*/, description]; | ||
prefix = nationalNumber; | ||
// Find the longest match | ||
while (prefix.length > 0) { | ||
description = data[prefix]; | ||
if (description) { | ||
return [2 /*return*/, description]; | ||
} | ||
// Remove a character from the end | ||
prefix = prefix.substring(0, prefix.length - 1); | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
@@ -97,16 +111,22 @@ _a.sent(); | ||
}); }; | ||
/** | ||
* 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) | ||
*/ | ||
var geocoder = function (phonenumber, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, prefix, dataPath, code; | ||
var nationalNumber, countryCallingCode, dataPath, code; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!phonenumber) { | ||
nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString(); | ||
countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString(); | ||
if (!nationalNumber || !countryCallingCode) { | ||
return [2 /*return*/, null]; | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
prefix = getPrefix(phonenumber); | ||
dataPath = path__default['default'].join(__dirname, '../resources/geocodes/', locale, countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 1: | ||
@@ -120,4 +140,6 @@ code = _a.sent(); | ||
dataPath = path__default['default'].join(__dirname, '../resources/geocodes/', 'en', countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 2: | ||
// return await getCode(dataPath, prefix) | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/, null]; | ||
@@ -128,6 +150,16 @@ } | ||
}; | ||
/** | ||
* 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) | ||
*/ | ||
var carrier = function (phonenumber, locale) { | ||
if (locale === void 0) { locale = 'en'; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, prefix, dataPath, code; | ||
var nationalNumber, countryCallingCode, dataPath, code; | ||
return __generator(this, function (_a) { | ||
@@ -139,6 +171,9 @@ switch (_a.label) { | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
prefix = getPrefix(phonenumber); | ||
nationalNumber = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.nationalNumber.toString(); | ||
countryCallingCode = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.countryCallingCode.toString(); | ||
if (!nationalNumber || !countryCallingCode) { | ||
return [2 /*return*/, null]; | ||
} | ||
dataPath = path__default['default'].join(__dirname, '../resources/carrier/', locale, countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 1: | ||
@@ -152,4 +187,6 @@ code = _a.sent(); | ||
dataPath = path__default['default'].join(__dirname, '../resources/carrier/', 'en', countryCallingCode + ".bson"); | ||
return [4 /*yield*/, getCode(dataPath, prefix)]; | ||
case 2: return [2 /*return*/, _a.sent()]; | ||
return [4 /*yield*/, getCode(dataPath, nationalNumber)]; | ||
case 2: | ||
// return await getCode(dataPath, prefix) | ||
return [2 /*return*/, _a.sent()]; | ||
case 3: return [2 /*return*/, null]; | ||
@@ -160,13 +197,18 @@ } | ||
}; | ||
/** | ||
* Provides all timezones related to the phone number | ||
* @param phonenumber The phone number | ||
*/ | ||
var timezones = function (phonenumber) { return __awaiter(void 0, void 0, void 0, function () { | ||
var countryCallingCode, dataPath, zones; | ||
var nr, dataPath, zones; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!phonenumber) { | ||
nr = phonenumber === null || phonenumber === void 0 ? void 0 : phonenumber.number.toString(); | ||
if (!nr) { | ||
return [2 /*return*/, null]; | ||
} | ||
countryCallingCode = phonenumber.countryCallingCode; | ||
nr = nr.replace(/^\+/, ''); | ||
dataPath = path__default['default'].join(__dirname, '../resources/timezones.bson'); | ||
return [4 /*yield*/, getCode(dataPath, countryCallingCode)]; | ||
return [4 /*yield*/, getCode(dataPath, nr)]; | ||
case 1: | ||
@@ -173,0 +215,0 @@ zones = _a.sent(); |
{ | ||
"name": "libphonenumber-geo-carrier", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "This library includes the geocoding, carrier mapping and timezone mapping functionalities that are available in some of googles libphonenumber libraries but not in libphonenumber-js (a port of libphonenumber).", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13641597
434