Socket
Socket
Sign inDemoInstall

libphonenumber-js

Package Overview
Dependencies
Maintainers
1
Versions
392
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libphonenumber-js - npm Package Compare versions

Comparing version 1.5.2 to 1.6.0

build/getExampleNumber.js

2

build/AsYouType.js

@@ -561,3 +561,3 @@ 'use strict';

value: function extract_country_calling_code() {
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(this.parsed_input, this.default_country, this.metadata),
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(this.parsed_input, this.default_country, this.metadata.metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,

@@ -564,0 +564,0 @@ number = _extractCountryCallin.number;

@@ -14,2 +14,6 @@ 'use strict';

var _metadata = require('./metadata');
var _metadata2 = _interopRequireDefault(_metadata);
var _parseIncompletePhoneNumber = require('./parseIncompletePhoneNumber');

@@ -122,3 +126,3 @@

// to a proper international phone number.
var numberWithoutIDD = (0, _IDD.stripIDDPrefix)(number, country, metadata.metadata);
var numberWithoutIDD = (0, _IDD.stripIDDPrefix)(number, country, metadata);

@@ -140,2 +144,4 @@ // If an IDD prefix was stripped then

metadata = new _metadata2.default(metadata);
// The thing with country phone codes

@@ -142,0 +148,0 @@ // is that they are orthogonal to each other

@@ -10,2 +10,4 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

@@ -249,3 +251,3 @@

// `findNumbers('88005553535', 'RU', [options], metadata)`.
if (typeof arg_2 === 'string') {
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {

@@ -252,0 +254,0 @@ options = _extends({ defaultCountry: arg_2 }, arg_3);

@@ -23,2 +23,6 @@ 'use strict';

var _parse = require('./parse');
var _parse2 = _interopRequireDefault(_parse);
var _IDD = require('./IDD');

@@ -44,8 +48,8 @@

// ```js
// format('8005553535', 'RU', 'International')
// format('8005553535', 'RU', 'International', metadata)
// format({ phone: '8005553535', country: 'RU' }, 'International')
// format({ phone: '8005553535', country: 'RU' }, 'International', metadata)
// format('+78005553535', 'National')
// format('+78005553535', 'National', metadata)
// format('8005553535', 'RU', 'INTERNATIONAL')
// format('8005553535', 'RU', 'INTERNATIONAL', metadata)
// format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL')
// format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', metadata)
// format('+78005553535', 'NATIONAL')
// format('+78005553535', 'NATIONAL', metadata)
// ```

@@ -60,34 +64,29 @@ //

if (input.country && metadata.hasCountry(input.country)) {
if (input.country) {
// Validate `input.country`.
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
metadata.country(input.country);
}
} else if (input.countryCallingCode) {
metadata.chooseCountryByCountryCallingCode(input.countryCallingCode);
} else return input.phone || '';
// `number` is a national (significant) number in this case.
var countryCallingCode = metadata.countryCallingCode();
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(input.phone, null, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
number = _extractCountryCallin.number;
var nationalNumber = options.v2 ? input.nationalNumber : input.phone;
countryCallingCode = countryCallingCode || input.countryCallingCode;
// This variable should have been declared inside `case`s
// but Babel has a bug and it says "duplicate variable declaration".
var number = void 0;
if (countryCallingCode) {
// Check country restriction
if (input.country && metadata.selectedCountry() && countryCallingCode !== metadata.countryCallingCode()) {
return input.phone;
}
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
}
if (!metadata.selectedCountry()) {
return input.phone;
}
switch (format_type) {
case 'International':
if (!number) {
return '+' + metadata.countryCallingCode();
case 'INTERNATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return '+' + countryCallingCode;
}
number = format_national_number(number, 'International', false, metadata);
number = '+' + metadata.countryCallingCode() + ' ' + number;
number = format_national_number(nationalNumber, 'INTERNATIONAL', false, metadata);
number = '+' + countryCallingCode + ' ' + number;
return add_extension(number, input.ext, metadata, options.formatExtension);

@@ -97,7 +96,7 @@

// `E.164` doesn't define "phone number extensions".
return '+' + metadata.countryCallingCode() + input.phone;
return '+' + countryCallingCode + nationalNumber;
case 'RFC3966':
return (0, _RFC.formatRFC3966)({
number: '+' + metadata.countryCallingCode() + input.phone,
number: '+' + countryCallingCode + nationalNumber,
ext: input.ext

@@ -116,17 +115,19 @@ });

if (options.humanReadable) {
var formattedForSameCountryCallingCode = countryCallingCode && formatIDDSameCountryCallingCodeNumber(number, countryCallingCode, options.fromCountry, metadata);
var formattedForSameCountryCallingCode = countryCallingCode && formatIDDSameCountryCallingCodeNumber(nationalNumber, metadata.countryCallingCode(), options.fromCountry, metadata);
if (formattedForSameCountryCallingCode) {
number = formattedForSameCountryCallingCode;
} else {
number = IDDPrefix + ' ' + metadata.countryCallingCode() + ' ' + format_national_number(number, 'International', false, metadata);
number = IDDPrefix + ' ' + countryCallingCode + ' ' + format_national_number(nationalNumber, 'INTERNATIONAL', false, metadata);
}
return add_extension(number, input.ext, metadata, options.formatExtension);
}
return '' + IDDPrefix + metadata.countryCallingCode() + number;
return '' + IDDPrefix + countryCallingCode + nationalNumber;
case 'National':
if (!number) {
case 'NATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return '';
}
number = format_national_number(number, 'National', true, metadata);
number = format_national_number(nationalNumber, 'NATIONAL', true, metadata);
return add_extension(number, input.ext, metadata, options.formatExtension);

@@ -171,3 +172,3 @@ }

return format_national_number_using_format(number, format, format_as === 'International', enforce_national_prefix, metadata);
return format_national_number_using_format(number, format, format_as === 'INTERNATIONAL', enforce_national_prefix, metadata);
}

@@ -229,10 +230,4 @@

// If country code is supplied.
// `format('8005553535', 'RU', 'National', [options], metadata)`.
// `format('8005553535', 'RU', 'NATIONAL', [options], metadata)`.
if (typeof arg_3 === 'string') {
// Will be `parse()`d later in code
input = {
phone: arg_1,
country: arg_2
};
format_type = arg_3;

@@ -246,11 +241,8 @@

}
input = (0, _parse2.default)(arg_1, { defaultCountry: arg_2, extended: true }, metadata);
}
// Just an international phone number is supplied
// `format('+78005553535', 'National', [options], metadata)`.
// `format('+78005553535', 'NATIONAL', [options], metadata)`.
else {
// Will be `parse()`d later in code
input = {
phone: arg_1
};
if (typeof arg_2 !== 'string') {

@@ -268,7 +260,9 @@ throw new Error('`format` argument not passed to `formatNumber(number, format)`');

}
input = (0, _parse2.default)(arg_1, { extended: true }, metadata);
}
}
// If the phone number is passed as a parsed number object.
// `format({ phone: '8005553535', country: 'RU' }, 'National', [options], metadata)`.
else if (is_object(arg_1) && typeof arg_1.phone === 'string') {
// `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
else if (is_object(arg_1)) {
input = arg_1;

@@ -285,7 +279,13 @@ format_type = arg_2;

if (format_type === 'International') {
format_type = 'INTERNATIONAL';
} else if (format_type === 'National') {
format_type = 'NATIONAL';
}
// Validate `format_type`.
switch (format_type) {
case 'International':
case 'E.164':
case 'National':
case 'INTERNATIONAL':
case 'NATIONAL':
case 'RFC3966':

@@ -328,3 +328,3 @@ case 'IDD':

if (toCountryCallingCode === '1') {
return toCountryCallingCode + ' ' + format_national_number(number, 'National', false, toCountryMetadata);
return toCountryCallingCode + ' ' + format_national_number(number, 'NATIONAL', false, toCountryMetadata);
}

@@ -340,5 +340,5 @@

// http://www.petitfute.com/voyage/225-info-pratiques-reunion
return format_national_number(number, 'National', false, toCountryMetadata);
return format_national_number(number, 'NATIONAL', false, toCountryMetadata);
}
}
//# sourceMappingURL=format.js.map

@@ -66,5 +66,5 @@ 'use strict';

if (can_be_internationally_dialled(number) && (0, _getNumberType.check_number_length_for_type)(number.phone, undefined, metadata) !== 'TOO_SHORT') {
formatted_number = (0, _format2.default)(number, 'International', metadata.metadata);
formatted_number = (0, _format2.default)(number, 'INTERNATIONAL', metadata.metadata);
} else {
formatted_number = (0, _format2.default)(number, 'National', metadata.metadata);
formatted_number = (0, _format2.default)(number, 'NATIONAL', metadata.metadata);
}

@@ -97,5 +97,5 @@ } else {

(country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile) && can_be_internationally_dialled(number)) {
formatted_number = (0, _format2.default)(number, 'International');
formatted_number = (0, _format2.default)(number, 'INTERNATIONAL');
} else {
formatted_number = (0, _format2.default)(number, 'National');
formatted_number = (0, _format2.default)(number, 'NATIONAL');
}

@@ -107,3 +107,3 @@ }

// if it cannot be internationally dialled.
return with_formatting ? (0, _format2.default)(number, 'International', metadata.metadata) : (0, _format2.default)(number, 'E.164', metadata.metadata);
return with_formatting ? (0, _format2.default)(number, 'INTERNATIONAL', metadata.metadata) : (0, _format2.default)(number, 'E.164', metadata.metadata);
}

@@ -110,0 +110,0 @@

@@ -30,16 +30,12 @@ 'use strict';

