format
format(number, numberFormat) is used to formats a phone number in the specified format using default rules.
Usage
Using Standard JavaScript:
const PNF = require('libphonenumbers').PhoneNumberFormat;
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Format number in the RFC3966 format
console.log(phoneUtil.format(number, PNF.RFC3966));
// tel:+1-202-456-2121
// Format number in the national format
console.log(phoneUtil.format(number, PNF.NATIONAL));
// (202) 456-2121
// Format number in the international format
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
// +1 202-456-2121
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
const PNF = libphonenumbers.PhoneNumberFormat;
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Format number in the RFC3966 format
console.log(phoneUtil.format(number, PNF.RFC3966));
// tel:+1-202-456-2121
// Format number in the national format
console.log(phoneUtil.format(number, PNF.NATIONAL));
// (202) 456-2121
// Format number in the international format
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
// +1 202-456-2121
PhoneNumberFormat
The following enums is used to pass to format(number, numberFormat).
Enum | Value |
---|---|
E164 | 0 |
INTERNATIONAL | 1 |
NATIONAL | 2 |
RFC3966 | 3 |