开源软件名称(OpenSource Name):AfterShip/phone开源软件地址(OpenSource Url):https://github.com/AfterShip/phone开源编程语言(OpenSource Language):TypeScript 57.8%开源软件介绍(OpenSource Introduction):Phone ·What is phone?
A common problem is that users normally input phone numbers in this way:
We always want:
Install
Usageconst {phone} = require('phone');
// or
import {phone} from 'phone'; 1. Simple usagephone('+852 6569-8900');
// { isValid: true, phoneNumber: '+85265698900', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' } 2. With Countryphone('+1(817) 569-8900', {country: ''});
// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1'}
phone('(817) 569-8900', {country: 'USA'});
// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1'}
phone('(817) 569-8900', {country: 'HKG'});
// { isValid: false }
// not a valid HKG mobile phone number
phone('+1(817) 569-8900', {country: 'HKG'});
// { isValid: false }
// not a valid HKG mobile phone number
phone('6123-6123', {country: 'HKG'});
// { isValid: true, phoneNumber: '+85261236123', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' } 3. Without country code and no phone prefixIf both country code and country phone prefix are not provided, the phone number will be treated as USA or Canada by default. phone('(817) 569-8900');
// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1' }
phone('(817) 569-8900', {country: ''});
// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1' }
phone('780-569-8900', {country: null});
// { isValid: true, phoneNumber: '+17805698900', countryIso2: 'CA', countryIso3: 'CAN', countryCode: '+1' }
// 780 is a Canada phone prefix
phone('6123-6123', {country: null});
// { isValid: false }
// as default country is USA / CAN and the phone number does not fit such countries' rules
4. With country code / phone prefix, but no |
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
phoneNumber | String | Yes | - | The phone number text you want to process |
country | String | No | null | Provided country code in iso-3166 alpha 2 or 3 format |
validateMobilePrefix | Boolean | No | true | Set to false if you want to skip phone number initial digit checking |
strictDetection | Boolean | No | false | Set to true if you want to disable trunk code detection logic. |
type PhoneResult = PhoneInvalidResult | PhoneValidResult;
interface PhoneValidResult {
isValid: true;
phoneNumber: string;
countryIso2: string;
countryIso3: string;
countryCode: string;
}
interface PhoneInvalidResult {
isValid: false;
phoneNumber: null;
countryIso2: null;
countryIso3: null;
countryCode: null;
}
Parameter | Type | Description |
---|---|---|
isValid | Boolean | To indicate if the result valid |
phoneNumber | String or null | Normalized phone number in E.164 format |
countryIso2 | String or null | Detected phone number country code in iso-3166 alpha 2 format |
countryIso3 | String or null | Detected phone number country code in iso-3166 alpha 3 format |
countryCode | String or null | Detected phone number country calling code with + sign |
yarn test
yarn start:example
or
yarn dev
And then visit http://localhost:8080
yarn build
Does phone
do any logical validation?
Yes. If you provide country
, and the phone number does not start with +
sign,
the module will validate phone_number_lengths
and mobile_begin_with
Why is phone
returning an invalid result for a valid phone number?
By default, the function will validate a mobile phone number only, to validate a landline phone number, please set validateMobilePrefix
to false
.
If you find the result is still incorrect, please submit a ticket to improve our validation rules.
Why is phone
returning an object with isValid = false
instead of returning a null directly?
It reserves the flexibility to extend the response interface for invalid result in future.
The interface of v3 has been changed for better usability, maintainability and flexibility, this shows all the changes from v2:
Version | Interface |
---|---|
v2 | phone(phoneNumber, country, allowLandline) |
v3 | phone(phoneNumber,{country: String, validateMobilePrefix: Boolean, strictDetection: Boolean}) |
Version | Result | Interface |
---|---|---|
v2 | - | [phoneNumber, country] |
v3 | Valid | {isValid: true, phoneNumber: string, countryIso2: string, countryIso3: string, countryCode: string} |
v3 | Invalid | {isValid: false, phoneNumber: null, countryIso2: null, countryIso3: null, countryCode: null} |
allowLandline
in v2 is essentially equals to validateMobilePrefix
in v3, however, the value is opposite.
Because allowLandline = true
in v2 means "Skip the mobile phone number prefix validation", and there is NO capability to verify if the input phone number is a valid landline phone number.
To avoid the misleading information, the parameter name has been changed to validateMobilePrefix
, and the input value is opposite, while validateMobilePrefix = false
means "Skip the mobile phone number prefix validation".
We've tried to make sure that this package works for as many cases as possible, if you notice that we have an incorrect rule for a country or other case, please open an issue to let us know.
For creating new pull requests regarding add or modify phone number formats, please include the reference information such as PDFs, websites, etc. Thank you very much.
This project is licensed under the MIT license.
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论