// Finds out national phone number type (fixed line, mobile, etc)
function get_number_type(arg_1, arg_2, arg_3) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3),
function get_number_type(arg_1, arg_2, arg_3, arg_4) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3, arg_4),
input = _sort_out_arguments.input,
options = _sort_out_arguments.options,
metadata = _sort_out_arguments.metadata;
// When no input was passed
// When `parse()` returned `{}`
// meaning that the phone number is not a valid one.
if (!input) {
return;
}
// When `parse()` returned `{}`
// meaning that the phone number is not a valid one.
if (!input.country) {

@@ -53,3 +49,3 @@ return;

var national_number = input.phone;
var nationalNumber = options.v2 ? input.nationalNumber : input.phone;
metadata.country(input.country);

@@ -61,3 +57,3 @@

// Is this national number even valid for this country
if (!(0, _common.matches_entirely)(national_number, metadata.nationalNumberPattern())) {
if (!(0, _common.matches_entirely)(nationalNumber, metadata.nationalNumberPattern())) {
return;

@@ -67,3 +63,3 @@ }

// Is it fixed line number
if (is_of_type(national_number, 'FIXED_LINE', metadata)) {
if (is_of_type(nationalNumber, 'FIXED_LINE', metadata)) {
// Because duplicate regular expressions are removed

@@ -87,3 +83,3 @@ // to reduce metadata size, if "mobile" pattern is ""

/* istanbul ignore if */
if (is_of_type(national_number, 'MOBILE', metadata)) {
if (is_of_type(nationalNumber, 'MOBILE', metadata)) {
return 'FIXED_LINE_OR_MOBILE';

@@ -109,3 +105,3 @@ }

if (is_of_type(national_number, _type, metadata)) {
if (is_of_type(nationalNumber, _type, metadata)) {
return _type;

@@ -116,3 +112,3 @@ }

function is_of_type(national_number, type, metadata) {
function is_of_type(nationalNumber, type, metadata) {
type = metadata.type(type);

@@ -130,12 +126,13 @@

// already checked before a specific number type.
if (type.possibleLengths() && type.possibleLengths().indexOf(national_number.length) < 0) {
if (type.possibleLengths() && type.possibleLengths().indexOf(nationalNumber.length) < 0) {
return false;
}
return (0, _common.matches_entirely)(national_number, type.pattern());
return (0, _common.matches_entirely)(nationalNumber, type.pattern());
}
// Sort out arguments
function sort_out_arguments(arg_1, arg_2, arg_3) {
function sort_out_arguments(arg_1, arg_2, arg_3, arg_4) {
var input = void 0;
var options = {};
var metadata = void 0;

@@ -146,7 +143,12 @@

if (typeof arg_1 === 'string') {
// If "resrict country" argument is being passed
// If "default country" argument is being passed
// then convert it to an `options` object.
// `getNumberType('88005553535', 'RU', metadata)`.
if (typeof arg_2 === 'string' || arg_2 === undefined) {
metadata = arg_3;
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {
options = arg_3;
metadata = arg_4;
} else {
metadata = arg_3;
}

@@ -160,2 +162,4 @@ // `parse` extracts phone numbers from raw text,

input = (0, _parse2.default)(arg_1, arg_2, metadata);
} else {
input = {};
}

@@ -167,3 +171,8 @@ }

else {
metadata = arg_2;
if (arg_3) {
options = arg_2;
metadata = arg_3;
} else {
metadata = arg_2;
}

@@ -177,2 +186,4 @@ // `parse` extracts phone numbers from raw text,

input = (0, _parse2.default)(arg_1, metadata);
} else {
input = {};
}

@@ -183,17 +194,18 @@ }

// `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`.
else if (is_object(arg_1) && typeof arg_1.phone === 'string') {
// The `arg_1` must be a valid phone number
// as a whole, not just a part of it which gets parsed here.
if ((0, _parse.is_viable_phone_number)(arg_1.phone)) {
input = arg_1;
else if (is_object(arg_1)) {
input = arg_1;
if (arg_3) {
options = arg_2;
metadata = arg_3;
} else {
metadata = arg_2;
}
metadata = arg_2;
} else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.');
return { input: input, metadata: new _metadata2.default(metadata) };
return { input: input, options: options, metadata: new _metadata2.default(metadata) };
}
// Should only be called for the "new" metadata which has "possible lengths".
function check_number_length_for_type(national_number, type, metadata) {
function check_number_length_for_type(nationalNumber, type, metadata) {
var type_info = metadata.type(type);

@@ -217,3 +229,3 @@

// (true for some non-geographical entities), so we just check mobile.
return test_number_length_for_type(national_number, 'MOBILE', metadata);
return check_number_length_for_type(nationalNumber, 'MOBILE', metadata);
}

@@ -249,7 +261,8 @@

var actual_length = national_number.length;
var actual_length = nationalNumber.length;
// In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
// // This is safe because there is never an overlap beween the possible lengths
// // and the local-only lengths; this is checked at build time.
// if (local_lengths && local_lengths.indexOf(national_number.length) >= 0)
// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
// {

@@ -256,0 +269,0 @@ // return 'IS_POSSIBLE_LOCAL_ONLY'

@@ -67,2 +67,7 @@ 'use strict';

}, {
key: 'getDefaultCountryMetadataForRegion',
value: function getDefaultCountryMetadataForRegion() {
return this.metadata.countries[this.countryCallingCodes()[this.countryCallingCode()][0]];
}
}, {
key: 'countryCallingCode',

@@ -97,2 +102,12 @@ value: function countryCallingCode() {

}, {
key: '_getFormats',
value: function _getFormats(country_metadata) {
return country_metadata[this.v1 ? 2 : this.v2 ? 3 : 4];
}
// For countries of the same region (e.g. NANPA)
// formats are all stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'formats',

@@ -102,3 +117,3 @@ value: function formats() {

var formats = this.country_metadata[this.v1 ? 2 : this.v2 ? 3 : 4] || [];
var formats = this._getFormats(this.country_metadata) || this._getFormats(this.getDefaultCountryMetadataForRegion()) || [];
return formats.map(function (_) {

@@ -114,5 +129,15 @@ return new Format(_, _this);

}, {
key: '_getNationalPrefixFormattingRule',
value: function _getNationalPrefixFormattingRule(country_metadata) {
return country_metadata[this.v1 ? 4 : this.v2 ? 5 : 6];
}
// For countries of the same region (e.g. NANPA)
// national prefix formatting rule is stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'nationalPrefixFormattingRule',
value: function nationalPrefixFormattingRule() {
return this.country_metadata[this.v1 ? 4 : this.v2 ? 5 : 6];
return this._getNationalPrefixFormattingRule(this.country_metadata) || this._getNationalPrefixFormattingRule(this.getDefaultCountryMetadataForRegion());
}

@@ -132,5 +157,16 @@ }, {

}, {
key: '_getNationalPrefixIsOptionalWhenFormatting',
value: function _getNationalPrefixIsOptionalWhenFormatting() {
return !!this.country_metadata[this.v1 ? 7 : this.v2 ? 8 : 9];
}
// For countries of the same region (e.g. NANPA)
// "national prefix is optional when parsing" flag is
// stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'nationalPrefixIsOptionalWhenFormatting',
value: function nationalPrefixIsOptionalWhenFormatting() {
return !!this.country_metadata[this.v1 ? 7 : this.v2 ? 8 : 9];
return this._getNationalPrefixIsOptionalWhenFormatting(this.country_metadata) || this._getNationalPrefixIsOptionalWhenFormatting(this.getDefaultCountryMetadataForRegion());
}

@@ -137,0 +173,0 @@ }, {

@@ -7,3 +7,5 @@ 'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; // This is a port of Google Android `libphonenumber`'s
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; // This is a port of Google Android `libphonenumber`'s
// `phonenumberutil.js` of 17th November, 2016.

@@ -37,4 +39,10 @@ //

var _isPossibleNumber = require('./isPossibleNumber');
var _RFC = require('./RFC3966');
var _PhoneNumber = require('./PhoneNumber');
var _PhoneNumber2 = _interopRequireDefault(_PhoneNumber);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -156,2 +164,5 @@

if (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {
if (options.v2) {
throw new Error('INVALID_COUNTRY');
}
throw new Error('Unknown country: ' + options.defaultCountry);

@@ -162,3 +173,3 @@ }

var _parse_input = parse_input(text),
var _parse_input = parse_input(text, options.v2),
formatted_phone_number = _parse_input.number,

@@ -171,2 +182,5 @@ ext = _parse_input.ext;

if (!formatted_phone_number) {
if (options.v2) {
throw new Error('NOT_A_NUMBER');
}
return {};

@@ -177,3 +191,3 @@ }

country = _parse_phone_number.country,
national_number = _parse_phone_number.national_number,
nationalNumber = _parse_phone_number.national_number,
countryCallingCode = _parse_phone_number.countryCallingCode,

@@ -183,2 +197,5 @@ carrierCode = _parse_phone_number.carrierCode;

if (!metadata.selectedCountry()) {
if (options.v2) {
throw new Error('INVALID_COUNTRY');
}
return {};

@@ -188,2 +205,13 @@ }

// Validate national (significant) number length.
if (nationalNumber.length < MIN_LENGTH_FOR_NSN) {
// Won't throw here because the regexp already demands length > 1.
/* istanbul ignore if */
if (options.v2) {
throw new Error('TOO_SHORT');
}
// Google's demo just throws an error in this case.
return {};
}
// Validate national (significant) number length.
//

@@ -197,3 +225,6 @@ // A sidenote:

//
if (national_number.length < MIN_LENGTH_FOR_NSN || national_number.length > _common.MAX_LENGTH_FOR_NSN) {
if (nationalNumber.length > _common.MAX_LENGTH_FOR_NSN) {
if (options.v2) {
throw new Error('TOO_LONG');
}
// Google's demo just throws an error in this case.

@@ -203,9 +234,25 @@ return {};

if (options.v2) {
var phoneNumber = new _PhoneNumber2.default(countryCallingCode, nationalNumber, metadata.metadata);
if (country) {
phoneNumber.country = country;
}
if (carrierCode) {
phoneNumber.carrierCode = carrierCode;
}
if (ext) {
phoneNumber.ext = ext;
}
return phoneNumber;
}
// Check if national phone number pattern matches the number
// National number pattern is different for each country,
// even for those ones which are part of the "NANPA" group.
var valid = country && (0, _common.matches_entirely)(national_number, metadata.nationalNumberPattern()) ? true : false;
var valid = country && (0, _common.matches_entirely)(nationalNumber, metadata.nationalNumberPattern()) ? true : false;
if (!options.extended) {
return valid ? result(country, national_number, ext) : {};
return valid ? result(country, nationalNumber, ext) : {};
}

@@ -218,4 +265,4 @@

valid: valid,
possible: valid ? true : options.extended === true && metadata.possibleLengths() && is_possible_number(national_number, countryCallingCode !== undefined, metadata),
phone: national_number,
possible: valid ? true : options.extended === true && metadata.possibleLengths() && (0, _isPossibleNumber.is_possible_number)(nationalNumber, countryCallingCode !== undefined, metadata),
phone: nationalNumber,
ext: ext

@@ -241,7 +288,14 @@ };

*/
function extract_formatted_phone_number(text) {
if (!text || text.length > MAX_INPUT_STRING_LENGTH) {
function extract_formatted_phone_number(text, v2) {
if (!text) {
return;
}
if (text.length > MAX_INPUT_STRING_LENGTH) {
if (v2) {
throw new Error('TOO_LONG');
}
return;
}
// Attempt to extract a possible number from the string passed in

@@ -404,3 +458,3 @@

// `parse('88005553535', 'RU', [options], metadata)`.
if (typeof arg_2 === 'string') {
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {

@@ -466,13 +520,2 @@ options = _extends({ defaultCountry: arg_2 }, arg_3);

function is_possible_number(national_number, is_international, metadata) {
switch ((0, _getNumberType.check_number_length_for_type)(national_number, undefined, metadata)) {
case 'IS_POSSIBLE':
return true;
// case 'IS_POSSIBLE_LOCAL_ONLY':
// return !is_international
default:
return false;
}
}
/**

@@ -482,3 +525,3 @@ * @param {string} text - Input.

*/
function parse_input(text) {
function parse_input(text, v2) {
// Parse RFC 3966 phone number URI.

@@ -489,3 +532,3 @@ if (text && text.indexOf('tel:') === 0) {

var number = extract_formatted_phone_number(text);
var number = extract_formatted_phone_number(text, v2);

@@ -528,3 +571,3 @@ // If the phone number is not viable, then abort.

function parse_phone_number(formatted_phone_number, default_country, metadata) {
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(formatted_phone_number, default_country, metadata),
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(formatted_phone_number, default_country, metadata.metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,

@@ -531,0 +574,0 @@ number = _extractCountryCallin.number;

@@ -15,2 +15,6 @@ 'use strict';

var _PhoneNumber = require('./PhoneNumber');
var _PhoneNumber2 = _interopRequireDefault(_PhoneNumber);
var _common = require('./common');

@@ -245,2 +249,13 @@

if (match) {
if (this.options.v2) {
var phoneNumber = new _PhoneNumber2.default(match.country, match.phone, this.metadata.metadata);
if (match.ext) {
phoneNumber.ext = match.ext;
}
return {
startsAt: match.startsAt,
endsAt: match.endsAt,
number: phoneNumber
};
}
return match;

@@ -247,0 +262,0 @@ }

@@ -30,3 +30,3 @@ "use strict";

country.formats.map(function (format) {
country.formats && country.formats.map(function (format) {
// When changing this array also change getters in `./metadata.js`

@@ -33,0 +33,0 @@ var format_array = [format.pattern, format.format, format.leading_digits_patterns, format.national_prefix_formatting_rule, format.national_prefix_is_optional_when_formatting, format.international_format];

@@ -213,3 +213,4 @@ 'use strict';

// Some countries don't have `availableFormats` specified,
// because those formats are inherited from the "main country for region".
// because those formats are inherited from the "main country for region":
// all non-"main" countries inherit their formats from the "main" country for that region.
if (territory.availableFormats) {

@@ -300,4 +301,9 @@ country.formats = territory.availableFormats[0].numberFormat.map(function (number_format) {

// Some countries don't have `availableFormats` specified,
// because those formats are meant to be copied
// from the "main country for region".
// because those formats are meant to be copied from the "main country for region":
// all non-"main" countries inherit their formats from the "main" country for that region.
// If that's the case then `nationalPrefixFormattingRule` and
// `nationalPrefixOptionalWhenFormatting` are also copied from the "main" region.
// `nationalPrefix` itself though seems to be always present
// even if it's the same for the "main" region.
// Examples: "RU" and "KZ", "US" and "CA".

@@ -322,11 +328,21 @@

var main_country_for_region_code = country_calling_code_to_countries[_country.phone_code][0];
var main_country_for_region = countries[main_country_for_region_code];
_country.formats = main_country_for_region.formats;
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats
// and phone numbers are formatted as a block in those countries.
if (!_country.formats) {
_country.formats = [];
if (main_country_for_region_code === _country_code) {
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats defined
// and phone numbers are not formatted in those countries.
if (!_country.formats) {
_country.formats = [];
}
} else {
if (_country.formats) {
throw new Error('Country "' + _country_code + '" is supposed to inherit formats from "' + main_country_for_region_code + '" but has its own formats defined.');
}
if (_country.nationalPrefixFormattingRule) {
throw new Error('Country "' + _country_code + '" is supposed to inherit "nationalPrefixFormattingRule" from "' + main_country_for_region_code + '" but has its own "nationalPrefixFormattingRule" defined.');
}
if (_country.nationalPrefixIsOptionalWhenFormatting) {
throw new Error('Country "' + _country_code + '" is supposed to inherit "nationalPrefixIsOptionalWhenFormatting" from "' + main_country_for_region_code + '" but has its own "nationalPrefixIsOptionalWhenFormatting" defined.');
}
}

@@ -333,0 +349,0 @@ }

@@ -16,2 +16,4 @@ 'use strict';

var _common = require('./common');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -48,11 +50,12 @@

*/
function isValidNumber(arg_1, arg_2, arg_3) {
var _sort_out_arguments = (0, _getNumberType.sort_out_arguments)(arg_1, arg_2, arg_3),
function isValidNumber(arg_1, arg_2, arg_3, arg_4) {
var _sort_out_arguments = (0, _getNumberType.sort_out_arguments)(arg_1, arg_2, arg_3, arg_4),
input = _sort_out_arguments.input,
options = _sort_out_arguments.options,
metadata = _sort_out_arguments.metadata;
if (!input) {
return false;
}
// This is just to support `isValidNumber({})`
// for cases when `parseNumber()` returns `{}`.
if (!input.country) {

@@ -68,8 +71,13 @@ return false;

// By default, countries only have type regexps when it's required for
// distinguishing different countries having the same `countryCallingCode`.
if (metadata.hasTypes()) {
return (0, _getNumberType2.default)(input, metadata.metadata) !== undefined;
return (0, _getNumberType2.default)(input, options, metadata.metadata) !== undefined;
}
return true;
// If there are no type regexps for this country in metadata then use
// `nationalNumberPattern` as a "better than nothing" replacement.
var national_number = options.v2 ? input.nationalNumber : input.phone;
return (0, _common.matches_entirely)(national_number, metadata.nationalNumberPattern());
}
//# sourceMappingURL=validate.js.map

@@ -11,2 +11,53 @@ <!-- (breaking change) `parseNumber()` is now `extended: true` by default (and the `extended` flag is no longer supported) meaning that by default it will parse all even remotely hypothetical phone numbers (this was decided to be the primary use case for this function, and it's how Google's libphonenumber does it). Pass `strict: true` flag for the old "only parse valid numbers" behaviour. -->

<!-- (breaking change) Better API.
https://github.com/catamphetamine/libphonenumber-js/issues/259
Maybe something like:
const number = parseNumber(value)
const {
number,
country,
nationalNumber,
carrierCode,
countryCallingCode,
valid,
possible
} = parseNumber(value, { extended: true })
// Maybe rename "extended" to something else.
-->
<!-- (breaking change)
`phone` property of phone number object renamed to `nationalNumber`
-->
<!-- (breaking change) `examples.mobile.json` now provides phone numbers in E.164 format (e.g. `+78005553535`). Previously those numbers were in "national (significant) number" form (e.g. `8005553535`).
-->
<!-- (breaking change) AsYouType -> AsYouTypeFormatter (maybe, or maybe not). -->
<!-- (breaking change) parse.js -> parseNumber.js, format.js -> formatNumber.js, validate.js -> isValidNumber.js. -->
<!-- (breaking change) `findNumbers()` now returns an array of `PhoneNumber` objects instead of objects having shape `{ country, phone }`. -->
<!-- Update according to the latest commits to `phonenumberutil.js`. -->
1.6.0 / 16.10.2018
==================
* Added `parsePhoneNumber()` function and `PhoneNumber` class.
* Added `v2: true` option to `findNumbers()` function.
* Added `getExampleNumber()` function.
* Added `isPossibleNumber()` function.
* In `formatNumber()` renamed `National` to `NATIONAL` and `International` to `INTERNATIONAL`. The older variants still work but are considered deprecated.
* (metadata file internal format breaking change) (doesn't affect users of this library) If anyone was using metadata files from this library bypassing the library functions (i.e. those who parsed `metadata.min.json` file manually) then there's a new internal optimization introduced in this version: previously `formats` were copy-pasted for each country of the same region (e.g. `NANPA`) while now the `formats` are only defined on the "main" country for region and other countries simply read the `formats` from it at runtime. This reduced the default metadata file size by 5 kilobytes.
1.5.0 / 26.09.2018

@@ -13,0 +64,0 @@ ==================

// `parse()` and `parseCustom` are deprecated.
// Use `fparseNumber()` and `parseNumberCustom()` instead.
import { CountryCallingCode, CountryCode, NumberFormat, NumberFound, NumberType, ParsedNumber, TelephoneNumber } from 'libphonenumber-js';
import { CountryCallingCode, CountryCode, NumberFormat, NumberFound, NumberType, ParsedNumber, NationalNumber, PhoneNumber } from 'libphonenumber-js';
export function parsePhoneNumber(text: string, metadata: object): PhoneNumber;
export function parsePhoneNumber(text: string, defaultCountry: CountryCode, metadata: object): PhoneNumber;
export function parse(text: string, metadata: object): ParsedNumber;

@@ -14,29 +17,35 @@ export function parse(text: string, options: CountryCode | { defaultCountry?: CountryCode, extended?: boolean }, metadata: object): ParsedNumber;

export function format(parsedNumber: ParsedNumber, format: NumberFormat, metadata: object): string;
export function format(phone: TelephoneNumber, format: NumberFormat, metadata: object): string;
export function format(phone: TelephoneNumber, country: CountryCode, format: NumberFormat, metadata: object): string;
export function format(phone: NationalNumber, format: NumberFormat, metadata: object): string;
export function format(phone: NationalNumber, country: CountryCode, format: NumberFormat, metadata: object): string;
export function formatNumber(parsedNumber: ParsedNumber, format: NumberFormat, metadata: object): string;
export function formatNumber(phone: TelephoneNumber, format: NumberFormat, metadata: object): string;
export function formatNumber(phone: TelephoneNumber, country: CountryCode, format: NumberFormat, metadata: object): string;
export function formatNumber(phone: NationalNumber, format: NumberFormat, metadata: object): string;
export function formatNumber(phone: NationalNumber, country: CountryCode, format: NumberFormat, metadata: object): string;
export function getNumberType(parsedNumber: ParsedNumber, metadata: object): NumberType;
export function getNumberType(phone: TelephoneNumber, metadata: object): NumberType;
export function getNumberType(phone: TelephoneNumber, country: CountryCode, metadata: object): NumberType;
export function getNumberType(phone: NationalNumber, metadata: object): NumberType;
export function getNumberType(phone: NationalNumber, country: CountryCode, metadata: object): NumberType;
export function getExampleNumber(country: CountryCode, examples: object, metadata: object): PhoneNumber;
export function isPossibleNumber(parsedNumber: ParsedNumber, metadata: object): boolean;
export function isPossibleNumber(phone: NationalNumber, metadata: object): boolean;
export function isPossibleNumber(phone: NationalNumber, country: CountryCode, metadata: object): boolean;
export function isValidNumber(parsedNumber: ParsedNumber, metadata: object): boolean;
export function isValidNumber(phone: TelephoneNumber, metadata: object): boolean;
export function isValidNumber(phone: TelephoneNumber, country: CountryCode, metadata: object): boolean;
export function isValidNumber(phone: NationalNumber, metadata: object): boolean;
export function isValidNumber(phone: NationalNumber, country: CountryCode, metadata: object): boolean;
export function isValidNumberForRegion(phone: TelephoneNumber, country: CountryCode, metadata: object): boolean;
export function isValidNumberForRegion(phone: NationalNumber, country: CountryCode, metadata: object): boolean;
// Deprecated.
export function findPhoneNumbers(text: string, metadata: object): NumberFound[];
export function findPhoneNumbers(text: string, options: CountryCode | { defaultCountry?: CountryCode }, metadata: object): NumberFound[];
export function findParsedNumbers(text: string, metadata: object): NumberFound[];
export function findParsedNumbers(text: string, options: CountryCode | { defaultCountry?: CountryCode }, metadata: object): NumberFound[];
// Deprecated.
export function searchPhoneNumbers(text: string, metadata: object): IterableIterator<NumberFound>;
export function searchPhoneNumbers(text: string, options: CountryCode | { defaultCountry?: CountryCode }, metadata: object): IterableIterator<NumberFound>;
export function searchParsedNumbers(text: string, metadata: object): IterableIterator<NumberFound>;
export function searchParsedNumbers(text: string, options: CountryCode | { defaultCountry?: CountryCode }, metadata: object): IterableIterator<NumberFound>;
// Deprecated.
export class PhoneNumberSearch {
export class ParsedNumberSearch {
constructor(text: string, metadata: object);

@@ -54,3 +63,3 @@ constructor(text: string, options: { defaultCountry?: CountryCode }, metadata: object);

export class PhoneNumberMatcher {
export class ParsedNumberMatcher {
constructor(text: string, metadata: object);

@@ -66,5 +75,5 @@ constructor(text: string, options: { defaultCountry?: CountryCode }, metadata: object);

export function formatIncompletePhoneNumber(number: string, metadata: object): string;
export function formatIncompletePhoneNumber(number: string, countryCode: CountryCode, metadata: object): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function formatIncompleteParsedNumber(number: string, metadata: object): string;
export function formatIncompleteParsedNumber(number: string, countryCode: CountryCode, metadata: object): string;
export function parseIncompleteParsedNumber(text: string): string;
export function parseParsedNumberCharacter(character: string): string;

@@ -5,2 +5,4 @@ 'use strict'

exports.parsePhoneNumber = require('./build/parsePhoneNumber').default
// Deprecated: remove `parse()` export in 2.0.0.

@@ -15,2 +17,4 @@ // (renamed to `parseNumber()`)

exports.getNumberType = require('./build/getNumberType').default
exports.getExampleNumber = require('./build/getExampleNumber').default
exports.isPossibleNumber = require('./build/isPossibleNumber').default
exports.isValidNumber = require('./build/validate').default

@@ -17,0 +21,0 @@ exports.isValidNumberForRegion = require('./build/isValidNumberForRegion').default

@@ -544,3 +544,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

value: function extract_country_calling_code() {
var _extractCountryCallin = extractCountryCallingCode(this.parsed_input, this.default_country, this.metadata),
var _extractCountryCallin = extractCountryCallingCode(this.parsed_input, this.default_country, this.metadata.metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,

@@ -547,0 +547,0 @@ number = _extractCountryCallin.number;

import { stripIDDPrefix } from './IDD';
import Metadata from './metadata';

@@ -106,3 +107,3 @@ import parseIncompletePhoneNumber from './parseIncompletePhoneNumber';

// to a proper international phone number.
var numberWithoutIDD = stripIDDPrefix(number, country, metadata.metadata);
var numberWithoutIDD = stripIDDPrefix(number, country, metadata);

@@ -124,2 +125,4 @@ // If an IDD prefix was stripped then

metadata = new Metadata(metadata);
// The thing with country phone codes

@@ -126,0 +129,0 @@ // is that they are orthogonal to each other

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

@@ -221,3 +223,3 @@

// `findNumbers('88005553535', 'RU', [options], metadata)`.
if (typeof arg_2 === 'string') {
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {

@@ -224,0 +226,0 @@ options = _extends({ defaultCountry: arg_2 }, arg_3);

@@ -10,4 +10,8 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

import { extractCountryCallingCode, VALID_PUNCTUATION, matches_entirely } from './common';
import {
// extractCountryCallingCode,
VALID_PUNCTUATION, matches_entirely } from './common';
import parse from './parse';
import { getIDDPrefix } from './IDD';

@@ -29,8 +33,8 @@

// ```js
// format('8005553535', 'RU', 'International')
// format('8005553535', 'RU', 'International', metadata)
// format({ phone: '8005553535', country: 'RU' }, 'International')
// format({ phone: '8005553535', country: 'RU' }, 'International', metadata)
// format('+78005553535', 'National')
// format('+78005553535', 'National', metadata)
// format('8005553535', 'RU', 'INTERNATIONAL')
// format('8005553535', 'RU', 'INTERNATIONAL', metadata)
// format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL')
// format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', metadata)
// format('+78005553535', 'NATIONAL')
// format('+78005553535', 'NATIONAL', metadata)
// ```

@@ -45,34 +49,29 @@ //

if (input.country && metadata.hasCountry(input.country)) {
if (input.country) {
// Validate `input.country`.
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
metadata.country(input.country);
}
} else if (input.countryCallingCode) {
metadata.chooseCountryByCountryCallingCode(input.countryCallingCode);
} else return input.phone || '';
// `number` is a national (significant) number in this case.
var countryCallingCode = metadata.countryCallingCode();
var _extractCountryCallin = extractCountryCallingCode(input.phone, null, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
number = _extractCountryCallin.number;
var nationalNumber = options.v2 ? input.nationalNumber : input.phone;
countryCallingCode = countryCallingCode || input.countryCallingCode;
// This variable should have been declared inside `case`s
// but Babel has a bug and it says "duplicate variable declaration".
var number = void 0;
if (countryCallingCode) {
// Check country restriction
if (input.country && metadata.selectedCountry() && countryCallingCode !== metadata.countryCallingCode()) {
return input.phone;
}
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
}
if (!metadata.selectedCountry()) {
return input.phone;
}
switch (format_type) {
case 'International':
if (!number) {
return '+' + metadata.countryCallingCode();
case 'INTERNATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return '+' + countryCallingCode;
}
number = format_national_number(number, 'International', false, metadata);
number = '+' + metadata.countryCallingCode() + ' ' + number;
number = format_national_number(nationalNumber, 'INTERNATIONAL', false, metadata);
number = '+' + countryCallingCode + ' ' + number;
return add_extension(number, input.ext, metadata, options.formatExtension);

@@ -82,7 +81,7 @@

// `E.164` doesn't define "phone number extensions".
return '+' + metadata.countryCallingCode() + input.phone;
return '+' + countryCallingCode + nationalNumber;
case 'RFC3966':
return formatRFC3966({
number: '+' + metadata.countryCallingCode() + input.phone,
number: '+' + countryCallingCode + nationalNumber,
ext: input.ext

@@ -101,17 +100,19 @@ });

if (options.humanReadable) {
var formattedForSameCountryCallingCode = countryCallingCode && formatIDDSameCountryCallingCodeNumber(number, countryCallingCode, options.fromCountry, metadata);
var formattedForSameCountryCallingCode = countryCallingCode && formatIDDSameCountryCallingCodeNumber(nationalNumber, metadata.countryCallingCode(), options.fromCountry, metadata);
if (formattedForSameCountryCallingCode) {
number = formattedForSameCountryCallingCode;
} else {
number = IDDPrefix + ' ' + metadata.countryCallingCode() + ' ' + format_national_number(number, 'International', false, metadata);
number = IDDPrefix + ' ' + countryCallingCode + ' ' + format_national_number(nationalNumber, 'INTERNATIONAL', false, metadata);
}
return add_extension(number, input.ext, metadata, options.formatExtension);
}
return '' + IDDPrefix + metadata.countryCallingCode() + number;
return '' + IDDPrefix + countryCallingCode + nationalNumber;
case 'National':
if (!number) {
case 'NATIONAL':
// Legacy argument support.
// (`{ country: ..., phone: '' }`)
if (!nationalNumber) {
return '';
}
number = format_national_number(number, 'National', true, metadata);
number = format_national_number(nationalNumber, 'NATIONAL', true, metadata);
return add_extension(number, input.ext, metadata, options.formatExtension);

@@ -156,3 +157,3 @@ }

return format_national_number_using_format(number, format, format_as === 'International', enforce_national_prefix, metadata);
return format_national_number_using_format(number, format, format_as === 'INTERNATIONAL', enforce_national_prefix, metadata);
}

@@ -214,10 +215,4 @@

// If country code is supplied.
// `format('8005553535', 'RU', 'National', [options], metadata)`.
// `format('8005553535', 'RU', 'NATIONAL', [options], metadata)`.
if (typeof arg_3 === 'string') {
// Will be `parse()`d later in code
input = {
phone: arg_1,
country: arg_2
};
format_type = arg_3;

@@ -231,11 +226,8 @@

}
input = parse(arg_1, { defaultCountry: arg_2, extended: true }, metadata);
}
// Just an international phone number is supplied
// `format('+78005553535', 'National', [options], metadata)`.
// `format('+78005553535', 'NATIONAL', [options], metadata)`.
else {
// Will be `parse()`d later in code
input = {
phone: arg_1
};
if (typeof arg_2 !== 'string') {

@@ -253,7 +245,9 @@ throw new Error('`format` argument not passed to `formatNumber(number, format)`');

}
input = parse(arg_1, { extended: true }, metadata);
}
}
// If the phone number is passed as a parsed number object.
// `format({ phone: '8005553535', country: 'RU' }, 'National', [options], metadata)`.
else if (is_object(arg_1) && typeof arg_1.phone === 'string') {
// `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
else if (is_object(arg_1)) {
input = arg_1;

@@ -270,7 +264,13 @@ format_type = arg_2;

if (format_type === 'International') {
format_type = 'INTERNATIONAL';
} else if (format_type === 'National') {
format_type = 'NATIONAL';
}
// Validate `format_type`.
switch (format_type) {
case 'International':
case 'E.164':
case 'National':
case 'INTERNATIONAL':
case 'NATIONAL':
case 'RFC3966':

@@ -313,3 +313,3 @@ case 'IDD':

if (toCountryCallingCode === '1') {
return toCountryCallingCode + ' ' + format_national_number(number, 'National', false, toCountryMetadata);
return toCountryCallingCode + ' ' + format_national_number(number, 'NATIONAL', false, toCountryMetadata);
}

@@ -325,5 +325,5 @@

// http://www.petitfute.com/voyage/225-info-pratiques-reunion
return format_national_number(number, 'National', false, toCountryMetadata);
return format_national_number(number, 'NATIONAL', false, toCountryMetadata);
}
}
//# sourceMappingURL=format.js.map

@@ -91,5 +91,5 @@ // This function is copy-pasted from

if (can_be_internationally_dialled(number) && check_number_length_for_type(number.phone, undefined, metadata) !== 'TOO_SHORT') {
formatted_number = format(number, 'International', metadata.metadata);
formatted_number = format(number, 'INTERNATIONAL', metadata.metadata);
} else {
formatted_number = format(number, 'National', metadata.metadata);
formatted_number = format(number, 'NATIONAL', metadata.metadata);
}

@@ -122,5 +122,5 @@ } else {

(country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile) && can_be_internationally_dialled(number)) {
formatted_number = format(number, 'International');
formatted_number = format(number, 'INTERNATIONAL');
} else {
formatted_number = format(number, 'National');
formatted_number = format(number, 'NATIONAL');
}

@@ -132,3 +132,3 @@ }

// if it cannot be internationally dialled.
return with_formatting ? format(number, 'International', metadata.metadata) : format(number, 'E.164', metadata.metadata);
return with_formatting ? format(number, 'INTERNATIONAL', metadata.metadata) : format(number, 'E.164', metadata.metadata);
}

@@ -135,0 +135,0 @@

@@ -12,16 +12,12 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

// Finds out national phone number type (fixed line, mobile, etc)
export default function get_number_type(arg_1, arg_2, arg_3) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3),
export default function get_number_type(arg_1, arg_2, arg_3, arg_4) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3, arg_4),
input = _sort_out_arguments.input,
options = _sort_out_arguments.options,
metadata = _sort_out_arguments.metadata;
// When no input was passed
// When `parse()` returned `{}`
// meaning that the phone number is not a valid one.
if (!input) {
return;
}
// When `parse()` returned `{}`
// meaning that the phone number is not a valid one.
if (!input.country) {

@@ -35,3 +31,3 @@ return;

var national_number = input.phone;
var nationalNumber = options.v2 ? input.nationalNumber : input.phone;
metadata.country(input.country);

@@ -43,3 +39,3 @@

// Is this national number even valid for this country
if (!matches_entirely(national_number, metadata.nationalNumberPattern())) {
if (!matches_entirely(nationalNumber, metadata.nationalNumberPattern())) {
return;

@@ -49,3 +45,3 @@ }

// Is it fixed line number
if (is_of_type(national_number, 'FIXED_LINE', metadata)) {
if (is_of_type(nationalNumber, 'FIXED_LINE', metadata)) {
// Because duplicate regular expressions are removed

@@ -69,3 +65,3 @@ // to reduce metadata size, if "mobile" pattern is ""

/* istanbul ignore if */
if (is_of_type(national_number, 'MOBILE', metadata)) {
if (is_of_type(nationalNumber, 'MOBILE', metadata)) {
return 'FIXED_LINE_OR_MOBILE';

@@ -91,3 +87,3 @@ }

if (is_of_type(national_number, _type, metadata)) {
if (is_of_type(nationalNumber, _type, metadata)) {
return _type;

@@ -98,3 +94,3 @@ }

export function is_of_type(national_number, type, metadata) {
export function is_of_type(nationalNumber, type, metadata) {
type = metadata.type(type);

@@ -112,12 +108,13 @@

// already checked before a specific number type.
if (type.possibleLengths() && type.possibleLengths().indexOf(national_number.length) < 0) {
if (type.possibleLengths() && type.possibleLengths().indexOf(nationalNumber.length) < 0) {
return false;
}
return matches_entirely(national_number, type.pattern());
return matches_entirely(nationalNumber, type.pattern());
}
// Sort out arguments
export function sort_out_arguments(arg_1, arg_2, arg_3) {
export function sort_out_arguments(arg_1, arg_2, arg_3, arg_4) {
var input = void 0;
var options = {};
var metadata = void 0;

@@ -128,7 +125,12 @@

if (typeof arg_1 === 'string') {
// If "resrict country" argument is being passed
// If "default country" argument is being passed
// then convert it to an `options` object.
// `getNumberType('88005553535', 'RU', metadata)`.
if (typeof arg_2 === 'string' || arg_2 === undefined) {
metadata = arg_3;
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {
options = arg_3;
metadata = arg_4;
} else {
metadata = arg_3;
}

@@ -142,2 +144,4 @@ // `parse` extracts phone numbers from raw text,

input = parse(arg_1, arg_2, metadata);
} else {
input = {};
}

@@ -149,3 +153,8 @@ }

else {
metadata = arg_2;
if (arg_3) {
options = arg_2;
metadata = arg_3;
} else {
metadata = arg_2;
}

@@ -159,2 +168,4 @@ // `parse` extracts phone numbers from raw text,

input = parse(arg_1, metadata);
} else {
input = {};
}

@@ -165,17 +176,18 @@ }

// `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`.
else if (is_object(arg_1) && typeof arg_1.phone === 'string') {
// The `arg_1` must be a valid phone number
// as a whole, not just a part of it which gets parsed here.
if (is_viable_phone_number(arg_1.phone)) {
input = arg_1;
else if (is_object(arg_1)) {
input = arg_1;
if (arg_3) {
options = arg_2;
metadata = arg_3;
} else {
metadata = arg_2;
}
metadata = arg_2;
} else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.');
return { input: input, metadata: new Metadata(metadata) };
return { input: input, options: options, metadata: new Metadata(metadata) };
}
// Should only be called for the "new" metadata which has "possible lengths".
export function check_number_length_for_type(national_number, type, metadata) {
export function check_number_length_for_type(nationalNumber, type, metadata) {
var type_info = metadata.type(type);

@@ -199,3 +211,3 @@

// (true for some non-geographical entities), so we just check mobile.
return test_number_length_for_type(national_number, 'MOBILE', metadata);
return check_number_length_for_type(nationalNumber, 'MOBILE', metadata);
}

@@ -231,7 +243,8 @@

var actual_length = national_number.length;
var actual_length = nationalNumber.length;
// In `libphonenumber-js` all "local-only" formats are dropped for simplicity.
// // This is safe because there is never an overlap beween the possible lengths
// // and the local-only lengths; this is checked at build time.
// if (local_lengths && local_lengths.indexOf(national_number.length) >= 0)
// if (local_lengths && local_lengths.indexOf(nationalNumber.length) >= 0)
// {

@@ -238,0 +251,0 @@ // return 'IS_POSSIBLE_LOCAL_ONLY'

@@ -54,2 +54,7 @@ var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

}, {
key: 'getDefaultCountryMetadataForRegion',
value: function getDefaultCountryMetadataForRegion() {
return this.metadata.countries[this.countryCallingCodes()[this.countryCallingCode()][0]];
}
}, {
key: 'countryCallingCode',

@@ -84,2 +89,12 @@ value: function countryCallingCode() {

}, {
key: '_getFormats',
value: function _getFormats(country_metadata) {
return country_metadata[this.v1 ? 2 : this.v2 ? 3 : 4];
}
// For countries of the same region (e.g. NANPA)
// formats are all stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'formats',

@@ -89,3 +104,3 @@ value: function formats() {

var formats = this.country_metadata[this.v1 ? 2 : this.v2 ? 3 : 4] || [];
var formats = this._getFormats(this.country_metadata) || this._getFormats(this.getDefaultCountryMetadataForRegion()) || [];
return formats.map(function (_) {

@@ -101,5 +116,15 @@ return new Format(_, _this);

}, {
key: '_getNationalPrefixFormattingRule',
value: function _getNationalPrefixFormattingRule(country_metadata) {
return country_metadata[this.v1 ? 4 : this.v2 ? 5 : 6];
}
// For countries of the same region (e.g. NANPA)
// national prefix formatting rule is stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'nationalPrefixFormattingRule',
value: function nationalPrefixFormattingRule() {
return this.country_metadata[this.v1 ? 4 : this.v2 ? 5 : 6];
return this._getNationalPrefixFormattingRule(this.country_metadata) || this._getNationalPrefixFormattingRule(this.getDefaultCountryMetadataForRegion());
}

@@ -119,5 +144,16 @@ }, {

}, {
key: '_getNationalPrefixIsOptionalWhenFormatting',
value: function _getNationalPrefixIsOptionalWhenFormatting() {
return !!this.country_metadata[this.v1 ? 7 : this.v2 ? 8 : 9];
}
// For countries of the same region (e.g. NANPA)
// "national prefix is optional when parsing" flag is
// stored in the "main" country for that region.
// E.g. "RU" and "KZ", "US" and "CA".
}, {
key: 'nationalPrefixIsOptionalWhenFormatting',
value: function nationalPrefixIsOptionalWhenFormatting() {
return !!this.country_metadata[this.v1 ? 7 : this.v2 ? 8 : 9];
return this._getNationalPrefixIsOptionalWhenFormatting(this.country_metadata) || this._getNationalPrefixIsOptionalWhenFormatting(this.getDefaultCountryMetadataForRegion());
}

@@ -124,0 +160,0 @@ }, {

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
// This is a port of Google Android `libphonenumber`'s

@@ -18,4 +20,8 @@ // `phonenumberutil.js` of 17th November, 2016.

import { is_possible_number } from './isPossibleNumber';
import { parseRFC3966 } from './RFC3966';
import PhoneNumber from './PhoneNumber';
// The minimum length of the national significant number.

@@ -135,2 +141,5 @@ var MIN_LENGTH_FOR_NSN = 2;

if (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {
if (options.v2) {
throw new Error('INVALID_COUNTRY');
}
throw new Error('Unknown country: ' + options.defaultCountry);

@@ -141,3 +150,3 @@ }

var _parse_input = parse_input(text),
var _parse_input = parse_input(text, options.v2),
formatted_phone_number = _parse_input.number,

@@ -150,2 +159,5 @@ ext = _parse_input.ext;

if (!formatted_phone_number) {
if (options.v2) {
throw new Error('NOT_A_NUMBER');
}
return {};

@@ -156,3 +168,3 @@ }

country = _parse_phone_number.country,
national_number = _parse_phone_number.national_number,
nationalNumber = _parse_phone_number.national_number,
countryCallingCode = _parse_phone_number.countryCallingCode,

@@ -162,2 +174,5 @@ carrierCode = _parse_phone_number.carrierCode;

if (!metadata.selectedCountry()) {
if (options.v2) {
throw new Error('INVALID_COUNTRY');
}
return {};

@@ -167,2 +182,13 @@ }

// Validate national (significant) number length.
if (nationalNumber.length < MIN_LENGTH_FOR_NSN) {
// Won't throw here because the regexp already demands length > 1.
/* istanbul ignore if */
if (options.v2) {
throw new Error('TOO_SHORT');
}
// Google's demo just throws an error in this case.
return {};
}
// Validate national (significant) number length.
//

@@ -176,3 +202,6 @@ // A sidenote:

//
if (national_number.length < MIN_LENGTH_FOR_NSN || national_number.length > MAX_LENGTH_FOR_NSN) {
if (nationalNumber.length > MAX_LENGTH_FOR_NSN) {
if (options.v2) {
throw new Error('TOO_LONG');
}
// Google's demo just throws an error in this case.

@@ -182,9 +211,25 @@ return {};

if (options.v2) {
var phoneNumber = new PhoneNumber(countryCallingCode, nationalNumber, metadata.metadata);
if (country) {
phoneNumber.country = country;
}
if (carrierCode) {
phoneNumber.carrierCode = carrierCode;
}
if (ext) {
phoneNumber.ext = ext;
}
return phoneNumber;
}
// Check if national phone number pattern matches the number
// National number pattern is different for each country,
// even for those ones which are part of the "NANPA" group.
var valid = country && matches_entirely(national_number, metadata.nationalNumberPattern()) ? true : false;
var valid = country && matches_entirely(nationalNumber, metadata.nationalNumberPattern()) ? true : false;
if (!options.extended) {
return valid ? result(country, national_number, ext) : {};
return valid ? result(country, nationalNumber, ext) : {};
}

@@ -197,4 +242,4 @@

valid: valid,
possible: valid ? true : options.extended === true && metadata.possibleLengths() && is_possible_number(national_number, countryCallingCode !== undefined, metadata),
phone: national_number,
possible: valid ? true : options.extended === true && metadata.possibleLengths() && is_possible_number(nationalNumber, countryCallingCode !== undefined, metadata),
phone: nationalNumber,
ext: ext

@@ -220,7 +265,14 @@ };

*/
export function extract_formatted_phone_number(text) {
if (!text || text.length > MAX_INPUT_STRING_LENGTH) {
export function extract_formatted_phone_number(text, v2) {
if (!text) {
return;
}
if (text.length > MAX_INPUT_STRING_LENGTH) {
if (v2) {
throw new Error('TOO_LONG');
}
return;
}
// Attempt to extract a possible number from the string passed in

@@ -383,3 +435,3 @@

// `parse('88005553535', 'RU', [options], metadata)`.
if (typeof arg_2 === 'string') {
if ((typeof arg_2 === 'undefined' ? 'undefined' : _typeof(arg_2)) !== 'object') {
if (arg_4) {

@@ -445,13 +497,2 @@ options = _extends({ defaultCountry: arg_2 }, arg_3);

function is_possible_number(national_number, is_international, metadata) {
switch (check_number_length_for_type(national_number, undefined, metadata)) {
case 'IS_POSSIBLE':
return true;
// case 'IS_POSSIBLE_LOCAL_ONLY':
// return !is_international
default:
return false;
}
}
/**

@@ -461,3 +502,3 @@ * @param {string} text - Input.

*/
function parse_input(text) {
function parse_input(text, v2) {
// Parse RFC 3966 phone number URI.

@@ -468,3 +509,3 @@ if (text && text.indexOf('tel:') === 0) {

var number = extract_formatted_phone_number(text);
var number = extract_formatted_phone_number(text, v2);

@@ -507,3 +548,3 @@ // If the phone number is not viable, then abort.

function parse_phone_number(formatted_phone_number, default_country, metadata) {
var _extractCountryCallin = extractCountryCallingCode(formatted_phone_number, default_country, metadata),
var _extractCountryCallin = extractCountryCallingCode(formatted_phone_number, default_country, metadata.metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,

@@ -510,0 +551,0 @@ number = _extractCountryCallin.number;

@@ -13,2 +13,4 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

import PhoneNumber from './PhoneNumber';
import { MAX_LENGTH_FOR_NSN, MAX_LENGTH_COUNTRY_CODE, VALID_PUNCTUATION, create_extension_pattern } from './common';

@@ -220,2 +222,13 @@

if (match) {
if (this.options.v2) {
var phoneNumber = new PhoneNumber(match.country, match.phone, this.metadata.metadata);
if (match.ext) {
phoneNumber.ext = match.ext;
}
return {
startsAt: match.startsAt,
endsAt: match.endsAt,
number: phoneNumber
};
}
return match;

@@ -222,0 +235,0 @@ }

@@ -24,3 +24,3 @@ export default function compress(input) {

country.formats.map(function (format) {
country.formats && country.formats.map(function (format) {
// When changing this array also change getters in `./metadata.js`

@@ -27,0 +27,0 @@ var format_array = [format.pattern, format.format, format.leading_digits_patterns, format.national_prefix_formatting_rule, format.national_prefix_is_optional_when_formatting, format.international_format];

@@ -344,3 +344,4 @@ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

// Some countries don't have `availableFormats` specified,
// because those formats are inherited from the "main country for region".
// because those formats are inherited from the "main country for region":
// all non-"main" countries inherit their formats from the "main" country for that region.
if (territory.availableFormats) {

@@ -431,4 +432,9 @@ country.formats = territory.availableFormats[0].numberFormat.map(function (number_format) {

// Some countries don't have `availableFormats` specified,
// because those formats are meant to be copied
// from the "main country for region".
// because those formats are meant to be copied from the "main country for region":
// all non-"main" countries inherit their formats from the "main" country for that region.
// If that's the case then `nationalPrefixFormattingRule` and
// `nationalPrefixOptionalWhenFormatting` are also copied from the "main" region.
// `nationalPrefix` itself though seems to be always present
// even if it's the same for the "main" region.
// Examples: "RU" and "KZ", "US" and "CA".

@@ -453,11 +459,21 @@

var main_country_for_region_code = country_calling_code_to_countries[_country.phone_code][0];
var main_country_for_region = countries[main_country_for_region_code];
_country.formats = main_country_for_region.formats;
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats
// and phone numbers are formatted as a block in those countries.
if (!_country.formats) {
_country.formats = [];
if (main_country_for_region_code === _country_code) {
// Some countries like Saint Helena and Falkland Islands
// ('AC', 'FK', 'KI', 'NU', 'SH', 'TA', ...)
// don't have any phone number formats defined
// and phone numbers are not formatted in those countries.
if (!_country.formats) {
_country.formats = [];
}
} else {
if (_country.formats) {
throw new Error('Country "' + _country_code + '" is supposed to inherit formats from "' + main_country_for_region_code + '" but has its own formats defined.');
}
if (_country.nationalPrefixFormattingRule) {
throw new Error('Country "' + _country_code + '" is supposed to inherit "nationalPrefixFormattingRule" from "' + main_country_for_region_code + '" but has its own "nationalPrefixFormattingRule" defined.');
}
if (_country.nationalPrefixIsOptionalWhenFormatting) {
throw new Error('Country "' + _country_code + '" is supposed to inherit "nationalPrefixIsOptionalWhenFormatting" from "' + main_country_for_region_code + '" but has its own "nationalPrefixIsOptionalWhenFormatting" defined.');
}
}

@@ -464,0 +480,0 @@ }

import parse, { is_viable_phone_number } from './parse';
import get_number_type, { sort_out_arguments } from './getNumberType';
import { matches_entirely } from './common';

@@ -33,11 +34,12 @@ /**

*/
export default function isValidNumber(arg_1, arg_2, arg_3) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3),
export default function isValidNumber(arg_1, arg_2, arg_3, arg_4) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3, arg_4),
input = _sort_out_arguments.input,
options = _sort_out_arguments.options,
metadata = _sort_out_arguments.metadata;
if (!input) {
return false;
}
// This is just to support `isValidNumber({})`
// for cases when `parseNumber()` returns `{}`.
if (!input.country) {

@@ -53,8 +55,13 @@ return false;

// By default, countries only have type regexps when it's required for
// distinguishing different countries having the same `countryCallingCode`.
if (metadata.hasTypes()) {
return get_number_type(input, metadata.metadata) !== undefined;
return get_number_type(input, options, metadata.metadata) !== undefined;
}
return true;
// If there are no type regexps for this country in metadata then use
// `nationalNumberPattern` as a "better than nothing" replacement.
var national_number = options.v2 ? input.nationalNumber : input.phone;
return matches_entirely(national_number, metadata.nationalNumberPattern());
}
//# sourceMappingURL=validate.js.map

@@ -8,2 +8,9 @@ 'use strict'

exports.parsePhoneNumber = function parsePhoneNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.parsePhoneNumber.apply(this, parameters)
}
// Deprecated: remove `parse()` export in 2.0.0.

@@ -48,2 +55,16 @@ // (renamed to `parseNumber()`)

exports.getExampleNumber = function getExampleNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.getExampleNumber.apply(this, parameters)
}
exports.isPossibleNumber = function isPossibleNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.isPossibleNumber.apply(this, parameters)
}
exports.isValidNumber = function isValidNumber()

@@ -50,0 +71,0 @@ {

export type CountryCode = '001' | 'AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | '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' | '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';
export type NumberFormat = 'National' | 'International' | 'E.164' | 'RFC3966' | 'IDD';
export type NumberFormat = 'NATIONAL' | 'National' | 'INTERNATIONAL' | 'International' | 'E.164' | 'RFC3966' | 'IDD';
export type NumberType = undefined | 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VOIP' | 'PERSONAL_NUMBER' | 'PAGER' | 'UAN' | 'VOICEMAIL' | 'FIXED_LINE_OR_MOBILE' | 'FIXED_LINE' | 'MOBILE';
export interface TelephoneNumber extends String { }
export interface NationalNumber extends String { }
export interface Extension extends String { }
export interface CountryCallingCode extends String { }
export class PhoneNumber {
constructor(countryCallingCode: CountryCallingCode | country: CountryCode, nationalNumber: NationalNumber, metadata: object);
countryCallingCode: CountryCallingCode,
country?: CountryCode,
nationalNumber: NationalNumber,
number: string,
carrierCode?: string,
ext?: Extension,
isPossible(): boolean;
isValid(): boolean;
getType(): NumberType;
format(format: NumberFormat, options?: object): string;
formatNational(options?: object): string;
formatInternational(options?: object): string;
getURI(options?: object): string;
}
export interface ParsedNumber {
countryCallingCode?: CountryCallingCode,
country: CountryCode,
phone: TelephoneNumber,
phone: NationalNumber,
ext?: Extension,

@@ -20,5 +37,6 @@ possible?: boolean,

export interface NumberFound {
country: CountryCode,
phone: TelephoneNumber,
country?: CountryCode,
phone?: NationalNumber,
ext?: Extension,
number?: PhoneNumber,
startsAt: number,

@@ -28,2 +46,4 @@ endsAt: number

export function parsePhoneNumber(text: string, defaultCountry?: CountryCode): PhoneNumber;
// `parse()` and `parseCustom` are deprecated.

@@ -38,23 +58,28 @@ // Use `fparseNumber()` and `parseNumberCustom()` instead.

export function format(parsedNumber: ParsedNumber, format: NumberFormat): string;
export function format(phone: TelephoneNumber, format: NumberFormat): string;
export function format(phone: TelephoneNumber, country: CountryCode, format: NumberFormat): string;
export function format(phone: NationalNumber, format: NumberFormat): string;
export function format(phone: NationalNumber, country: CountryCode, format: NumberFormat): string;
export function formatNumber(parsedNumber: ParsedNumber, format: NumberFormat): string;
export function formatNumber(phone: TelephoneNumber, format: NumberFormat): string;
export function formatNumber(phone: TelephoneNumber, country: CountryCode, format: NumberFormat): string;
export function formatNumber(phone: NationalNumber, format: NumberFormat): string;
export function formatNumber(phone: NationalNumber, country: CountryCode, format: NumberFormat): string;
export function getNumberType(parsedNumber: ParsedNumber): NumberType;
export function getNumberType(phone: TelephoneNumber, country?: CountryCode): NumberType;
export function getNumberType(phone: NationalNumber, country?: CountryCode): NumberType;
export function getExampleNumber(country: CountryCode, examples: object): PhoneNumber;
export function isPossibleNumber(parsedNumber: ParsedNumber): boolean;
export function isPossibleNumber(phone: NationalNumber, country?: CountryCode): boolean;
export function isValidNumber(parsedNumber: ParsedNumber): boolean;
export function isValidNumber(phone: TelephoneNumber, country?: CountryCode): boolean;
export function isValidNumber(phone: NationalNumber, country?: CountryCode): boolean;
export function isValidNumberForRegion(phone: TelephoneNumber, country: CountryCode): boolean;
export function isValidNumberForRegion(phone: NationalNumber, country: CountryCode): boolean;
// Deprecated.
export function findPhoneNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): NumberFound[];
export function searchPhoneNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): IterableIterator<NumberFound>;
export function findParsedNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): NumberFound[];
export function searchParsedNumbers(text: string, options?: CountryCode | { defaultCountry?: CountryCode }): IterableIterator<NumberFound>;
// Deprecated.
export class PhoneNumberSearch {
export class ParsedNumberSearch {
constructor(text: string, options?: { defaultCountry?: CountryCode });

@@ -68,3 +93,3 @@ hasNext(): boolean;

export class PhoneNumberMatcher {
export class ParsedNumberMatcher {
constructor(text: string, options?: { defaultCountry?: CountryCode });

@@ -79,5 +104,5 @@ hasNext(): boolean;

export function formatIncompletePhoneNumber(number: string, countryCode?: CountryCode): string;
export function parseIncompletePhoneNumber(text: string): string;
export function parsePhoneNumberCharacter(character: string): string;
export function formatIncompleteParsedNumber(number: string, countryCode?: CountryCode): string;
export function parseIncompleteParsedNumber(text: string): string;
export function parseParsedNumberCharacter(character: string): string;

@@ -84,0 +109,0 @@ export class AsYouType {

import metadata from './metadata.min.json'
import parsePhoneNumberCustom from './es6/parsePhoneNumber'
import parseNumberCustom from './es6/parse'
import formatNumberCustom from './es6/format'
import getNumberTypeCustom from './es6/getNumberType'
import getExampleNumberCustom from './es6/getExampleNumber'
import isPossibleNumberCustom from './es6/isPossibleNumber'
import isValidNumberCustom from './es6/validate'

@@ -25,2 +29,9 @@ import isValidNumberForRegionCustom from './es6/isValidNumberForRegion'

export function parsePhoneNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return parsePhoneNumberCustom.apply(this, parameters)
}
export function parseNumber()

@@ -65,2 +76,16 @@ {

export function getExampleNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return getExampleNumberCustom.apply(this, parameters)
}
export function isPossibleNumber()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return isPossibleNumberCustom.apply(this, parameters)
}
export function isValidNumber()

@@ -67,0 +92,0 @@ {

{
"name": "libphonenumber-js",
"version": "1.5.2",
"version": "1.6.0",
"description": "A simpler (and smaller) rewrite of Google Android's popular libphonenumber library",

@@ -5,0 +5,0 @@ "main": "index.common.js",

@@ -47,3 +47,22 @@ # libphonenumber-js

_(new API)_
```js
import { parsePhoneNumber } from 'libphonenumber-js'
try {
const phoneNumber = parsePhoneNumber('Phone: 8 (800) 555 35 35.', 'RU')
phoneNumber.country === 'RU'
phoneNumber.number === '+78005553535'
phoneNumber.isValid() === true
phoneNumber.getType() === 'TOLL_FREE'
} catch (error) {
// Not a phone number, non-existent country, etc.
}
```
<details>
<summary>Legacy API</summary>
```js
import { parseNumber } from 'libphonenumber-js'

@@ -54,20 +73,37 @@

```
</details>
### Format phone number
_(new API)_
```js
import { parsePhoneNumber } from 'libphonenumber-js'
const phoneNumber = parsePhoneNumber('+12133734253')
phoneNumber.formatInternational() === '+1 213 373 4253'
phoneNumber.formatNational() === '(213) 373-4253'
phoneNumber.getURI() === 'tel:+12133734253'
```
<details>
<summary>Legacy API</summary>
```js
import { formatNumber } from 'libphonenumber-js'
formatNumber('+12133734253', 'International')
formatNumber('+12133734253', 'INTERNATIONAL')
// Outputs: '+1 213 373 4253'
formatNumber('+12133734253', 'National')
formatNumber('+12133734253', 'NATIONAL')
// Outputs: '(213) 373-4253'
formatNumber({ country: 'US', phone: '2133734253' }, 'International')
formatNumber({ country: 'US', phone: '2133734253' }, 'INTERNATIONAL')
// Outputs: '+1 213 373 4253'
formatNumber({ country: 'US', phone: '2133734253' }, 'National')
formatNumber({ country: 'US', phone: '2133734253' }, 'NATIONAL')
// Outputs: '(213) 373-4253'
```
</details>

@@ -88,2 +124,4 @@ ### "As You Type" formatter

_(new API)_
```js

@@ -93,5 +131,43 @@ import { findNumbers } from 'libphonenumber-js'

findNumbers(`
The number is +7 (800) 555-35-35 and
not (213) 373-4253 as written
in the document.
The number is +7 (800) 555-35-35
and not (213) 373-4253 ext. 1234
as it's written in the document.
`, 'US', {
v2: true
})
// Outputs:
//
// [{
// number: PhoneNumber {
// country: 'RU',
// countryCallingCode: '7',
// number: '+78005553535',
// nationalNumber: '8005553535'
// },
// startsAt : 14,
// endsAt : 32
// }, {
// number: PhoneNumber {
// country: 'US',
// countryCallingCode: '1',
// number: '+12133734253',
// nationalNumber: '2133734253',
// ext: '1234'
// },
// startsAt : 41,
// endsAt : 65
// }]
```
<details>
<summary>Legacy API</summary>
```js
import { findNumbers } from 'libphonenumber-js'
findNumbers(`
The number is +7 (800) 555-35-35
and not (213) 373-4253 ext. 1234
as it's written in the document.
`, 'US')

@@ -109,6 +185,8 @@

// country : 'US',
// ext : '1234',
// startsAt : 41,
// endsAt : 55
// endsAt : 65
// }]
```
</details>

@@ -127,17 +205,129 @@ ## Definitions

"Country calling code" are the digits between the `+` and the national (significant) number when the number is written in international format. E.g. for US country calling code is `1` and for France it's `33`.
"Country calling code" are the digits between the `+` and the national (significant) number when the number is written in international format. E.g. for US country calling code is `1` and for France it's `33`. Several countries can share the same "country calling code", e.g. [NANPA](https://en.wikipedia.org/wiki/North_American_Numbering_Plan) countries like USA and Canada sharing the same `1` country calling code.
## API
### parsePhoneNumber(text, [defaultCountry])
Parses `text` extracting a phone number from it.
```js
import { parsePhoneNumber } from 'libphonenumber-js'
try {
const phoneNumber = parsePhoneNumber('Call: (213) 373-42-53 ext. 1234.', 'US')
} catch (error) {
// Not a phone number, non-existent country, etc.
}
```
Returns an instance of [`PhoneNumber`](#phonenumber) class. Throws in case of an error: no phone number found, non-existent [country calling code](#country-calling-code), invalid default country, etc.
### `PhoneNumber`
<!--
`PhoneNumber` class constructor accepts two arguments: `country`/`countryCallingCode` and `nationalNumber`. Also `metadata`.
```js
const phoneNumber = new PhoneNumber('RU', '8005553535', metadata)
```
-->
`PhoneNumber` class instance has the following properties:
* `number` — The phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. Example: `"+12133734253"`.
* `countryCallingCode` — The [country calling code](#country-calling-code). Example: `"1"`.
* `nationalNumber` — The [national (significant) number](#national-significant-number). Example: `"2133734253"`.
* `country` — The [country code](#country-code). Will be `undefined` when no `country` could be derived from the phone number. For example, when several countries have the same `countryCallingCode` and the `nationalNumber` doesn't look like it belongs to any of them. Example: `"US"`.
* `ext` — The [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)), if any. Example: `"1234"`.
* `carrierCode` — The ["carrier code"](https://www.voip-info.org/carrier-identification-codes/), if any. "Carrier codes" are only used in Colombia and Brazil and only when dialing within those countries from a mobile phone to a fixed line number. Example: `"15"`.
`PhoneNumber` class instance provides the following methods:
#### `format(format: String, [options])`
Formats the phone number into a string according to a `format`.
Available `format`s:
* `NATIONAL` — Example: `"(213) 373-4253"`
* `INTERNATIONAL` — Example: `"+1 213 373 4253"`
* [`E.164`](https://en.wikipedia.org/wiki/E.164) — Example: `"+12133734253"`
* [`RFC3966`](https://www.ietf.org/rfc/rfc3966.txt) (the phone number URI) — Example: `"tel:+12133734253;ext=123"`
* `IDD` — ["Out-of-country" dialing](https://en.wikipedia.org/wiki/International_direct_dialing) format. Example: `"01178005553535"` for `+7 800 555 35 35` being called out of `options.fromCountry === "US"`. If no `options.fromCountry` was passed or if there's no default IDD prefix for `options.fromCountry` then returns `undefined`. Pass `options.humanReadable: true` for a human-readable output (same output as Google `liphonenumber`'s `formatOutOfCountryCallingNumber()`).
Available `options`:
* `formatExtension(number, extension)` — Formats `number` and `extension` into a string. By default returns `${number} ext. ${extension}` for almost all countries with rare exceptions of some special cases like `${number} x${extension}` for UK.
* `humanReadable: Boolean` — Is only used for `IDD` formatting.
Examples:
```js
import { parsePhoneNumber } from 'libphonenumber-js'
const phoneNumber = parsePhoneNumber('+12133734253')
phoneNumber.format("NATIONAL") === '(213) 373-4253'
phoneNumber.format("INTERNATIONAL") === '+1 213 373 4253'
phoneNumber.format("RFC3966") === 'tel:+12133734253'
// Aliases
phoneNumber.formatNational() === phoneNumber.format("NATIONAL")
phoneNumber.formatInternational() === phoneNumber.format("INTERNATIONAL")
phoneNumber.getURI() === phoneNumber.format("RFC3966")
```
#### `isPossible()`
Checks if the phone number is "possible". Only checks the phone number length, doesn't check the number digits against any regular expressions.
#### `isValid()`
Checks if the phone number is "valid". First checks the phone number length and then checks the phone number digits against all available regular expressions. By default the library uses the "minimal" metadata which is only 75 kilobytes in size but also doesn't include most of the validation regular expressions. Some very basic validation is still included for each country though. "Full" metadata (140 kilobytes) can be used (see [Customizing metadata](#customizing-metadata) section of this document).
See also ["Using phone number validation feature"](#using-phone-number-validation-feature) considerations.
#### `getType()`
Determines phone number type (fixed line, mobile, toll free, etc). This function will work if `--extended` (or relevant `--types`) metadata is available (see [Customizing metadata](#customizing-metadata) section of this document). The regular expressions used to differentiate between various phone number types consume a lot of space (two thirds of the total size of the 140 kilobyte `--extended` library build) therefore they're not included in the bundle by default.
<details>
<summary>The list of possible return values</summary>
* `undefined`
* `MOBILE`
* `FIXED_LINE`
* `FIXED_LINE_OR_MOBILE`
* `PREMIUM_RATE`
* `TOLL_FREE`
* `SHARED_COST`
* `VOIP`
* `PERSONAL_NUMBER`
* `PAGER`
* `UAN`
* `VOICEMAIL`
</details>
### parseNumber(text, [defaultCountry], [options])
_(previously called `parse()`)_
_(legacy API)_
Attempts to parse a phone number from `text`.
If [`defaultCountry`](https://github.com/catamphetamine/libphonenumber-js#country-code) is passed then it's gonna be the default country for parsing non-international phone numbers.
If [`defaultCountry`](#country-code) is passed then it's gonna be the default country for parsing non-international phone numbers.
Returns `{ country, phone, ext }` object where
* `country` is a [country code](https://github.com/catamphetamine/libphonenumber-js#country-code).
* `phone` is a [national (significant) number](https://github.com/catamphetamine/libphonenumber-js#national-significant-number).
* `country` is a [country code](#country-code).
* `phone` is a [national (significant) number](#national-significant-number).
* `ext` is a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)).
If the phone number supplied isn't valid then an empty object `{}` is returned.
<details>
<summary>Examples</summary>
```js

@@ -166,3 +356,6 @@ // Parses international numbers.

```
</details>
####
Available `options`:

@@ -174,8 +367,11 @@

<details>
<summary><code>{ extended: true }</code> documentation and examples</summary>
The result of "extended" parsing is an object where
* `country` is a [country code](https://github.com/catamphetamine/libphonenumber-js#country-code).
* `phone` is a [national (significant) number](https://github.com/catamphetamine/libphonenumber-js#national-significant-number).
* `country` is a [country code](#country-code).
* `phone` is a [national (significant) number](#national-significant-number).
* `ext` is a [phone number extension](https://en.wikipedia.org/wiki/Extension_(telephone)).
* `countryCallingCode` is a [country calling code](https://github.com/catamphetamine/libphonenumber-js#country-calling-code).
* `countryCallingCode` is a [country calling code](#country-calling-code).
* [`carrierCode`](https://www.voip-info.org/carrier-identification-codes/)s are only used in Colombia and Brazil and only when dialing within those countries from a mobile phone to a fixed line number.

@@ -249,3 +445,9 @@ * `valid: boolean` — whether it's a "valid" (real) phone number.

The "extended" parsing mode is the default behaviour of the original Google's `libphonenumber`: it still returns parsed data even if the phone number being parsed is not considered valid (but is kinda "possible"). I guess this kind of behaviour is better for crawling websites for phone numbers because when mining "big data" it is better to extract all possible info rather than discard some pieces of it prematurely, e.g. when national (significant) number regexp for some country gets outdated which might very well happen because phone numbering plans are changing constantly around the world. Maybe after all it would make sense to make the "extended" parsing mode the default one in the next major version. I guess it would.
</details>
####
<details>
<summary>Also parses IDD-prefixed phone numbers</summary>
Sometimes users icorrectly input phone numbers in ["out-of-country" dialing](https://en.wikipedia.org/wiki/International_direct_dialing) (IDD-prefixed) format instead of the proper international phone number format (the "+" notation). In such cases `parseNumber()` will attempt to parse such IDD-prefixed numbers if "default country" is provided:

@@ -259,35 +461,38 @@

```
</details>
### formatNumber(number, format, [options])
_(previously called `format()`)_
_(legacy API)_
Formats a `number` into a string according to a `format`.
Available `format`s:
* `National` — e.g. `(213) 373-4253`
* `International` — e.g. `+1 213 373 4253`
* [`E.164`](https://en.wikipedia.org/wiki/E.164) — e.g. `+12133734253`
* [`RFC3966`](https://www.ietf.org/rfc/rfc3966.txt) (the phone number URI) — e.g. `tel:+12133734253;ext=123`
* `IDD` — ["Out-of-country" dialing](https://en.wikipedia.org/wiki/International_direct_dialing) format, e.g. `01178005553535` for `+7 800 555 35 35` being called out of `options.fromCountry === US`. If no `options.fromCountry` was passed or if there's no default IDD prefix for `options.fromCountry` then returns `undefined`. Pass `options.humanReadable: true` for a human-readable output (same output as Google `liphonenumber`'s `formatOutOfCountryCallingNumber()`).
Available `format`s and `options` are the same as for [`PhoneNumber.format(format)`](#formatformat-string-options).
The `number` argument must be either a result of `parseNumber()` function call (to strip national prefix) or an E.164 phone number string (e.g. `+12133734253`).
<details>
<summary>Examples</summary>
```js
// Formats E.164 phone numbers.
formatNumber('+12133734253', 'National') === '(213) 373-4253'
formatNumber('+12133734253', 'International') === '+1 213 373 4253'
formatNumber('+12133734253', 'NATIONAL') === '(213) 373-4253'
formatNumber('+12133734253', 'INTERNATIONAL') === '+1 213 373 4253'
// Formats E.164 phone numbers when
// they're not "valid" but still "possible".
formatNumber('+11111111111', 'National') === '(111) 111-1111'
formatNumber('+11111111111', 'International') === '+1 111 111 1111'
formatNumber('+11111111111', 'NATIONAL') === '(111) 111-1111'
formatNumber('+11111111111', 'INTERNATIONAL') === '+1 111 111 1111'
// Formats E.164 phone numbers when
// they're not "valid" and not "possible" (invalid length).
formatNumber('+11111', 'National') === '1111'
formatNumber('+11111', 'International') === '+1 1111'
formatNumber('+11111', 'NATIONAL') === '1111'
formatNumber('+11111', 'INTERNATIONAL') === '+1 1111'
// Formats a result of `parseNumber()` function call.
const parsedNumber = parseNumber('2133734253', 'US')
formatNumber(parsedNumber, 'National') === '(213) 373-4253'
formatNumber(parsedNumber, 'International') === '+1 213 373 4253'
formatNumber(parsedNumber, 'NATIONAL') === '(213) 373-4253'
formatNumber(parsedNumber, 'INTERNATIONAL') === '+1 213 373 4253'

@@ -297,4 +502,4 @@ // Formats a result of `parseNumber()` function call in "extended" mode

const possibleNumber = parseNumber('+11111111111', { extended: true })
formatNumber(possibleNumber, 'National') === '(111) 111-1111'
formatNumber(possibleNumber, 'International') === '+1 111 111 1111'
formatNumber(possibleNumber, 'NATIONAL') === '(111) 111-1111'
formatNumber(possibleNumber, 'INTERNATIONAL') === '+1 111 111 1111'

@@ -304,7 +509,7 @@ // Formats a result of `parseNumber()` function call in "extended" mode

const possibleNumber = parseNumber('+11111', { extended: true })
formatNumber(possibleNumber, 'National') === '1111'
formatNumber(possibleNumber, 'International') === '+1 1111'
formatNumber(possibleNumber, 'NATIONAL') === '1111'
formatNumber(possibleNumber, 'INTERNATIONAL') === '+1 1111'
// Formats phone number extensions.
formatNumber({ country: 'US', phone: '2133734253', ext: '123' }, 'National') === '(213) 373-4253 ext. 123'
formatNumber({ country: 'US', phone: '2133734253', ext: '123' }, 'NATIONAL') === '(213) 373-4253 ext. 123'

@@ -315,17 +520,7 @@ // When given an object not having `phone` property

```
</details>
Available `options`:
```js
{
formatExtension(number, extension) — Formats `number` and `extension` into a string.
By default returns `${number} ext. ${extension}`
for almost all countries with rare exceptions of
some special cases like `${number} x${extension}` for UK.
}
```
### `class` AsYouType(defaultCountry)
Creates a formatter for a partially entered phone number. The [`defaultCountry`](https://github.com/catamphetamine/libphonenumber-js#country-code) is optional and, if specified, is gonna be the default country for formatting non-international phone numbers. The formatter instance provides two methods:
Creates a formatter for a partially entered phone number. The [`defaultCountry`](#country-code) is optional and, if specified, is gonna be the default country for formatting non-international phone numbers. The formatter instance provides two methods:

@@ -342,3 +537,3 @@ * `input(text)` — Takes any text, parses it and appends the digits to the input. Returns the formatted phone number.

* `country` — Phone number [country](https://github.com/catamphetamine/libphonenumber-js#country-code).
* `country` — Phone number [country](#country-code).
* `getNationalNumber()` — Returns the national (significant) number part of the phone number.

@@ -385,2 +580,6 @@ * `getTemplate()` — Returns the template used to format the output (or `undefined`). Digits (and the `+` sign, if present) are denoted by `x`-es.

New API returns phone numbers as instances of [`PhoneNumber`](#phonenumber) class. Legacy API returns phone numbers as objects having shape `{ country, phone }`.
New API example:
```js

@@ -390,5 +589,43 @@ import { findNumbers } from 'libphonenumber-js'

findNumbers(`
The number is +7 (800) 555-35-35 and
not (213) 373-4253 as written
in the document.
The number is +7 (800) 555-35-35
and not (213) 373-4253 ext. 1234
as it's written in the document.
`, 'US', {
v2: true
})
// Outputs:
//
// [{
// number: PhoneNumber {
// country: 'RU',
// countryCallingCode: '7',
// number: '+78005553535',
// nationalNumber: '8005553535'
// },
// startsAt : 14,
// endsAt : 32
// }, {
// number: PhoneNumber {
// country: 'US',
// countryCallingCode: '1',
// number: '+12133734253',
// nationalNumber: '2133734253',
// ext: '1234'
// },
// startsAt : 41,
// endsAt : 65
// }]
```
<details>
<summary>Legacy API example</summary>
```js
import { findNumbers } from 'libphonenumber-js'
findNumbers(`
The number is +7 (800) 555-35-35
and not (213) 373-4253 ext. 1234
as it's written in the document.
`, 'US')

@@ -407,9 +644,16 @@

// country : 'US',
// ext : '1234',
// startsAt : 41,
// endsAt : 55
// endsAt : 65
// }]
```
</details>
####
By default it processes the whole text and then outputs the phone numbers found. If the text is very big (say, a hundred thousand characters) then it might freeze the user interface for a couple of seconds. To avoid such lags one can employ iterators to perform the search asynchronously (e.g. using `requestIdleCallback` or `requestAnimationFrame`).
<details>
<summary>Asynchronous search example</summary>
ES6 iterator:

@@ -422,3 +666,3 @@

The number is +7 (800) 555-35-35 and
not (213) 373-4253 as written
not (213) 373-4253 as it's written
in the document.

@@ -443,3 +687,3 @@ `

The number is +7 (800) 555-35-35 and
not (213) 373-4253 as written
not (213) 373-4253 as it's written
in the document.

@@ -463,3 +707,6 @@ `, {

```
</details>
####
Although Google's javascript port doesn't have the `findNumbers()` functionality the Java and C++ ports do. I guess Google just doesn't need to crawl phone numbers on Node.js because they can afford to hire a Java/C++ developer to do that. Still, javascript nowadays is the most popular programming language given its simplicity and user-friendliness. The `findNumbers()` function provided is a port of Google's `PhoneNumberMatcher.java` into javascript.

@@ -469,6 +716,11 @@

Determines phone number type (fixed line, mobile, toll free, etc). This function will work if `--extended` (or relevant `--types`) metadata is available (see [Metadata](#metadata) section of this document). The regular expressions used to differentiate between various phone number types consume a lot of space (two thirds of the total size of the `--extended` library build) therefore they're not included in the bundle by default.
_(legacy API)_
See the description for [`PhoneNumber.getType()`](#gettype).
The `number` argument can be either a result of the `parseNumber()` function call — `{ country, phone }` — or a string (phone number digits only) possibly accompanied with the second `defaultCountry` argument.
<details>
<summary>Examples</summary>
```js

@@ -479,5 +731,8 @@ getNumberType('+79160151539') === 'MOBILE'

```
</details>
### isValidNumber(number, [defaultCountry])
_(legacy API)_
Checks if a phone number is valid, the validation is more strict than `parseNumber()`.

@@ -487,2 +742,5 @@

<details>
<summary>Examples</summary>
```js

@@ -497,9 +755,21 @@ isValidNumber('+12133734253') === true

```
</details>
The difference between using `parseNumber()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany `parseNumber('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany. But, if the metadata is compiled with `--extended` (or relevant `--types`) flag (see [Metadata](#metadata) section of this document) then `isValidNumber()` is gonna use those precise regular expressions for extensive validation and `isValid('123456', 'DE')` will return `false` because the phone number `123456` doesn't actually exist in Germany.
####
<details>
<summary>The difference between using <code>parseNumber()</code> and <code>isValidNumber()</code></summary>
The difference between using `parseNumber()` and `isValidNumber()` for phone number validation is that `isValidNumber()` also checks the precise regular expressions of possible phone numbers for a country. For example, for Germany `parseNumber('123456', 'DE')` would return `{ country: 'DE', phone: '123456' }` because this phone number matches the general phone number rules for Germany. But, if the metadata is compiled with `--extended` (or relevant `--types`) flag (see [Customizing metadata](#customizing-metadata) section of this document) then `isValidNumber()` is gonna use those precise regular expressions for extensive validation and `isValid('123456', 'DE')` will return `false` because the phone number `123456` doesn't actually exist in Germany.
This is how it is implemented in the original Google's [`libphonenumber`](https://static.javadoc.io/com.googlecode.libphonenumber/libphonenumber/8.9.1/com/google/i18n/phonenumbers/PhoneNumberUtil.html#parse-java.lang.CharSequence-java.lang.String-): `parseNumber()` parses phone numbers and loosely validates them while `isValidNumber()` validates phone numbers precisely (provided the precise regular expressions are included in metadata).
The precise regular expressions aren't included in the default metadata because that would cause the default metadata to grow twice in its size: the complete ("full") metadata size is about 145 kilobytes while the reduced ("default") metadata size is about 77 kilobytes. Hence in the default configuration `isValidNumber()` performs absolutely the same "lite" validation as `parseNumber()`. For enabling extensive phone number validation the simplest way is to import functions from `libphonenumber-js/custom` module and supply them with `libphonenumber-js/metadata.full.json`. For generating custom metadata see the instructions provided in the [Customizing metadata](#customizing-metadata) section of this document.
</details>
####
<details>
<summary><code>isValidNumberForRegion()</code></summary>
The optional `defaultCountry` argument is the _default_ country, i.e. it does not restrict to just that country, e.g. in those cases where several countries share the same phone numbering rules (NANPA, Britain, etc). For example, even though the number `07624 369230` belongs to the Isle of Man ("IM" country code) calling `isValidNumber('07624369230', 'GB')` still returns `true` because the country is not restricted to `GB`, it's just that `GB` is the default one for the phone numbering rules. For restricting the country see `isValidNumberForRegion()` though restricting a country [might not be a good idea](https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion).

@@ -518,2 +788,3 @@

```
</details>

@@ -536,5 +807,18 @@ #### Using phone number validation feature

## getExampleNumber(country, examples)
Returns an instance of [`PhoneNumber`](#phonenumber) class.
```js
import examples from 'libphonenumber-js/examples.mobile.json'
import { getExampleNumber } from 'libphonenumber-js'
const phoneNumber = getExampleNumber('RU', examples)
phoneNumber.formatNational() === '8 (912) 345-67-89'
```
### getCountryCallingCode(country)
There have been requests for a function returning a [country calling code](https://github.com/catamphetamine/libphonenumber-js#country-calling-code) by [country code](https://github.com/catamphetamine/libphonenumber-js#country-code).
There have been requests for a function returning a [country calling code](#country-calling-code) by [country code](#country-code).

@@ -591,10 +875,2 @@ ```js

## Examples
For those asking for phone number examples for use in `<input placeholder/>`s: see `libphonenumber-js/examples.mobile.json`.
<!-- ## To do
Everything's done -->
## Bug reporting

@@ -669,6 +945,6 @@

This library comes prepackaged with three flavours of metadata
This library comes prepackaged with three flavours of [metadata](#metadata):
* `metadata.full.json` — contains everything, including all regular expressions for precise phone number validation and getting phone number type, but weighs `145 kilobytes`.
* `metadata.min.json` — (default) the minimal one, doesn't contain regular expressions for precise phone number validation and getting phone number type, weighs `80 kilobytes`.
* `metadata.full.json` — contains everything, including all regular expressions for precise phone number validation and getting phone number type, but weighs `140 kilobytes`.
* `metadata.min.json` — (default) the minimal one, doesn't contain regular expressions for precise phone number validation and getting phone number type, weighs `75 kilobytes`.
* `metadata.mobile.json` — contains regular expressions for precise **mobile** phone number validation, weighs `105 kilobytes`.

@@ -678,2 +954,5 @@

<details>
<summary>See generate custom metadata instructions.</summary>
First, add metadata generation script to **your project's** `package.json`

@@ -697,5 +976,9 @@

* `--types ...` argument may be passed instead of `--extended` to only include the precise phone number type regular expressions for a specific set of phone number types (a comma-separated list, e.g. `--types mobile,fixed_line`). [The complete list of phone number types](https://github.com/catamphetamine/libphonenumber-js/blob/master/source/tools/generate.js#L6-L18).
</details>
Then use the generated `metadata.min.json` with the exported "custom" functions.
####
<details>
<summary>Then use the generated <code>metadata.min.json</code> with the exported "custom" functions.</summary>
In ES6 that would be

@@ -781,2 +1064,3 @@

```
</details>

@@ -783,0 +1067,0 @@ Metadata should be re-generated each time the project is being deployed because Google constantly updates their metadata.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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

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

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