Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

awesome-phonenumber

Package Overview
Dependencies
Maintainers
1
Versions
166
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

awesome-phonenumber - npm Package Compare versions

Comparing version 2.73.0 to 3.0.0

163

index.d.ts

@@ -0,61 +1,124 @@

export type PhoneNumberFormat =
'e164' |
'international' |
'national' |
'rfc3966' |
'significant';
declare namespace AwesomePhonenumber
export type PhoneNumberTypes =
'fixed-line' |
'fixed-line-or-mobile' |
'mobile' |
'pager' |
'personal-number' |
'premium-rate' |
'shared-cost' |
'toll-free' |
'uan' |
'voip' |
'unknown';
export class PhoneNumber
{
type PhoneNumberFormat =
'e164' |
'international' |
'national' |
'rfc3966' |
'significant';
/** @deprecated use `getPhoneNumber()` instead */
constructor( phoneNumber: string, regionCode?: string );
type PhoneNumberTypes =
'fixed-line' |
'fixed-line-or-mobile' |
'mobile' |
'pager' |
'personal-number' |
'premium-rate' |
'shared-cost' |
'toll-free' |
'uan' |
'voip' |
'unknown';
isValid( ): boolean;
canBeInternationallyDialled( ): boolean;
isPossible( ): boolean;
getType( ): PhoneNumberTypes;
isMobile( ): boolean;
isFixedLine( ): boolean;
getNumber( type?: PhoneNumberFormat ): string;
getNumberFrom( regionCode: string ): string;
getRegionCode( ): string;
getCountryCode( ): number;
toJSON( ): any;
/**
* @deprecated Use the exported {@link getCountryCodeForRegionCode} function instead.
*/
static getCountryCodeForRegionCode( regionCode: string ): number;
class PhoneNumber
{
constructor( phoneNumber: string, countryCode?: string );
/**
* @deprecated Use the exported {@link getRegionCodeForCountryCode} function instead.
*/
static getRegionCodeForCountryCode( countryCode: number ): string;
isValid( ): boolean;
canBeInternationallyDialled( ): boolean;
isPossible( ): boolean;
getType( ): PhoneNumberTypes;
isMobile( ): boolean;
isFixedLine( ): boolean;
getNumber( type?: PhoneNumberFormat ): string;
getNumberFrom( regionCode: string ): string;
getRegionCode( ): string;
getCountryCode( ): number;
toJSON( ): any;
/**
* @deprecated Use the exported {@link getSupportedCallingCodes} function instead.
*/
static getSupportedCallingCodes( ): string[ ];
static getCountryCodeForRegionCode( regionCode: string ): number;
static getRegionCodeForCountryCode( countryCode: number ): string;
static getSupportedCallingCodes( ): string[ ];
static getSupportedRegionCodes( ): string[ ];
static getExample( regionCode: string, type?: PhoneNumberTypes ): PhoneNumber;
static getAsYouType( regionCode: string ): AsYouType;
}
/**
* @deprecated Use the exported {@link getSupportedRegionCodes} function instead.
*/
static getSupportedRegionCodes( ): string[ ];
function PhoneNumber( phoneNumber: string, countryCode?: string ): PhoneNumber;
/**
* @deprecated Use the exported {@link getExample} function instead.
*/
static getExample( regionCode: string, type?: PhoneNumberTypes ): PhoneNumber;
class AsYouType
{
addChar( char: string ): string;
number( ): string;
removeChar( ): string;
reset( number?: string ): string;
getPhoneNumber( ): PhoneNumber;
}
/**
* @deprecated Use the exported {@link getAsYouType} function instead.
*/
static getAsYouType( regionCode: string ): AsYouType;
}
export default AwesomePhonenumber.PhoneNumber;
/** @deprecated use `parsePhoneNumber()` instead */
export function PhoneNumber( phoneNumber: string, regionCode?: string ): PhoneNumber;
/**
* Parse a phone number into a PhoneNumber class.
*
* @example
* // Using a national phone number format
* parsePhoneNumber( '0707123456', 'SE' )
* // Using an international (e164) phone number format
* parsePhoneNumber( '+46707123456' )
*
* @param phoneNumber Either an `e164` formatted (international) phone number
* or a _national_ phone number.
* @param regionCode Region code for the phone number (only required if
* {@link phoneNumber} is on a _national_ format).
* Example: 'SE' for Sweden, 'CH' for Switzerland, etc.
*/
export function parsePhoneNumber( phoneNumber: string, regionCode?: string ): PhoneNumber;
export function getCountryCodeForRegionCode( regionCode: string ): number;
export function getRegionCodeForCountryCode( countryCode: number ): string;
export function getSupportedCallingCodes( ): string[ ];
export function getSupportedRegionCodes( ): string[ ];
/**
* Get an example phone number, given a region code and a phone number
* {@link PhoneNumberTypes type}.
*
* @param regionCode Region code
* @param type Phone number {@link PhoneNumberTypes type}
*/
export function getExample( regionCode: string, type?: PhoneNumberTypes ): PhoneNumber;
/**
* Get an instance of the AsYouType class, based on a region code.
*
* @param regionCode The region code to get an AsYouType instance for.
*/
export function getAsYouType( regionCode: string ): AsYouType;
export class AsYouType
{
private constructor( );
addChar( char: string ): string;
number( ): string;
removeChar( ): string;
reset( number?: string ): string;
getPhoneNumber( ): PhoneNumber;
}
/** @deprecated use `parsePhoneNumber()` instead */
export default PhoneNumber;

