Gathering detailed insights and metrics for @xdanangelxoqenpm/officiis-unde-iusto
Gathering detailed insights and metrics for @xdanangelxoqenpm/officiis-unde-iusto
Gathering detailed insights and metrics for @xdanangelxoqenpm/officiis-unde-iusto
Gathering detailed insights and metrics for @xdanangelxoqenpm/officiis-unde-iusto
npm install @xdanangelxoqenpm/officiis-unde-iusto
Typescript
Module System
Node Version
NPM Version
48.1
Supply Chain
48.1
Quality
75.7
Maintenance
100
Vulnerability
99.6
License
JavaScript (100%)
Total Downloads
754
Last Day
1
Last Week
3
Last Month
21
Last Year
754
2,046 Commits
1 Watching
1 Branches
1 Contributors
Latest Version
1.0.0
Package Id
@xdanangelxoqenpm/officiis-unde-iusto@1.0.0
Unpacked Size
27.63 kB
Size
9.83 kB
File Count
10
NPM Version
10.5.0
Node Version
20.12.2
Publised On
25 Apr 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-40%
3
Compared to previous week
Last month
10.5%
21
Compared to previous month
Last year
0%
754
Compared to previous year
18
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.
TypeScript typings are provided within the package.
Uses libphonenumber v8.13.35
parsePhoneNumber
is an object
{ regionCode: 'SE' }
instead of a region code stringtoJSON( )
on v3Since 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 | @xdanangelxoqenpm/officiis-unde-iusto 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 '@xdanangelxoqenpm/officiis-unde-iusto' 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 canBeInternationallyDialled: true, 23 type: 'mobile', 24 countryCode: 46, 25 typeIsMobile: true, 26 typeIsFixedLine: false, 27};
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 canBeInternationallyDialled: boolean; 16 type: PhoneNumberTypes; // a string union, see below 17 countryCode: number; 18 typeIsMobile: boolean; 19 typeIsFixedLine: boolean; 20}
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 error?: unknown; 7};
1import { 2 parsePhoneNumber, 3 getNumberFrom, 4 getExample, 5 getCountryCodeForRegionCode, 6 getRegionCodeForCountryCode, 7 getSupportedCallingCodes, 8 getSupportedRegionCodes, 9 getAsYouType, 10} from '@xdanangelxoqenpm/officiis-unde-iusto'
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.
1import { parsePhoneNumber, getNumberFrom } from '@xdanangelxoqenpm/officiis-unde-iusto' 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 '@xdanangelxoqenpm/officiis-unde-iusto' 2 3getExample( regionCode[, phoneNumberType] ); // Parsed phone number
The phoneNumberType
is any of the types defined above.
1import { getExample } from '@xdanangelxoqenpm/officiis-unde-iusto' 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 '@xdanangelxoqenpm/officiis-unde-iusto' 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 '@xdanangelxoqenpm/officiis-unde-iusto' 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 '@xdanangelxoqenpm/officiis-unde-iusto' 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.
No security vulnerabilities found.