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).

EnumValue
E1640
INTERNATIONAL1
NATIONAL2
RFC39663