Gathering detailed insights and metrics for awesome-phonenumber
Gathering detailed insights and metrics for awesome-phonenumber
Gathering detailed insights and metrics for awesome-phonenumber
Gathering detailed insights and metrics for awesome-phonenumber
Google's libphonenumber pre-compiled with the closure compiler
npm install awesome-phonenumber
99.6
Supply Chain
99.6
Quality
85.6
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
665 Stars
306 Commits
54 Forks
6 Watching
2 Branches
7 Contributors
Updated on 21 Nov 2024
Minified
Minified + Gzipped
JavaScript (92.71%)
TypeScript (6.36%)
Shell (0.93%)
Cumulative downloads
Total Downloads
Last day
8.1%
87,809
Compared to previous day
Last week
11.2%
493,732
Compared to previous week
Last month
10.9%
1,918,107
Compared to previous month
Last year
55.4%
17,647,806
Compared to previous year
This library is a pre-compiled version of Google's libphonenumber
, with a slightly simpler interface. It has a minimal footprint - is by far the smallest libphonenumber-based library available on npmjs, and has no dependencies.
Unlike libphonenumber, it includes a findNumbers( )
function to find phone numbers in text.
TypeScript typings are provided within the package.
Uses libphonenumber v8.13.47
parsePhoneNumber
is an object
{ regionCode: 'SE' }
instead of a region code stringtoJSON( )
on v3findNumbers( )
feature, to find phone numbers in textSince this library is pre-compiled, it doesn't depend on the closure compiler, and needs not load it on start. This makes the library faster and saves you a lot of space. It also means this library is trivial to use in any webpack
project (or using any other means to run in the browser).
Among all the popular phone number using Google's libphonenumber
(or mimicing it), only this one, google-libphonenumber
and libphonenumber-js
have decent README's with examples. This may have changed since first doing these benchmarks.
A library should be quick to load (require()
), quick to parse first time and all consecutive times. It shouldn't bloat your node_modules
, and it should have a small memory footprint, if possible.
The following is the result of a test program which loads the library, then parses a phone number, and then once again. It's called 100 times for each library and the mean values are shown here. Parsing a phone number first time might be slower because of initially compiling/optimizing regular expressions and whatnot. Parsing a phone number a second time will show the speed of likely all future parsing within that process.
Action | awesome-phonenumber 2.56.0 (lib 8.12.29) | google-libphonenumber 3.2.22 (lib 8.12.27) | libphonenumber-js 1.9.23 (lib -) |
---|---|---|---|
Load library first time | 11.0 ms ✅ | 29.67 ms | 32.87 ms |
Parse first phone number | 4.3 ms | 4.01 ms | 3.43 ms ✅ |
⇒ Load + parse first number | 15.3 ms ✅ | 33.68 ms | 36.3 ms |
Parse second phone number | 0.78 ms ✅ | 0.97 ms | 0.92 ms |
Increased memory usage | 5.12 M ✅ | 9.99 M | 5.86 M |
node_modules size | 296 K ✅ | 600 K | 7.6 M |
node_modules files | 8 | 7 ✅ | 653 |
1import { parsePhoneNumber } from 'awesome-phonenumber' 2 3const pn = parsePhoneNumber( '0707123456', { regionCode: 'SE' } ); 4// or on e164 format: 5const pn = parsePhoneNumber( '+46707123456' ); 6 7// pn is now the same as: 8const pn = { 9 valid: true, 10 11 number: { 12 input: '0707123456', 13 e164: '+46707123456', 14 international: '+46 70 712 34 56', 15 national: '070-712 34 56', 16 rfc3966: 'tel:+46-70-712-34-56', 17 significant: '707123456', 18 }, 19 possibility: 'is-possible', 20 regionCode: 'SE', 21 possible: true, 22 shortPossible: false, 23 shortValid: false, 24 canBeInternationallyDialled: true, 25 type: 'mobile', 26 countryCode: 46, 27 typeIsMobile: true, 28 typeIsFixedLine: false, 29};
The return type is ParsedPhoneNumber
which is either a ParsedPhoneNumberValid
or a ParsedPhoneNumberInvalid
. The valid
property identifies whether the parsing was successful or not, hence which type is returned.
The format of a successful parsing is:
1interface ParsedPhoneNumberValid { 2 valid: true; 3 4 number: { 5 input: string; 6 international: string; 7 national: string; 8 e164: string; 9 rfc3966: string; 10 significant: string; 11 }; 12 possibility: PhoneNumberPossibility; // a string union, see below 13 regionCode: string; 14 possible: boolean; 15 shortPossible: boolean; 16 shortValid: boolean; 17 canBeInternationallyDialled: boolean; 18 type: PhoneNumberTypes; // a string union, see below 19 countryCode: number; 20 typeIsMobile: boolean; 21 typeIsFixedLine: boolean; 22}
If the number failed to be parsed, or there was another error, the return type is:
1interface ParsedPhoneNumberInvalid { 2 valid: false; 3 4 possible: false; 5 possibility: 'invalid'; 6 shortPossible: boolean; 7 shortValid: boolean; 8 error?: unknown; 9};
Note that an incorrect (invalid) phone number can still be a valid short number for the given region.
1import { 2 parsePhoneNumber, 3 findNumbers, 4 getNumberFrom, 5 getExample, 6 getCountryCodeForRegionCode, 7 getRegionCodeForCountryCode, 8 getSupportedCallingCodes, 9 getSupportedRegionCodes, 10 getAsYouType, 11} from 'awesome-phonenumber'
parsePhoneNumber( phoneNumber, { regionCode: string } )
parses a phone number as described above.
The first argument is the phone number to parse, on either national or international (e164, i.e. prefixed with a +
) form. If national form, the second argument is required to contain a regionCode
string property, e.g. 'SE' for Sweden, 'CH' for Switzerland, etc.
To find (extract) phone numbers in text, use findNumbers( )
:
1import { findNumbers } from 'awesome-phonenumber' 2 3const text = 'My number is +46 707 123 456, otherwise call +33777777777.'; 4const numbers = findNumbers( text );
The returned list of numbers is of the type PhoneNumberMatch
such as:
1interface PhoneNumberMatch 2{ 3 text: string; // The raw string found 4 phoneNumber: object; // Same as the result of parsePhoneNumber() 5 start: number; // Start offset in the text 6 end: number; // End offset in the text 7}
A second options argument to findNumbers( text, options )
can be provided on the form:
1interface FindNumbersOptions 2{ 3 defaultRegionCode?: string; 4 leniency?: FindNumbersLeniency; 5 maxTries?: number; 6}
where FindNumbersLeniency
is an enum of 'valid'
or 'possible'
. The default is 'valid'
meaning that only valid phone numbers are found. If this is set to 'possible'
also possible (but invalid) phone numbers are found.
defaultRegionCode
can be set (e.g. to 'SE'
for Sweden), in which case phone numbers on national form (i.e. without +
prefix) will be found, as long as they are from that region.
For really large texts, maxTries
will set the maximum number of phone numbers to try to find (not necessary actually find).
1import { parsePhoneNumber, getNumberFrom } from 'awesome-phonenumber' 2 3const pn = parsePhoneNumber( '0707654321', { regionCode: 'SE' } ); 4if ( pn.valid ) { 5 const fromJp = getNumberFrom( pn, 'JP' ); 6 // fromJp is the number to call from Japan: 7 fromJp.number === "010 46 70 765 43 21"; 8}
The return value from getNumberFrom
is a PhoneNumberFrom
which is either a PhoneNumberFromValid
or a PhoneNumberFromInvalid
.
The PhoneNumberFromValid
is defined as:
1interface PhoneNumberFromValid 2{ 3 valid: true; 4 number: string; 5}
The PhoneNumberFromInvalid
is defined as:
1interface PhoneNumberFromInvalid 2{ 3 valid: false; 4 error?: unknown; 5}
Sometimes you want to display a formatted example phone number for a certain country (and maybe also a certain type of phone number). The getExample
function is used for this.
1import { getExample } from 'awesome-phonenumber' 2 3getExample( regionCode[, phoneNumberType] ); // Parsed phone number
The phoneNumberType
is any of the types defined above.
1import { getExample } from 'awesome-phonenumber' 2 3// Get an example Swedish phone number 4const example = getExample( 'SE' ); // A ParsedPhoneNumberValid 5const exampleMobile = getExample( 'SE', 'mobile' ); // A ParsedPhoneNumberValid 6 7example.number.e164; // e.g. '+468123456' 8exampleMobile.number.e164; // e.g. '+46701234567' 9exampleMobile.number.national; // e.g. '070 123 45 67'
There are conversion functions between the 2-character ISO 3166-1 region codes (e.g. 'SE' for Sweden) and the corresponding country calling codes.
1import { 2 getCountryCodeForRegionCode, 3 getRegionCodeForCountryCode, 4 getSupportedCallingCodes, 5 getSupportedRegionCodes, 6} from 'awesome-phonenumber' 7 8getCountryCodeForRegionCode( regionCode ); // -> countryCode 9getRegionCodeForCountryCode( countryCode ); // -> regionCode
1getCountryCodeForRegionCode( 'SE' ); // -> 46 2getRegionCodeForCountryCode( 46 ); // -> 'SE'
1getSupportedCallingCodes( ); // -> [ calling codes... ]
1getSupportedRegionCodes( ); // -> [ region codes... ]
The API consists of the PhoneNumber
class which sometimes uses enums. These are:
1type PhoneNumberTypes = 2 | 'fixed-line' 3 | 'fixed-line-or-mobile' 4 | 'mobile' 5 | 'pager' 6 | 'personal-number' 7 | 'premium-rate' 8 | 'shared-cost' 9 | 'toll-free' 10 | 'uan' 11 | 'voip' 12 | 'unknown'
1type PhoneNumberPossibility = 2 | 'is-possible' 3 | 'invalid-country-code' 4 | 'too-long' 5 | 'too-short' 6 | 'unknown'
1'international' 2'national' 3'e164' 4'rfc3966' 5'significant'
You can create an AsYouType
class with getAsYouType()
to format a phone number as it is being typed.
1import { getAsYouType } from 'awesome-phonenumber' 2 3const ayt = getAsYouType( 'SE' );
The returned class instance has the following methods
1// Add a character to the end of the number 2ayt.addChar( nextChar: string ); 3 4// Get the current formatted number 5ayt.number( ); 6 7// Remove the last character 8ayt.removeChar( ); 9 10// Replace the whole number with a new number (or an empty number if undefined) 11ayt.reset( number?: string ); 12 13// Get a ParsedPhoneNumber object representing the current number 14ayt.getPhoneNumber( );
All the functions above except getPhoneNumber( )
return the current formatted number as a string.
1import { getAsYouType } from 'awesome-phonenumber' 2 3const ayt = getAsYouType( 'SE' ); 4ayt.addChar( '0' ); // -> '0' 5ayt.addChar( '7' ); // -> '07' 6ayt.addChar( '0' ); // -> '070' 7ayt.addChar( '7' ); // -> '070 7' 8ayt.addChar( '1' ); // -> '070 71' 9ayt.addChar( '2' ); // -> '070 712' 10ayt.addChar( '3' ); // -> '070 712 3' 11ayt.addChar( '4' ); // -> '070 712 34' 12ayt.addChar( '5' ); // -> '070 712 34 5' 13ayt.addChar( '6' ); // -> '070 712 34 56' 14ayt.removeChar( ); // -> '070 712 34 5' 15ayt.addChar( '7' ); // -> '070 712 34 57'
No vulnerabilities found.
Reason
12 commit(s) and 9 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
Found 1/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
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
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