Google's libphonenumber pre-compiled with the closure compiler
Installations
npm install awesome-phonenumber
Score
99.6
Supply Chain
99.6
Quality
85.6
Maintenance
100
Vulnerability
99.6
License
Developer
grantila
Developer Guide
Module System
CommonJS, ESM
Min. Node Version
>=18
Typescript Support
Yes
Node Version
22.9.0
NPM Version
10.8.3
Statistics
665 Stars
306 Commits
54 Forks
6 Watching
2 Branches
7 Contributors
Updated on 21 Nov 2024
Bundle Size
331.48 kB
Minified
74.55 kB
Minified + Gzipped
Languages
JavaScript (92.71%)
TypeScript (6.36%)
Shell (0.93%)
Total Downloads
Cumulative downloads
Total Downloads
52,122,035
Last day
3.7%
85,193
Compared to previous day
Last week
10%
496,792
Compared to previous week
Last month
9.2%
1,932,463
Compared to previous month
Last year
55.6%
17,684,070
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Awesome phonenumber parser
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
Versions
- v3:
- Changed API (although with backwards compatible ABI)
- Added ESM export
- v4:
- Changed API to be much cleaner
- No constructor
- No functions on returned object
- No errors being thrown
- Not backwards compatible, although like v3 except:
- The second argument to
parsePhoneNumber
is an object- E.g.
{ regionCode: 'SE' }
instead of a region code string
- E.g.
- The return value is like
toJSON( )
on v3
- The second argument to
- Changed API to be much cleaner
- v5:
- Dropped Node 12 support
- v6:
- Dropped Node 16 support
- v7:
- Added
findNumbers( )
feature, to find phone numbers in text - Added support for short numbers
- Added
Comparison with other libraries
Since 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 |
Basic usage
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.
API
1import { 2 parsePhoneNumber, 3 findNumbers, 4 getNumberFrom, 5 getExample, 6 getCountryCodeForRegionCode, 7 getRegionCodeForCountryCode, 8 getSupportedCallingCodes, 9 getSupportedRegionCodes, 10 getAsYouType, 11} from 'awesome-phonenumber'
parsePhoneNumber
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.
findNumbers
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).
getNumberFrom
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}
getExample
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.
Example
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'
Country codes
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
Example
1getCountryCodeForRegionCode( 'SE' ); // -> 46 2getRegionCodeForCountryCode( 46 ); // -> 'SE'
Supported calling codes
1getSupportedCallingCodes( ); // -> [ calling codes... ]
Supported region codes
1getSupportedRegionCodes( ); // -> [ region codes... ]
API types
The API consists of the PhoneNumber
class which sometimes uses enums. These are:
Phone number types
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'
Phone number possibilities
1type PhoneNumberPossibility = 2 | 'is-possible' 3 | 'invalid-country-code' 4 | 'too-long' 5 | 'too-short' 6 | 'unknown'
Phone number formats
1'international' 2'national' 3'e164' 4'rfc3966' 5'significant'
As-you-type formatting
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.
Example
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
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/master.yml:45
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 0 commits out of 1 are checked with a SAST tool
Reason
5 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 4
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql.yml:17
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql.yml:16
- Warn: no topLevel permission defined: .github/workflows/branches.yml:1
- Warn: no topLevel permission defined: .github/workflows/codeql.yml:1
- Warn: no topLevel permission defined: .github/workflows/master.yml:1
- Info: no jobLevel write permissions found
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/branches.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/branches.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:36: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql.yml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/codeql.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:51: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/master.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/master.yml:53: update your workflow using https://app.stepsecurity.io/secureworkflow/grantila/awesome-phonenumber/master.yml/master?enable=pin
- Info: 0 out of 10 GitHub-owned GitHubAction dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
4.5
/10
Last Scanned on 2024-11-25
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