Gathering detailed insights and metrics for fullstack-phone
Gathering detailed insights and metrics for fullstack-phone
Gathering detailed insights and metrics for fullstack-phone
Gathering detailed insights and metrics for fullstack-phone
npm install fullstack-phone
Typescript
Module System
Min. Node Version
Node Version
NPM Version
79.9
Supply Chain
99.2
Quality
94.1
Maintenance
100
Vulnerability
100
License
JavaScript (90.25%)
Shell (9.75%)
Total Downloads
49,407
Last Day
17
Last Week
95
Last Month
407
Last Year
7,295
91 Stars
478 Commits
18 Forks
18 Watching
5 Branches
19 Contributors
Latest Version
1.168.0
Package Id
fullstack-phone@1.168.0
Unpacked Size
1.06 MB
Size
94.78 kB
File Count
11
NPM Version
8.19.4
Node Version
16.20.2
Publised On
14 Dec 2024
Cumulative downloads
Total Downloads
Last day
142.9%
17
Compared to previous day
Last week
6.7%
95
Compared to previous week
Last month
-61.2%
407
Compared to previous month
Last year
-21.4%
7,295
Compared to previous year
npm | Libphonenumber version |
---|---|
v8.13.52 |
fullstack-phone provides formatting, validation, and parsing of phone numbers per-region. The system is optimized for use as two modules:
This project was extended from Nathan Hammond's project, which itself is an adaptation of Google's libphonenumber library.
1npm install fullstack-phone
Demo App: fullstack-phone-demo
The modules are optimized for use in two environments.
On the server (requires Node 4+):
1// Node.js: 2var phoneServer = require('fullstack-phone/server'); 3 4var metadata = phoneServer.loadMeta(['US', 'RU']); // load US and RU phone metadata 5// now serve the metadata via some REST API or write it to a file for bundling with client code
On the client (assuming a client-side bundler that provides require
, like webpack):
1// Browser: 2var phoneClient = require('fullstack-phone/client'); 3 4// fetch the metadata somehow, then pass to createPhoneHandler to instantiate a handler: 5var phoneHandler = phoneClient.createPhoneHandler(metadata);
Once initialized, the phone handler can be used to process phone numbers:
1// phone handler functions: 2 3phoneHandler.getSupportedRegions(); // > ['US', 'RU'] 4 5phoneHandler.formatPhoneNumber( 6 { countryCode: '1', nationalNumber: '5101234567'}, 7 { style: 'national'} 8); 9// > '(510) 123-4567' 10 11phoneHandler.parsePhoneNumber('5101234567', 'US'); 12// > { countryCode: '1', nationalNumber: '5101234567' } 13 14phoneHandler.validatePhoneNumber( 15 { countryCode: '1', nationalNumber: '5101234567'}, 16 'US' 17); 18// > [Error: PHONE_INVALID_FOR_REGION]
It's also possible to use both within the same environment. Using the server module in the browser, however, nullifies the advantages of the per-region metadata slicing.
Google’s libphonenumber library is the de-facto industry standard for processing international phone numbers, providing support for formatting, validating, and normalizing phone numbers in 250+ regions. However, the default phone metadata is quite heavy. Various custom JS packages have reduced the code & metadata footprint by:
This package fills a different niche by providing:
When using Google libphonenumber directly, processing a phone number requires parsing a string or initializing a protocol buffer phone number object and calling setters for its various properties.
In contrast, fullstack-phone provides a more idiomatic JavaScript phone object, removing the need to call multiple setters. Most of the phone number functions here operate on a canonical phoneObj
, as follows:
1{ 2 countryCode : '1', 3 nationalNumber : '5105551234', 4 extension : '999' 5}
countryCode
'1'
.nationalNumber
'4085551212'
.extension
'123'
.For proper formatting and validation results, nationalNumber
and countryCode
must only contain digits. In addition, countryCode
must be the calling code of a country for which the phone handler was initialized. (For example, if a phoneObj
is passed with countryCode: 44
, the phone handler must have been loaded with GB metadata for proper results.)
The phoneObj
object can be created by calling parsePhoneNumber
on a phone number string.
1var phoneServer = require('fullstack-phone/server');
The phone server exposes only one function:
phoneServer.loadMeta(regionCodeArray)
Given an array of two-letter region codes (ISO 3166-1 alpha-2), return a metadata bundle for those regions, to be passed to phoneClient.createPhoneHandler()
.
regionCodeArray
is undefined, all regional metadata is returned.loadMeta
adds support for the following regions to Google libphonenumber:
loadMeta
takes care of this automatically.The full list of region codes supported is:
1['001', 'AC', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GT', 'GU', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TA', 'TC', 'TD', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW']
Note that '001'
is used to load metadata for global numbers, such as 1-800 numbers.
1var meta = phoneServer.loadMeta(['DE', 'AU']); 2// > metadata object for DE and AU 3 4var meta = phoneServer.loadMeta(); 5// > metadata object for all regions
1var phoneClient = require('fullstack-phone/client');
The phone client exposes only one function:
phoneClient.createPhoneHandler(metadata)
Given a metadata bundle from phoneServer.loadMeta()
, return a phone handler instantiated for that metadata.
1var phoneHandler = phoneClient.createPhoneHandler(metadata);
The phone handler returned by createPhoneHandler
provides the following methods:
Any method that takes a phoneObj
parameter can throw the following exception if called with an invalid canonical phone object:
1phoneHandler.inferPhoneNumberType(123); 2// Uncaught Error: Phone object conversion failed 3 4phoneHandler.inferPhoneNumberType({ countryCode: 1, nationalNumber: false }); 5// Uncaught Error: Phone object conversion failed
Any method that takes a regionCode
string can throw the following exception if called with a region code for which the handler has not been initialized:
1phoneHandler.getCountryCodeForRegion(); 2// Uncaught Error: Metadata not loaded for region: undefined 3 4phoneHandler.getCountryCodeForRegion('XX'); 5// Uncaught Error: Metadata not loaded for region: XX
phoneHandler.getSupportedRegions()
Return array of supported region codes.
Note that if a dependent region was loaded (such as the Bahamas), the main region for the shared country code is also reported a supported region (e.g., US).
1phoneHandler.getSupportedRegions(); 2// > ['US', 'RU']
phoneHandler.countryCodeToRegionCodeMap()
Return map from country calling codes to array of supported regions.
1phoneHandler.countryCodeToRegionCodeMap(); 2// > { '1': [ 'US', 'BS' ], '7': [ 'RU' ] }
phoneHandler.getCountryCodeForRegion(regionCode)
Return a country calling code as a number, given a regionCode
string (assuming metadata has already been loaded for that region).
1phoneHandler.getCountryCodeForRegion('RU'); 2// > 7
phoneHandler.formatPhoneNumber(phoneObj, options)
Return a formatted phone number as a string, given a phoneObj
object and options
object with a valid style
property ('national'
, 'international'
, 'e164'
, or 'rfc3966'
).
The options
object has a single string property to indicate the formatting style desired:
1{ 2 style: 'national' // or 'international', 'e164', 'rfc3966' 3}
1var phone = { countryCode: '1', nationalNumber: '5101234567' }; 2 3phoneHandler.formatPhoneNumber(phone, { style: 'international' }); 4// > '+1 510-123-4567' 5 6phoneHandler.formatPhoneNumber(phone, { style: 'national' }); 7// > '(510) 123-4567' 8 9phoneHandler.formatPhoneNumber(phone, { style: 'e164' }); 10// > '+15101234567' 11 12phoneHandler.formatPhoneNumber(phone, { style: 'rfc3966' }) 13// > 'tel:+1-510-123-4567'
phoneHandler.validatePhoneNumber(phoneObj, ?regionCode)
Perform full validation on a phone number: Given a phoneObj
object and optional regionCode
string, return an Error object indicating any problems with the phone object (or true
if it passed validation).
If regionCode
is provided, the phone number is validated for the given region. If it is omitted, the phone number is validated for the region inferred from the number itself. In both cases, the handler needs to have already been instantiated with metadata for the expected region(s).
The possible error messages are:
'PHONE_INVALID_FOR_REGION'
'PHONE_INVALID_COUNTRY_CODE'
phoneObj.countryCode
is not recognized for one of these reasons:
phoneObj.countryCode
, orregionCode
passed.'PHONE_NUMBER_TOO_LONG'
'PHONE_NUMBER_TOO_SHORT'
'PHONE_NUMBER_INVALID_LENGTH'
1// valid US phone number 2phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '5105261234' }, 'US'); 3// > true 4 5// regionCode is optional 6phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '5105261234' }); 7// > true 8 9phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '5' }, 'US'); 10// > [Error: PHONE_NUMBER_TOO_SHORT] 11 12phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '51052612341' }, 'US'); 13// > [Error: PHONE_NUMBER_TOO_LONG] 14 15// 10 digits (like a US phone number), but not an actual valid number 16phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '1234567890' }, 'US'); 17// > [Error: PHONE_INVALID_FOR_REGION] 18phoneHandler.validatePhoneNumber({ countryCode: '1', nationalNumber: '1234567890' }); 19// > [Error: PHONE_INVALID_FOR_REGION] 20 21// completely invalid countryCode 22phoneHandler.validatePhoneNumber({ countryCode: '999', nationalNumber: '5105261234' }, 'US'); 23// > [Error: PHONE_INVALID_COUNTRY_CODE] 24 25// countryCode 44 is for GB, but US regionCode was passed 26phoneHandler.validatePhoneNumber({ countryCode: '44', nationalNumber: '1212345678' }, 'US'); 27// > [Error: PHONE_INVALID_COUNTRY_CODE] 28 29// valid GB number 30phoneHandler.validatePhoneNumber({ countryCode: '44', nationalNumber: '1212345678' }, 'GB'); 31// > true 32phoneHandler.validatePhoneNumber({ countryCode: '44', nationalNumber: '1212345678' }); 33// > true
phoneHandler.validateLength(phoneObj, ?regionCode)
Perform minimal validation (length check only) on a phone number: Given a phoneObj
object and optional regionCode
string, return an Error object indicating any length problems with the phone object (or true
if it passed the length validation).
If regionCode
is provided, the phone number is validated for the given region. If it is omitted, the phone number is validated for the region inferred from the number itself. In both cases, the handler needs to have already been instantiated with metadata for the expected region(s).
The possible error messages are:
'PHONE_INVALID_COUNTRY_CODE'
phoneObj.countryCode
is not recognized for one of these reasons:
phoneObj.countryCode
, orregionCode
passed.'PHONE_NUMBER_TOO_LONG'
'PHONE_NUMBER_TOO_SHORT'
'PHONE_NUMBER_INVALID_LENGTH'
'PHONE_NUMBER_POSSIBLE_LOCAL_ONLY'
'PHONE_INVALID_FOR_REGION
'
1// any 10-digit phone number in the US passes validateLength 2phoneHandler.validateLength({ countryCode: '1', nationalNumber: '1234567890' }, 'US'); 3// > true 4 5// regionCode is optional 6phoneHandler.validateLength({ countryCode: '1', nationalNumber: '1234567890' }); 7// > true 8 9// 7-digit numbers are only possible locally in the US 10phoneHandler.validateLength({ countryCode: '1', nationalNumber: '1234567' }, 'US'); 11// > [Error: PHONE_NUMBER_POSSIBLE_LOCAL_ONLY] 12 13// 6-digit numbers are too short in the US 14phoneHandler.validateLength({ countryCode: '1', nationalNumber: '123456' }, 'US'); 15// > [Error: PHONE_NUMBER_TOO_SHORT] 16 17// 11-digit numbers are too long in the US 18phoneHandler.validateLength({ countryCode: '1', nationalNumber: '12345678901' }, 'US'); 19// > [Error: PHONE_NUMBER_TOO_LONG] 20 21// wrong regionCode yields PHONE_INVALID_COUNTRY_CODE (regionCode AD does not match countryCode 1) 22phoneHandler.validateLength({ countryCode: '1', nationalNumber: '1234567890' }, 'AD'); 23// > [Error: PHONE_INVALID_COUNTRY_CODE] 24 25// a completely invalid countryCode (999) also yields PHONE_INVALID_COUNTRY_CODE 26phoneHandler.validateLength({ countryCode: '999', nationalNumber: '1234567890' }); 27// > [Error: PHONE_INVALID_COUNTRY_CODE] 28 29// 7-digit number in Andorra (regionCode AD, countryCode 376) is in between valid number lengths 30phoneHandler.validateLength({ countryCode: '376', nationalNumber: '1234567' }, 'AD'); 31// > [Error: PHONE_NUMBER_INVALID_LENGTH]
phoneHandler.parsePhoneNumber(phoneNumberToParse, ?regionCode)
Parse a string and return a phoneObj
object (or an Error object if parsing failed), given the string parameters phoneNumberToParse
and optional regionCode
.
The optional regionCode
parameter provides a fallback if the country code cannot be extracted from the phoneNumberToParse
string. If the string contains +
followed by the country code, then regionCode
can be safely omitted. In both cases, however, the handler needs to have already been instantiated with metadata for the expected region(s).
The possible error messages are:
'PHONE_INVALID_COUNTRY_CODE'
'PHONE_NUMBER_TOO_SHORT'
'PHONE_NUMBER_TOO_LONG'
'PHONE_NOT_A_NUMBER'
'PHONE_TOO_SHORT_AFTER_IDD'
1phoneHandler.parsePhoneNumber('5101234567', 'US'); 2// > { countryCode: '1', nationalNumber: '5101234567' } 3 4phoneHandler.parsePhoneNumber('ABC', 'US'); 5// > [Error: PHONE_NOT_A_NUMBER] 6 7// regionCode is optional if string has '+' followed by country code 8phoneHandler.parsePhoneNumber('+1 510-123-4567'); // international format 9phoneHandler.parsePhoneNumber('+15101234567'); // E.164 format 10phoneHandler.parsePhoneNumber('tel:+1-510-123-4567'); // RFC 3966 format 11// > { countryCode: '1', nationalNumber: '5101234567' } 12 13// Do not omit regionCode if phoneNumberString is missing '+' 14phoneHandler.parsePhoneNumber('15101234567'); 15// > [Error: PHONE_INVALID_COUNTRY_CODE] 16h.parsePhoneNumber("15101234567", "US"); 17// > { countryCode: '1', nationalNumber: '5101234567' }
phoneHandler.getExampleNumberForType(type, regionCode)
Return an example phoneObj
object, given the string parameters type
and regionCode
.
The type
parameter is an enum based on libphonenumber i18n.phonenumbers.PhoneNumberType and can be any of the following strings:
'FIXED_LINE'
'MOBILE'
'FIXED_LINE_OR_MOBILE'
'TOLL_FREE'
'PREMIUM_RATE'
'SHARED_COST'
'VOIP'
'PERSONAL_NUMBER'
'PAGER'
'UAN'
'VOICEMAIL'
'UNKNOWN'
1phoneHandler.getExampleNumberForType('MOBILE', 'US'); 2// > { countryCode: '1', nationalNumber: '2015550123' }
phoneHandler.inferPhoneNumberType(phoneObj)
Return a string indicating the phone number type (see getExampleNumberForType
), given a valid phoneobj
. Returns 'UNKNOWN'
if metadata has not been loaded for the region of the phone number or the number type otherwise cannot be inferred.
1phoneHandler.inferPhoneNumberType({ countryCode: '1', nationalNumber: '5105261568' }); 2// > 'FIXED_LINE_OR_MOBILE' 3 4// GB landline 5phoneHandler.inferPhoneNumberType({ countryCode: '44', nationalNumber: '1212345678' }); 6// > 'FIXED_LINE' 7 8// GB mobile number 9phoneHandler.inferPhoneNumberType({ countryCode: '44', nationalNumber: '7400123456' }) 10// > 'MOBILE' 11 12// invalid GB phone number 13phoneHandler.inferPhoneNumberType({ countryCode: '44', nationalNumber: '999999' }); 14// > 'UNKNOWN'
phoneHandler.inferPhoneNumberRegion(phoneObj)
Return the two letter region code associated with a valid phoneObj
.
Returns null
if the region cannot be determined. (This can happen if metadata has not been loaded for the region associated with the phoneObj.countryCode
.)
Important: This method only guarantees correct results for valid phone numbers. (See the libphonenumber source code.)
1phoneHandler.inferPhoneNumberRegion({ countryCode: '44', nationalNumber: '1212345678' }); 2// > 'GB' 3 4phoneHandler.inferPhoneNumberRegion({ countryCode: '99', nationalNumber: '1212345678' }); 5// > null
phoneHandler.getAsYouTypeFormatter(regionCode)
Return a new AsYouTypeFormatter object instantiated for the given regionCode
.
1var formatter = phoneHandler.getAsYouTypeFormatter('GB'); 2// > AsYouTypeFormatter object initialized to Great Britain
The initialized AsYouTypeFormatter object itself exposes the following methods:
formatter.inputDigit(digit)
Given a digit (number or string), output the phone number formatted thus far (given the history of inputted digits).
Note that digit
can also be '+'
or '*'
1formatter.inputDigit('5'); // > '5' 2formatter.inputDigit('1'); // > '51' 3formatter.inputDigit('0'); // > '510' 4formatter.inputDigit('1'); // > '510-1' 5formatter.inputDigit('2'); // > '510-12' 6formatter.inputDigit('3'); // > '510-123' 7formatter.inputDigit('4'); // > '510-1234' 8formatter.inputDigit('5'); // > '(510) 123-45' 9formatter.inputDigit('6'); // > '(510) 123-456' 10formatter.inputDigit('7'); // > '(510) 123-4567'
formatter.clear()
Clear the formatter state.
1formatter.inputDigit('5'); // > '5' 2formatter.inputDigit('1'); // > '51' 3formatter.inputDigit('0'); // > '510' 4formatter.inputDigit('1'); // > '510-1' 5formatter.inputDigit('2'); // > '510-12' 6formatter.clear(); 7formatter.inputDigit('9'); // > '9' 8formatter.inputDigit('1'); // > '91' 9formatter.inputDigit('9'); // > '919' 10formatter.inputDigit('4'); // > '919-4' 11formatter.inputDigit('8'); // > '919-48' 12...
formatter.inputDigitAndRememberPosition(digit)
Same as inputDigit, but remembers the (1-indexed) position where the digit was entered, to be retrieved later by getRememberedPosition. This position can update as formatting characters are inserted by the AsYouTypeFormatter.
formatter.getRememberedPosition()
Returns the (1-indexed) position (as a number) of the digit previously passed to inputDigitAndRememberPosition.
1// getRememberedPosition starts out as 0 2formatter.getRememberedPosition(); // > 0 3 4formatter.inputDigitAndRememberPosition(5); // > '5' 5 6// the 5 was inputted at position 1 7formatter.getRememberedPosition(); // > 1 8 9// input additional digits until parens are inserted 10formatter.inputDigit(1); // > '51' 11formatter.inputDigit(0); // > '510' 12formatter.inputDigit(1); // > '510-1' 13formatter.inputDigit(2); // > '510-12' 14formatter.inputDigit(3); // > '510-123' 15formatter.inputDigit(4); // > '510-1234' 16formatter.inputDigit(5); // > '(510) 123-45' 17 18// now the original 5 is at position 2, since an open paren was inserted before it 19formatter.getRememberedPosition(); // 2
see DEVELOPMENT.md
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
12 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-02
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More