@@ -22,1 +22,9 @@ const exportedName = 'PhoneNumber$$module$src$index';

module.exports.default = module.exports;
module.exports.parsePhoneNumber = module.exports;
module.exports.getCountryCodeForRegionCode = module.exports.getCountryCodeForRegionCode;
module.exports.getRegionCodeForCountryCode = module.exports.getRegionCodeForCountryCode;
module.exports.getSupportedCallingCodes = module.exports.getSupportedCallingCodes;
module.exports.getSupportedRegionCodes = module.exports.getSupportedRegionCodes;
module.exports.getExample = module.exports.getExample;
module.exports.getAsYouType = module.exports.getAsYouType;

@@ -5,3 +5,3 @@ {

"license": "MIT",
"version": "2.73.0",
"version": "3.0.0",
"author": "Gustaf Räntilä <g.rantila@gmail.com>",

@@ -17,7 +17,17 @@ "repository": {

"types": "./index.d.ts",
"exports": {
".": {
"require": "./index.js",
"default": "./index-esm.mjs"
}
},
"scripts": {
"build:libphonenumber": "node_modules/.bin/gulp",
"build:rewrite": "ts-node scripts/rewrite.ts",
"build": "yarn build:libphonenumber && yarn build:rewrite",
"test": "scripts/test.sh",
"build:esm:build": "rollup -c ./rollup.config.js",
"build:esm:rewrite": "cat build/index-esm.js src/esm-outro.js > index-esm.mjs",
"build:esm": "yarn build:esm:build && yarn build:esm:rewrite",
"build": "yarn build:libphonenumber && yarn build:rewrite && yarn build:esm",
"test:compile": "scripts/test.sh",
"test": "yarn test:compile && NODE_OPTIONS=--experimental-vm-modules jest",
"cz": "git-cz",

@@ -32,2 +42,3 @@ "semantic-release": "semantic-release"

"index.js",
"index-esm.js",
"index.d.ts"

@@ -47,17 +58,21 @@ ],

"devDependencies": {
"@babel/preset-env": "^7.17.10",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-commonjs": "^21.0.1",
"@types/jest": "^27.5.0",
"@types/mkdirp": "^1.0.1",
"@types/mocha": "8.2.2",
"@types/node": "^16.11.12",
"chai": "4.3.4",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
"google-closure-compiler": "20210907.0.0",
"google-closure-library": "^20210808.0.0",
"gulp": "4.0.2",
"mkdirp": "1.0.4",
"mocha": "8.4.0",
"pre-commit": "1.x",
"replace": "1.2.1",
"rimraf-promise": "2.x",
"ts-node": "^10.4.0",
"google-closure-compiler": "20220502.0.0",
"google-closure-library": "^20220502.0.0",
"gulp": "^4.0.2",
"jest": "^28.1.0",
"mkdirp": "^1.0.4",
"pre-commit": "^1.2.2",
"replace": "^1.2.1",
"rmfr": "^2.0.0",
"ts-jest-resolver": "^2.0.0",
"rollup": "^2.66.0",
"ts-node": "^10.7.0",
"typescript": "^4.2.4"

@@ -64,0 +79,0 @@ },

@@ -16,2 +16,10 @@ [![npm version][npm-image]][npm-url]

### Versions
- v3:
- Changed API (although with backwards compatible ABI)
- Added ESM export
## Comparison with other libraries

@@ -39,6 +47,6 @@

## Basic usage
```js
var PhoneNumber = require( 'awesome-phonenumber' );
```ts
import { parsePhoneNumber } from 'awesome-phonenumber'
var pn = new PhoneNumber( '0707123456', 'SE' );
const pn = parsePhoneNumber( '0707123456', 'SE' );
pn.isValid( ); // -> true

@@ -76,10 +84,78 @@ pn.isMobile( ); // -> true

### Detect country
## API
When constructed with a phone number on `e164` format (i.e. prefixed with a `+`), awesome-phonenumber will auto-detect the country:
```ts
import {
parsePhoneNumber,
getCountryCodeForRegionCode,
getRegionCodeForCountryCode,
getSupportedCallingCodes,
getSupportedRegionCodes,
getExample,
getAsYouType,
} from 'awesome-phonenumber'
```
```js
PhoneNumber( '+46707123456' ).getRegionCode( ); // -> 'SE'
### parsePhoneNumber
`parsePhoneNumber( phoneNumber, regionCode )` creates a PhoneNumber instance.
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 `regionCode` is required, e.g. 'SE' for Sweden, 'CH' for Switzerland, etc.
The return is an instance of the PhoneNumber class, with the methods:
```ts
class PhoneNumber {
isValid( ): boolean;
canBeInternationallyDialled( ): boolean;
isPossible( ): boolean;
// any of the "Phone number types" defined above
getType( ): PhoneNumberTypes;
// true if type is 'mobile' or 'fixed-line-or-mobile'
isMobile( ): boolean;
// true if type is 'fixed-line' or 'fixed-line-or-mobile'
isFixedLine( ): boolean;
getNumber( type?: PhoneNumberFormat ): string;
// Formatted number when calling from regionCode
getNumberFrom( regionCode: string ): string;
getRegionCode( ): string;
getCountryCode( ): number;
// JSON blob output as seen in "Basic usage" above
toJSON( ): any;
}
```
#### getNumberFrom
```ts
// Calling the Swedish number 0707123456 from Japan:
parsePhoneNumber( '0707123456', 'SE' ).getNumberFrom( 'JP' );
// -> '010 46 70 712 34 56'
```
#### Example
```ts
import { parsePhoneNumber } from 'awesome-phonenumber'
const pn = parsePhoneNumber( '+46707123456' );
pn.getRegionCode( ); // -> 'SE'
```
```ts
import { parsePhoneNumber } from 'awesome-phonenumber'
const pn = parsePhoneNumber( '0707123456', 'SE' );
```
## API types

@@ -90,3 +166,3 @@

### <a name="phone-number-types"></a>Phone number types
```js
```ts
'fixed-line'

@@ -107,3 +183,3 @@ 'fixed-line-or-mobile'

```js
```ts
'is-possible'

@@ -118,3 +194,3 @@ 'invalid-country-code'

```js
```ts
'international'

@@ -127,23 +203,24 @@ 'national'

## API functions
### Library
```js
var PhoneNumber = require( 'awesome-phonenumber' );
```
## Country codes
### 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.
```js
PhoneNumber.getCountryCodeForRegionCode( regionCode ); // -> countryCode
PhoneNumber.getRegionCodeForCountryCode( countryCode ); // -> regionCode
```ts
import {
getCountryCodeForRegionCode,
getRegionCodeForCountryCode,
getSupportedCallingCodes,
getSupportedRegionCodes,
} from 'awesome-phonenumber'
getCountryCodeForRegionCode( regionCode ); // -> countryCode
getRegionCodeForCountryCode( countryCode ); // -> regionCode
```
#### Example
### Example
```js
PhoneNumber.getCountryCodeForRegionCode( 'SE' ); // -> 46
PhoneNumber.getRegionCodeForCountryCode( 46 ); // -> 'SE'
```ts
getCountryCodeForRegionCode( 'SE' ); // -> 46
getRegionCodeForCountryCode( 46 ); // -> 'SE'
```

@@ -153,4 +230,4 @@

```js
PhoneNumber.getSupportedCallingCodes( ); // -> [ calling codes... ]
```ts
getSupportedCallingCodes( ); // -> [ calling codes... ]
```

@@ -160,61 +237,43 @@

```js
PhoneNumber.getSupportedRegionCodes( ); // -> [ region codes... ]
```ts
getSupportedRegionCodes( ); // -> [ region codes... ]
```
### Phone numbers
An instance of the `PhoneNumber` class will be created even if `PhoneNumber` is called as a function.
## <a name="example"></a>Example phone numbers for country
```js
var pn = PhoneNumber( number, regionCode );
// is the same as
var pn = new PhoneNumber( number, regionCode );
```
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.
PhoneNumber objects can also be created using the `getExample( regionCode[, type ] )` function, see section [Example phone numbers for country](#example) below.
```ts
import { getExample } from 'awesome-phonenumber'
```js
pn.toJSON( ); // -> json blob as seen in "Basic usage" above
pn.isValid( ); // -> Boolean
pn.isPossible( ); // -> Boolean
pn.getType( ); // -> Any of the "Phone number types" defined above
pn.isMobile( ); // -> true if type is 'mobile' or 'fixed-line-or-mobile'
pn.isFixedLine( ); // -> true if type is 'fixed-line' or 'fixed-line-or-mobile'
pn.getNumber( [ format ] ); // -> Formatted number, see "Basic usage" for examples
// Returns the number formatted to how to dial it from another region.
pn.getNumberFrom( fromRegionCode );
getExample( regionCode[, phoneNumberType] ); // PhoneNumber object
```
#### Example
The `phoneNumberType` is any of the [types defined above](#phone-number-types).
```js
// Calling the Swedish number 0707123456 from Japan:
PhoneNumber( '0707123456', 'SE' ).getNumberFrom( 'JP' ); // '010 46 70 712 34 56'
```
### Example
### <a name="example"></a>Example phone numbers for country
```ts
import { getExample } from 'awesome-phonenumber'
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.
```js
PhoneNumber.getExample( regionCode[, phoneNumberType] ); // PhoneNumber object
// Get an example Swedish phone number
getExample( 'SE' ).getNumber( ); // '+468123456'
getExample( 'SE', 'mobile' ).getNumber( ); // '+46701234567'
getExample( 'SE', 'mobile' ).getNumber( 'national' ); // '070 123 45 67'
```
The `phoneNumberType` is any of the [types defined above](#phone-number-types).
## As-you-type formatting
#### Example
You can create an `AsYouType` class with `getAsYouType()` to format a phone number as it is being typed.
```js
PhoneNumber.getExample( 'SE' ).getNumber( ); // '+468123456'
PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( ); // '+46701234567'
PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( 'national' ); // '070 123 45 67'
```ts
import { getAsYouType } from 'awesome-phonenumber'
const ayt = getAsYouType( 'SE' );
```
### As-you-type formatting
The returned class instance has the following methods
You can use an `AsYouType` class to format a phone number as it is being typed. An instance of this class is retrieved by `var ayt = PhoneNumber.getAsYouType( regionCode )`, and has the following methods:
```js
```ts
// Add a character to the end of the number

@@ -236,8 +295,10 @@ ayt.addChar( nextChar );

All the functions above except `getPhoneNumber( )` return the current formatted number (as a String of course, as it may include spaces and other characters).
All the functions above except `getPhoneNumber( )` return the current formatted number as a string.
#### Example
```js
var ayt = PhoneNumber.getAsYouType( 'SE' );
```ts
import { getAsYouType } from 'awesome-phonenumber'
const ayt = getAsYouType( 'SE' );
ayt.addChar( '0' ); // -> '0'

@@ -244,0 +305,0 @@ ayt.addChar( '7' ); // -> '07'

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc