All Projects → paypal → Fullstack Phone

paypal / Fullstack Phone

Licence: apache-2.0
A dual-module phone number system with dynamic regional metadata ☎️

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Fullstack Phone

Phone
With a given country and phone number, validate and reformat the mobile phone number to the E.164 standard. The purpose of this is to allow us to send SMS to mobile phones only.
Stars: ✭ 531 (+598.68%)
Mutual labels:  phone
Prometheusalert
Prometheus Alert是开源的运维告警中心消息转发系统,支持主流的监控系统Prometheus,Zabbix,日志系统Graylog和数据可视化系统Grafana发出的预警消息,支持钉钉,微信,华为云短信,腾讯云短信,腾讯云电话,阿里云短信,阿里云电话等
Stars: ✭ 822 (+981.58%)
Mutual labels:  phone
Authy
Rinvex Authy is a simple wrapper for @Authy TOTP API, the best rated Two-Factor Authentication service for consumers, simplest 2fa Rest API for developers and a strong authentication platform for the enterprise.
Stars: ✭ 34 (-55.26%)
Mutual labels:  phone
Tensorflowandroiddemo
TensorFlow android demo 车道线 车辆 人脸 动作 骨架 识别 检测 抽烟 打电话 闭眼 睁眼
Stars: ✭ 589 (+675%)
Mutual labels:  phone
Linphone Android
Linphone.org mirror for linphone-android (https://gitlab.linphone.org/BC/public/linphone-android)
Stars: ✭ 740 (+873.68%)
Mutual labels:  phone
Flutter adapter
A plugin that adapts the flutter application to different platforms, allowing your flutter application to flexibly and efficiently adapt to various platforms in the same flutter project, maximizing UI multiplexing, and sharing business logic code across different platforms. Support to select different layout styles in real time according to the screen orientation.
Stars: ✭ 27 (-64.47%)
Mutual labels:  phone
Red Moon
Android screen filter app for night time phone use.
Stars: ✭ 476 (+526.32%)
Mutual labels:  phone
Arg.js
🇦🇷 🛠 NPM library. Validation of Argentinian bank account numbers, IDs and phone numbers
Stars: ✭ 52 (-31.58%)
Mutual labels:  phone
Crboxinputview
Verify code input view. Support security type for password.短信验证码输入框,支持密文模式
Stars: ✭ 749 (+885.53%)
Mutual labels:  phone
Xtream Iptv Player Reactnative
Stars: ✭ 30 (-60.53%)
Mutual labels:  phone
Telegram Sms
An SMS-forwarding Robot Running on Your Android Device.
Stars: ✭ 641 (+743.42%)
Mutual labels:  phone
Calendar Phonegap Plugin
📅 Cordova plugin to Create, Change, Delete and Find Events in the native Calendar
Stars: ✭ 729 (+859.21%)
Mutual labels:  phone
Vonage Php Sdk Core
Vonage REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
Stars: ✭ 849 (+1017.11%)
Mutual labels:  phone
Twilio Csharp
Twilio C#/.NET Helper Library for .NET Framework 3.5+ and supported .NET Core versions
Stars: ✭ 541 (+611.84%)
Mutual labels:  phone
Google Libphonenumber
The up-to-date and reliable Google's libphonenumber package for node.js.
Stars: ✭ 984 (+1194.74%)
Mutual labels:  phone
Operative Framework
operative framework is a OSINT investigation framework, you can interact with multiple targets, execute multiple modules, create links with target, export rapport to PDF file, add note to target or results, interact with RESTFul API, write your own modules.
Stars: ✭ 511 (+572.37%)
Mutual labels:  phone
Carton Android
Carton SDK and main sample
Stars: ✭ 7 (-90.79%)
Mutual labels:  phone
Cn Mobile Location
中国手机号码归属地接口
Stars: ✭ 53 (-30.26%)
Mutual labels:  phone
Brazilian Utils
Utils library for specific Brazilian businesses
Stars: ✭ 1,023 (+1246.05%)
Mutual labels:  phone
Yunmai Data Extract
Extract your data from the Yunmai weighing scales cloud API so you can use it elsewhere
Stars: ✭ 21 (-72.37%)
Mutual labels:  phone

fullstack-phone ☎️

Master Develop npm Libphonenumber version
Build Status Build Status npm version v8.12.8

fullstack-phone provides formatting, validation, and parsing of phone numbers per-region. The system is optimized for use as two modules:

  1. a metadata server providing dynamic regional metadata
  2. a lightweight, Closure-compiled phone client (26KB, 9KB gzipped)

This project was extended from Nathan Hammond's project, which itself is an adaptation of Google's libphonenumber library.

Contents

Installation

npm install fullstack-phone

Usage

Demo App: fullstack-phone-demo

The modules are optimized for use in two environments.

On the server (requires Node 4+):

// Node.js:
var phoneServer = require('fullstack-phone/server');

var metadata = phoneServer.loadMeta(['US', 'RU']); // load US and RU phone metadata
// now serve the metadata via some REST API or write it to a file for bundling with client code

On the client (assuming a client-side bundler that provides require, like webpack):

// Browser:
var phoneClient = require('fullstack-phone/client');

// fetch the metadata somehow, then pass to createPhoneHandler to instantiate a handler:
var phoneHandler = phoneClient.createPhoneHandler(metadata);

Once initialized, the phone handler can be used to process phone numbers:

// phone handler functions:

phoneHandler.getSupportedRegions(); // > ['US', 'RU']

phoneHandler.formatPhoneNumber(
  { countryCode: '1', nationalNumber: '5101234567'},
  { style: 'national'}
);
// > '(510) 123-4567'

phoneHandler.parsePhoneNumber('5101234567', 'US');
// > { countryCode: '1', nationalNumber: '5101234567' }

phoneHandler.validatePhoneNumber(
  { countryCode: '1', nationalNumber: '5101234567'},
  'US'
);
// > [Error: PHONE_INVALID_FOR_REGION]

It's also possible to use both within the same environment. Using the server module in the browser, however, nullifies the advantages of the per-region metadata slicing.

Why

Google’s libphonenumber library is the de-facto industry standard for processing international phone numbers, providing support for formatting, validating, and normalizing phone numbers in 250+ regions. However, the default phone metadata is quite heavy. Various custom JS packages have reduced the code & metadata footprint by:

This package fills a different niche by providing:

  • The official libphonenumber code (not a pure JS re-write)
  • A small, static code base that doesn’t change for different regions
  • Pluggable metadata bundles for individual regions

Canonical Phone Object

When using Google libphonenumber directly, processing a phone number requires parsing a string or initializing a protocol buffer phone number object and calling setters for its various properties.

In contrast, fullstack-phone provides a more idiomatic JavaScript phone object, removing the need to call multiple setters. Most of the phone number functions here operate on a canonical phoneObj, as follows:

{
  countryCode : '1',
  nationalNumber : '5105551234',
  extension : '999'
}
  • countryCode

    • Required. A number or string of digits representing a country phone code, e.g., '1'.
  • nationalNumber

    • Required. A number or string of digits representing a phone number, as defined by E.164, e.g., '4085551212'.
    • Note that this excludes the leading national prefix (or "trunk code"), which is 0 or 1 in most territories.
    • Italian leading zeros should be included here.
  • extension

    • Optional. A string representing the phone number extension, e.g., '123'.

Notes

For proper formatting and validation results, nationalNumber and countryCode must only contain digits. In addition, countryCode must be the calling code of a country for which the phone handler was initialized. (For example, if a phoneObj is passed with countryCode: 44, the phone handler must have been loaded with GB metadata for proper results.)

The phoneObj object can be created by calling parsePhoneNumber on a phone number string.

APIs

Server

var phoneServer = require('fullstack-phone/server');

The phone server exposes only one function:

loadMeta

phoneServer.loadMeta(regionCodeArray)

Given an array of two-letter region codes (ISO 3166-1 alpha-2), return a metadata bundle for those regions, to be passed to phoneClient.createPhoneHandler().

Notes:
  • If regionCodeArray is undefined, all regional metadata is returned.
  • loadMeta adds support for the following regions to Google libphonenumber:
    • PN: Pitcairn Islands (copied from New Zealand metadata)
    • AN: Netherlands Antilles (copied from Bonaire, Sint Eustatius and Saba)
  • Some regions depend on metadata of other regions. For example, the Bahamas (BS) shares a telephone country code (1) with the United States (US). Since the US is considered the main region for country code 1, the US metadata must be included to support processing of Bahamas phone numbers. loadMeta takes care of this automatically.

The full list of region codes supported is:

['001', 'AC', 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GT', 'GU', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TA', 'TC', 'TD', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW']

Note that '001' is used to load metadata for global numbers, such as 1-800 numbers.

Examples
var meta = phoneServer.loadMeta(['DE', 'AU']);
// > metadata object for DE and AU

var meta = phoneServer.loadMeta();
// > metadata object for all regions

Client

var phoneClient = require('fullstack-phone/client');

The phone client exposes only one function:

createPhoneHandler

phoneClient.createPhoneHandler(metadata)

Given a metadata bundle from phoneServer.loadMeta(), return a phone handler instantiated for that metadata.

Phone Handler

var phoneHandler = phoneClient.createPhoneHandler(metadata);

The phone handler returned by createPhoneHandler provides the following methods:

Exceptions

Any method that takes a regionCode string can throw the following exception if called with a region code for which the handler has not been initialized:

phoneHandler.getCountryCodeForRegion();
// > Error: Metadata not loaded for region: undefined

phoneHandler.getCountryCodeForRegion('XX');
// > Error: Metadata not loaded for region: XX

getSupportedRegions

phoneHandler.getSupportedRegions()

Return array of supported region codes.

Note that if a dependent region was loaded (such as the Bahamas), the main region for the shared country code is also reported a supported region (e.g., US).

Example
phoneHandler.getSupportedRegions();
// > ['US', 'RU']

countryCodeToRegionCodeMap

phoneHandler.countryCodeToRegionCodeMap()

Return map from country calling codes to array of supported regions.

Example
phoneHandler.countryCodeToRegionCodeMap();
// > { '1': [ 'US', 'BS' ], '7': [ 'RU' ] }

getCountryCodeForRegion

phoneHandler.getCountryCodeForRegion(regionCode)

Given a regionCode (assuming metadata has already been loaded for that region), return its country calling code as a number type.

Example
phoneHandler.getCountryCodeForRegion('RU');
// > 7

formatPhoneNumber

phoneHandler.formatPhoneNumber(phoneObj, options)

Given a phoneObj object and options object, return a formatted phone number as a string.

The options object has a single string property to indicate the formatting style desired:

{
    style: 'national' // or 'international', 'e164', 'rfc3966'
}
Examples
var phone = { countryCode: '1', nationalNumber: '5101234567' };

var options = { style: 'international' };
phoneHandler.formatPhoneNumber(phone, options);
// > '+1 510-123-4567'

options.style = 'national';
phoneHandler.formatPhoneNumber(phone, options);
// > '(510) 123-4567'

options.style = 'e164';
phoneHandler.formatPhoneNumber(phone, options);
// > '+15101234567'

options.style = 'rfc3966';
phoneHandler.formatPhoneNumber(phone, options)
// > 'tel:+1-510-123-4567'

validatePhoneNumber

phoneHandler.validatePhoneNumber(phoneObj, regionCode)

Given a phoneObj object and regionCode string, return an Error object indicating any problems with the phone object (or true if it passed validation).

The possible error messages are:

  • 'PHONE_INVALID_FOR_REGION'
  • 'PHONE_INVALID_COUNTRY_CODE'
  • 'PHONE_NUMBER_TOO_LONG'
  • 'PHONE_NUMBER_TOO_SHORT'
  • 'PHONE_NUMBER_INVALID_LENGTH'
    • Not too long, not too short, but not just right, either. For example, Andorra (AD) numbers are 6, 8, or 8 digits, so a 7-digit number yields this error.
Example
var phone = { countryCode: '1', nationalNumber: '5' };
phoneHandler.validatePhoneNumber(phone, 'US');
// > [Error: PHN_NUMBER_TOO_SHORT]

phone = { countryCode: '1', nationalNumber: '5105261234'};
phoneHandler.validatePhoneNumber(phone, 'US');
// true

parsePhoneNumber

phoneHandler.parsePhoneNumber(phoneNumberToParse, regionCode)

Given string parameters phoneNumberToParse and regionCode, return a phoneObj object or an Error object if the number is invalid.

The possible error messages are:

  • 'PHONE_INVALID_COUNTRY_CODE'
  • 'PHONE_NUMBER_TOO_SHORT'
  • 'PHONE_NUMBER_TOO_LONG'
  • 'PHONE_NOT_A_NUMBER'
  • 'PHONE_TOO_SHORT_AFTER_IDD'
Example
phoneHandler.parsePhoneNumber('5101234567', 'US');
// > { countryCode: '1', nationalNumber: '5101234567' }

phoneHandler.parsePhoneNumber('ABC', 'US');
// > [Error: PHN_NOT_A_NUMBER]

getExampleNumberForType

phoneHandler.getExampleNumberForType(type, regionCode)

Given the string parameters type and regionCode, return a phoneObj object representing an example number for the given type.

The type parameter is an enum based on libphonenumber i18n.phonenumbers.PhoneNumberType and can be any of the following strings:

  • 'FIXED_LINE'
  • 'MOBILE'
  • 'FIXED_LINE_OR_MOBILE'
  • 'TOLL_FREE'
  • 'PREMIUM_RATE'
  • 'SHARED_COST'
  • 'VOIP'
  • 'PERSONAL_NUMBER'
  • 'PAGER'
  • 'UAN'
  • 'VOICEMAIL'
  • 'UNKNOWN'
Example
phoneHandler.getExampleNumberForType('MOBILE', 'US');
// > { countryCode: '1', nationalNumber: '2015550123' }

getAsYouTypeFormatter

phoneHandler.getAsYouTypeFormatter(regionCode)

Return a new AsYouTypeFormatter object instantiated for the given regionCode.

Example
var formatter = phoneHandler.getAsYouTypeFormatter('GB');
// > AsYouTypeFormatter object initialized to Great Britain

AsYouTypeFormatter methods

The initialized AsYouTypeFormatter object itself exposes the following methods:

inputDigit
formatter.inputDigit(digit)

Given a digit (number or string), output the phone number formatted thus far, given the history of inputted digits.

Note that digit can also be '+' or '*'

Example
formatter.inputDigit('5'); // > '5'
formatter.inputDigit('1'); // > '51'
formatter.inputDigit('0'); // > '510'
formatter.inputDigit('1'); // > '510-1'
formatter.inputDigit('2'); // > '510-12'
formatter.inputDigit('3'); // > '510-123'
formatter.inputDigit('4'); // > '510-1234'
formatter.inputDigit('5'); // > '(510) 123-45'
formatter.inputDigit('6'); // > '(510) 123-456'
formatter.inputDigit('7'); // > '(510) 123-4567'
clear
formatter.clear()

Clear the formatter state.

Example
formatter.inputDigit('5'); // > '5'
formatter.inputDigit('1'); // > '51'
formatter.inputDigit('0'); // > '510'
formatter.inputDigit('1'); // > '510-1'
formatter.inputDigit('2'); // > '510-12'
formatter.clear();
formatter.inputDigit('9'); // > '9'
formatter.inputDigit('1'); // > '91'
formatter.inputDigit('9'); // > '919'
formatter.inputDigit('4'); // > '919-4'
formatter.inputDigit('8'); // > '919-48'
...

Development

see DEVELOPMENT.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].