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.2.22 to 1.3.0

build/getNumberType.js

25

build/AsYouType.js

@@ -27,4 +27,8 @@ 'use strict';

var _types = require('./types');
var _getNumberType = require('./getNumberType');
var _parseIncompletePhoneNumber = require('./parseIncompletePhoneNumber');
var _parseIncompletePhoneNumber2 = _interopRequireDefault(_parseIncompletePhoneNumber);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

return this.process_input((0, _common.parse_phone_number_digits)(extracted_number));
return this.process_input((0, _parseIncompletePhoneNumber2.default)(extracted_number));
}

@@ -547,5 +551,5 @@ }, {

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

@@ -557,2 +561,11 @@ if (!countryCallingCode) {

this.countryCallingCode = countryCallingCode;
// Sometimes people erroneously write national prefix
// as part of an international number, e.g. +44 (0) ....
// This violates the standards for international phone numbers,
// so "As You Type" formatter assumes no national prefix
// when parsing a phone number starting from `+`.
// Even if it did attempt to filter-out that national prefix
// it would look weird for a user trying to enter a digit
// because from user's perspective the keyboard "wouldn't be working".
this.national_number = number;

@@ -613,3 +626,3 @@

value: function is_possible_number(number) {
var validation_result = (0, _types.check_number_length_for_type)(number, undefined, this.metadata);
var validation_result = (0, _getNumberType.check_number_length_for_type)(number, undefined, this.metadata);
switch (validation_result) {

@@ -616,0 +629,0 @@ case 'IS_POSSIBLE':

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

exports.parseDigit = parseDigit;
exports.parse_phone_number_digits = parse_phone_number_digits;
exports.parse_national_number_and_country_calling_code = parse_national_number_and_country_calling_code;
exports.extractCountryCallingCode = extractCountryCallingCode;
exports.matches_entirely = matches_entirely;

@@ -16,2 +15,8 @@ exports.create_extension_pattern = create_extension_pattern;

var _parseIncompletePhoneNumber = require('./parseIncompletePhoneNumber');
var _parseIncompletePhoneNumber2 = _interopRequireDefault(_parseIncompletePhoneNumber);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// `DASHES` will be right after the opening square bracket of the "character class"

@@ -99,23 +104,11 @@ var DASHES = '-\u2010-\u2015\u2212\u30FC\uFF0D';

/**
* Drops all punctuation leaving only digits and the leading `+` sign (if any).
* Also converts wide-ascii and arabic-indic numerals to conventional numerals.
*
* E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
*
* @param {string} number
* @return {string}
*/
function parse_phone_number_digits(number) {
return (LEADING_PLUS_CHARS_PATTERN.test(number) ? '+' : '') + drop_and_substitute_characters(number, DIGITS);
}
// Parses a formatted phone number
// and returns `{ country_calling_code, number }`
// where `number` is the national (significant) phone number.
// and returns `{ countryCallingCode, number }`
// where `number` is just the "number" part
// which is left after extracting `countryCallingCode`
// and is not necessarily a "national (significant) number"
// and might as well contain national prefix.
//
// (aka `maybeExtractCountryPhoneCode`)
//
function parse_national_number_and_country_calling_code(number, country, metadata) {
number = parse_phone_number_digits(number);
function extractCountryCallingCode(number, country, metadata) {
number = (0, _parseIncompletePhoneNumber2.default)(number);

@@ -134,3 +127,4 @@ if (!number) {

// If an IDD prefix was stripped then
// convert the number to international one.
// convert the number to international one
// for subsequent parsing.
if (numberWithoutIDD && numberWithoutIDD !== number) {

@@ -188,36 +182,2 @@ number = '+' + numberWithoutIDD;

// For any character not being part of `replacements`
// it is removed from the phone number.
function drop_and_substitute_characters(text, replacements) {
var replaced = '';
// Using `.split('')` to iterate through a string here
// to avoid requiring `Symbol.iterator` polyfill.
// `.split('')` is generally not safe for Unicode,
// but in this particular case for `digits` it is safe.
// for (const character of text)
for (var _iterator = text.split(''), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var character = _ref;
var replacement = replacements[character.toUpperCase()];
if (replacement) {
replaced += replacement;
}
}
return replaced;
}
// The RFC 3966 format for extensions.

@@ -224,0 +184,0 @@ var RFC3966_EXTN_PREFIX = ';ext=';

@@ -62,6 +62,8 @@ 'use strict';

var _parse_national_numbe = (0, _common.parse_national_number_and_country_calling_code)(input.phone, null, metadata),
countryCallingCode = _parse_national_numbe.countryCallingCode,
number = _parse_national_numbe.number;
// `number` is a national (significant) number in this case.
var _extractCountryCallin = (0, _common.extractCountryCallingCode)(input.phone, null, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
number = _extractCountryCallin.number;
countryCallingCode = countryCallingCode || input.countryCallingCode;

@@ -68,0 +70,0 @@

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

var number_type = (0, _types2.default)(number, metadata.metadata);
var number_type = (0, _getNumberType2.default)(number, metadata.metadata);
var is_valid_number = number_type === number;

@@ -66,3 +66,3 @@

if (can_be_internationally_dialled(number) && (0, _types.check_number_length_for_type)(number.phone, undefined, metadata) !== 'TOO_SHORT') {
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);

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

var _types = require('./types');
var _getNumberType = require('./getNumberType');
var _types2 = _interopRequireDefault(_types);
var _getNumberType2 = _interopRequireDefault(_getNumberType);

@@ -121,0 +121,0 @@ var _getCountryCallingCode = require('./getCountryCallingCode');

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

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

@@ -25,6 +29,10 @@

var _types = require('./types');
var _getCountryCallingCode = require('./getCountryCallingCode');
var _types2 = _interopRequireDefault(_types);
var _getCountryCallingCode2 = _interopRequireDefault(_getCountryCallingCode);
var _getNumberType = require('./getNumberType');
var _getNumberType2 = _interopRequireDefault(_getNumberType);
var _RFC = require('./RFC3966');

@@ -365,3 +373,3 @@

// fixed-line/mobile/etc regular expressions.
else if ((0, _types2.default)({ phone: national_phone_number, country: country }, metadata.metadata)) {
else if ((0, _getNumberType2.default)({ phone: national_phone_number, country: country }, metadata.metadata)) {
return country;

@@ -449,3 +457,3 @@ }

function is_possible_number(national_number, is_international, metadata) {
switch ((0, _types.check_number_length_for_type)(national_number, undefined, metadata)) {
switch ((0, _getNumberType.check_number_length_for_type)(national_number, undefined, metadata)) {
case 'IS_POSSIBLE':

@@ -508,5 +516,5 @@ return true;

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

@@ -519,47 +527,32 @@ if (!number) {

// Attempt to extract country from international phone number.
if (countryCallingCode) {
// Sometimes there are several countries
// corresponding to the same country phone code
// (e.g. NANPA countries all having `1` country phone code).
// Therefore, to reliably determine the exact country,
// national (significant) number should have been parsed first.
//
// When `metadata.json` is generated, all "ambiguous" country phone codes
// get their countries populated with the full set of
// "phone number type" regular expressions.
//
country = find_country_code(countryCallingCode, number, metadata);
if (country) {
metadata.country(country);
} else {
// Formatting information for regions which share
// a country calling code is contained by only one region
// for performance reasons. For example, for NANPA region
// ("North American Numbering Plan Administration",
// which includes USA, Canada, Cayman Islands, Bahamas, etc)
// it will be contained in the metadata for `US`.
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
}
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
} else if (default_country) {
metadata.country(default_country);
country = default_country;
metadata.country(country);
countryCallingCode = metadata.countryCallingCode();
countryCallingCode = (0, _getCountryCallingCode2.default)(default_country, metadata.metadata);
} else return {};
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
var _parse_national_numbe = parse_national_number(number, metadata),
national_number = _parse_national_numbe.national_number,
carrier_code = _parse_national_numbe.carrier_code;
var _parse_national_numbe2 = parse_national_number(number, metadata),
national_number = _parse_national_numbe2.national_number,
carrier_code = _parse_national_numbe2.carrier_code;
// Sometimes there are several countries
// corresponding to the same country phone code
// (e.g. NANPA countries all having `1` country phone code).
// Therefore, to reliably determine the exact country,
// national (significant) number should have been parsed first.
//
// When `metadata.json` is generated, all "ambiguous" country phone codes
// get their countries populated with the full set of
// "phone number type" regular expressions.
//
var exactCountry = find_country_code(countryCallingCode, national_number, metadata);
if (exactCountry) {
country = exactCountry;
metadata.country(country);
}
return {

@@ -574,3 +567,3 @@ country: country,

function parse_national_number(number, metadata) {
var national_number = (0, _common.parse_phone_number_digits)(number);
var national_number = (0, _parseIncompletePhoneNumber2.default)(number);
var carrier_code = void 0;

@@ -599,3 +592,3 @@

// a valid short number.
switch ((0, _types.check_number_length_for_type)(potential_national_number, undefined, metadata)) {
switch ((0, _getNumberType.check_number_length_for_type)(potential_national_number, undefined, metadata)) {
case 'TOO_SHORT':

@@ -602,0 +595,0 @@ // case 'IS_POSSIBLE_LOCAL_ONLY':

@@ -13,2 +13,5 @@ 'use strict';

* Parses phone number characters from a string.
* Drops all punctuation leaving only digits and the leading `+` sign (if any).
* Also converts wide-ascii and arabic-indic numerals to conventional numerals.
* E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
* @param {string} string

@@ -15,0 +18,0 @@ * @return {string}

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = is_valid;
exports.default = isValidNumber;

@@ -12,46 +12,62 @@ var _parse = require('./parse');

var _types = require('./types');
var _getNumberType = require('./getNumberType');
var _types2 = _interopRequireDefault(_types);
var _getNumberType2 = _interopRequireDefault(_getNumberType);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Checks if a given phone number is valid
//
// Example use cases:
//
// ```js
// is_valid('8005553535', 'RU')
// is_valid('8005553535', 'RU', metadata)
// is_valid({ phone: '8005553535', country: 'RU' })
// is_valid({ phone: '8005553535', country: 'RU' }, metadata)
// is_valid('+78005553535')
// is_valid('+78005553535', metadata)
// ```
//
function is_valid(arg_1, arg_2, arg_3) {
var _sort_out_arguments = (0, _types.sort_out_arguments)(arg_1, arg_2, arg_3),
input = _sort_out_arguments.input,
metadata = _sort_out_arguments.metadata;
/**
* Checks if a given phone number is valid.
*
* If the `number` is a string, it will be parsed to an object,
* but only if it contains only valid phone number characters (including punctuation).
* If the `number` is an object, it is used as is.
*
* 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', metadata)`
* 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
*
* Examples:
*
* ```js
* isValidNumber('+78005553535', metadata)
* isValidNumber('8005553535', 'RU', metadata)
* isValidNumber('88005553535', 'RU', metadata)
* isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
* ```
*/
function isValidNumber(arg_1, arg_2, arg_3) {
var _sort_out_arguments = (0, _getNumberType.sort_out_arguments)(arg_1, arg_2, arg_3),
input = _sort_out_arguments.input,
metadata = _sort_out_arguments.metadata;
if (!input) {
return false;
}
if (!input) {
return false;
}
if (!input.country) {
return false;
}
if (!input.country) {
return false;
}
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
metadata.country(input.country);
metadata.country(input.country);
if (metadata.hasTypes()) {
return (0, _types2.default)(input, metadata.metadata) !== undefined;
}
if (metadata.hasTypes()) {
return (0, _getNumberType2.default)(input, metadata.metadata) !== undefined;
}
return true;
return true;
}
//# sourceMappingURL=validate.js.map

@@ -11,2 +11,9 @@ <!-- (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. -->

1.3.0 / 25.07.2018
==================
* Fixed `parseNumber()`, `isValidNumber()` and `getNumberType()` in some rare cases (made them a bit less strict where it fits): previously they were treating `defaultCountry` argument as "the country" in case of local numbers, e.g. `isValidNumber('07624 369230', 'GB')` would be `false` because `07624 369230` number belongs to `IM` (the Isle of Man). While `IM` is not `GB` it should still be `true` because `GB` is the _default_ country, without it necessarily being _the_ country.
* Added a new function `isValidNumberForRegion(number, country)` which mimics [Google's `libphonenumber`'s one](https://github.com/googlei18n/libphonenumber/blob/master/FAQ.md#when-should-i-use-isvalidnumberforregion).
1.2.13 / 30.05.2018

@@ -13,0 +20,0 @@ ===================

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

exports.formatNumber = require('./build/format').default
exports.getNumberType = require('./build/types').default
exports.getNumberType = require('./build/getNumberType').default
exports.isValidNumber = require('./build/validate').default
exports.isValidNumberForRegion = require('./build/isValidNumberForRegion').default
exports.findPhoneNumbers = require('./build/findPhoneNumbers').default

@@ -17,0 +18,0 @@ exports.searchPhoneNumbers = require('./build/findPhoneNumbers').searchPhoneNumbers

@@ -12,3 +12,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; }; }();

import { matches_entirely, VALID_DIGITS, VALID_PUNCTUATION, PLUS_CHARS, parse_phone_number_digits, parse_national_number_and_country_calling_code } from './common';
import { matches_entirely, VALID_DIGITS, VALID_PUNCTUATION, PLUS_CHARS, extractCountryCallingCode } from './common';

@@ -19,4 +19,6 @@ import { extract_formatted_phone_number, find_country_code, strip_national_prefix_and_carrier_code } from './parse';

import { check_number_length_for_type } from './types';
import { check_number_length_for_type } from './getNumberType';
import parseIncompletePhoneNumber from './parseIncompletePhoneNumber';
// Used in phone number format template creation.

@@ -110,3 +112,3 @@ // Could be any digit, I guess.

return this.process_input(parse_phone_number_digits(extracted_number));
return this.process_input(parseIncompletePhoneNumber(extracted_number));
}

@@ -535,5 +537,5 @@ }, {

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

@@ -545,2 +547,11 @@ if (!countryCallingCode) {

this.countryCallingCode = countryCallingCode;
// Sometimes people erroneously write national prefix
// as part of an international number, e.g. +44 (0) ....
// This violates the standards for international phone numbers,
// so "As You Type" formatter assumes no national prefix
// when parsing a phone number starting from `+`.
// Even if it did attempt to filter-out that national prefix
// it would look weird for a user trying to enter a digit
// because from user's perspective the keyboard "wouldn't be working".
this.national_number = number;

@@ -547,0 +558,0 @@

import { stripIDDPrefix } from './IDD';
import parseIncompletePhoneNumber from './parseIncompletePhoneNumber';
// `DASHES` will be right after the opening square bracket of the "character class"

@@ -85,23 +87,11 @@ var DASHES = '-\u2010-\u2015\u2212\u30FC\uFF0D';

/**
* Drops all punctuation leaving only digits and the leading `+` sign (if any).
* Also converts wide-ascii and arabic-indic numerals to conventional numerals.
*
* E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
*
* @param {string} number
* @return {string}
*/
export function parse_phone_number_digits(number) {
return (LEADING_PLUS_CHARS_PATTERN.test(number) ? '+' : '') + drop_and_substitute_characters(number, DIGITS);
}
// Parses a formatted phone number
// and returns `{ country_calling_code, number }`
// where `number` is the national (significant) phone number.
// and returns `{ countryCallingCode, number }`
// where `number` is just the "number" part
// which is left after extracting `countryCallingCode`
// and is not necessarily a "national (significant) number"
// and might as well contain national prefix.
//
// (aka `maybeExtractCountryPhoneCode`)
//
export function parse_national_number_and_country_calling_code(number, country, metadata) {
number = parse_phone_number_digits(number);
export function extractCountryCallingCode(number, country, metadata) {
number = parseIncompletePhoneNumber(number);

@@ -120,3 +110,4 @@ if (!number) {

// If an IDD prefix was stripped then
// convert the number to international one.
// convert the number to international one
// for subsequent parsing.
if (numberWithoutIDD && numberWithoutIDD !== number) {

@@ -174,36 +165,2 @@ number = '+' + numberWithoutIDD;

// For any character not being part of `replacements`
// it is removed from the phone number.
function drop_and_substitute_characters(text, replacements) {
var replaced = '';
// Using `.split('')` to iterate through a string here
// to avoid requiring `Symbol.iterator` polyfill.
// `.split('')` is generally not safe for Unicode,
// but in this particular case for `digits` it is safe.
// for (const character of text)
for (var _iterator = text.split(''), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var character = _ref;
var replacement = replacements[character.toUpperCase()];
if (replacement) {
replaced += replacement;
}
}
return replaced;
}
// The RFC 3966 format for extensions.

@@ -210,0 +167,0 @@ var RFC3966_EXTN_PREFIX = ';ext=';

@@ -10,3 +10,3 @@ 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 { parse_national_number_and_country_calling_code, VALID_PUNCTUATION, matches_entirely } from './common';
import { extractCountryCallingCode, VALID_PUNCTUATION, matches_entirely } from './common';

@@ -48,6 +48,8 @@ import { getIDDPrefix } from './IDD';

var _parse_national_numbe = parse_national_number_and_country_calling_code(input.phone, null, metadata),
countryCallingCode = _parse_national_numbe.countryCallingCode,
number = _parse_national_numbe.number;
// `number` is a national (significant) number in this case.
var _extractCountryCallin = extractCountryCallingCode(input.phone, null, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
number = _extractCountryCallin.number;
countryCallingCode = countryCallingCode || input.countryCallingCode;

@@ -54,0 +56,0 @@

@@ -10,3 +10,3 @@ // This function is copy-pasted from

import format from './format';
import get_number_type, { check_number_length_for_type } from './types';
import get_number_type, { check_number_length_for_type } from './getNumberType';
import getCountryCallingCode from './getCountryCallingCode';

@@ -13,0 +13,0 @@ import { REGION_CODE_FOR_NON_GEO_ENTITY } from './common';

@@ -8,8 +8,12 @@ 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 { parse_phone_number_digits, parse_national_number_and_country_calling_code, VALID_DIGITS, VALID_PUNCTUATION, PLUS_CHARS, MAX_LENGTH_FOR_NSN, matches_entirely, create_extension_pattern } from './common';
import { extractCountryCallingCode, VALID_DIGITS, VALID_PUNCTUATION, PLUS_CHARS, MAX_LENGTH_FOR_NSN, matches_entirely, create_extension_pattern } from './common';
import parseIncompletePhoneNumber from './parseIncompletePhoneNumber';
import Metadata from './metadata';
import get_number_type, { check_number_length_for_type } from './types';
import getCountryCallingCode from './getCountryCallingCode';
import get_number_type, { check_number_length_for_type } from './getNumberType';
import { parseRFC3966 } from './RFC3966';

@@ -489,5 +493,5 @@

function parse_phone_number(formatted_phone_number, default_country, metadata) {
var _parse_national_numbe = parse_national_number_and_country_calling_code(formatted_phone_number, default_country, metadata),
countryCallingCode = _parse_national_numbe.countryCallingCode,
number = _parse_national_numbe.number;
var _extractCountryCallin = extractCountryCallingCode(formatted_phone_number, default_country, metadata),
countryCallingCode = _extractCountryCallin.countryCallingCode,
number = _extractCountryCallin.number;

@@ -500,47 +504,32 @@ if (!number) {

// Attempt to extract country from international phone number.
if (countryCallingCode) {
// Sometimes there are several countries
// corresponding to the same country phone code
// (e.g. NANPA countries all having `1` country phone code).
// Therefore, to reliably determine the exact country,
// national (significant) number should have been parsed first.
//
// When `metadata.json` is generated, all "ambiguous" country phone codes
// get their countries populated with the full set of
// "phone number type" regular expressions.
//
country = find_country_code(countryCallingCode, number, metadata);
if (country) {
metadata.country(country);
} else {
// Formatting information for regions which share
// a country calling code is contained by only one region
// for performance reasons. For example, for NANPA region
// ("North American Numbering Plan Administration",
// which includes USA, Canada, Cayman Islands, Bahamas, etc)
// it will be contained in the metadata for `US`.
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
}
metadata.chooseCountryByCountryCallingCode(countryCallingCode);
} else if (default_country) {
metadata.country(default_country);
country = default_country;
metadata.country(country);
countryCallingCode = metadata.countryCallingCode();
countryCallingCode = getCountryCallingCode(default_country, metadata.metadata);
} else return {};
// Parsing national prefixes and carrier codes
// is only required for local phone numbers
// but some people don't understand that
// and sometimes write international phone numbers
// with national prefixes (or maybe even carrier codes).
// http://ucken.blogspot.ru/2016/03/trunk-prefixes-in-skype4b.html
// Google's original library forgives such mistakes
// and so does this library, because it has been requested:
// https://github.com/catamphetamine/libphonenumber-js/issues/127
var _parse_national_numbe = parse_national_number(number, metadata),
national_number = _parse_national_numbe.national_number,
carrier_code = _parse_national_numbe.carrier_code;
var _parse_national_numbe2 = parse_national_number(number, metadata),
national_number = _parse_national_numbe2.national_number,
carrier_code = _parse_national_numbe2.carrier_code;
// Sometimes there are several countries
// corresponding to the same country phone code
// (e.g. NANPA countries all having `1` country phone code).
// Therefore, to reliably determine the exact country,
// national (significant) number should have been parsed first.
//
// When `metadata.json` is generated, all "ambiguous" country phone codes
// get their countries populated with the full set of
// "phone number type" regular expressions.
//
var exactCountry = find_country_code(countryCallingCode, national_number, metadata);
if (exactCountry) {
country = exactCountry;
metadata.country(country);
}
return {

@@ -555,3 +544,3 @@ country: country,

function parse_national_number(number, metadata) {
var national_number = parse_phone_number_digits(number);
var national_number = parseIncompletePhoneNumber(number);
var carrier_code = void 0;

@@ -558,0 +547,0 @@

@@ -5,2 +5,5 @@ import { parseDigit } from './common';

* Parses phone number characters from a string.
* Drops all punctuation leaving only digits and the leading `+` sign (if any).
* Also converts wide-ascii and arabic-indic numerals to conventional numerals.
* E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
* @param {string} string

@@ -7,0 +10,0 @@ * @return {string}

import parse, { is_viable_phone_number } from './parse';
import get_number_type, { sort_out_arguments } from './types';
import get_number_type, { sort_out_arguments } from './getNumberType';
// Checks if a given phone number is valid
//
// Example use cases:
//
// ```js
// is_valid('8005553535', 'RU')
// is_valid('8005553535', 'RU', metadata)
// is_valid({ phone: '8005553535', country: 'RU' })
// is_valid({ phone: '8005553535', country: 'RU' }, metadata)
// is_valid('+78005553535')
// is_valid('+78005553535', metadata)
// ```
//
export default function is_valid(arg_1, arg_2, arg_3) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3),
input = _sort_out_arguments.input,
metadata = _sort_out_arguments.metadata;
/**
* Checks if a given phone number is valid.
*
* If the `number` is a string, it will be parsed to an object,
* but only if it contains only valid phone number characters (including punctuation).
* If the `number` is an object, it is used as is.
*
* 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', metadata)`
* 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
*
* Examples:
*
* ```js
* isValidNumber('+78005553535', metadata)
* isValidNumber('8005553535', 'RU', metadata)
* isValidNumber('88005553535', 'RU', metadata)
* isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
* ```
*/
export default function isValidNumber(arg_1, arg_2, arg_3) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3),
input = _sort_out_arguments.input,
metadata = _sort_out_arguments.metadata;
if (!input) {
return false;
}
if (!input) {
return false;
}
if (!input.country) {
return false;
}
if (!input.country) {
return false;
}
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
if (!metadata.hasCountry(input.country)) {
throw new Error('Unknown country: ' + input.country);
}
metadata.country(input.country);
metadata.country(input.country);
if (metadata.hasTypes()) {
return get_number_type(input, metadata.metadata) !== undefined;
}
if (metadata.hasTypes()) {
return get_number_type(input, metadata.metadata) !== undefined;
}
return true;
return true;
}
//# sourceMappingURL=validate.js.map

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

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

@@ -56,0 +63,0 @@ {

@@ -5,4 +5,5 @@ import metadata from './metadata.min.json'

import formatNumberCustom from './es6/format'
import getNumberTypeCustom from './es6/types'
import getNumberTypeCustom from './es6/getNumberType'
import isValidNumberCustom from './es6/validate'
import isValidNumberForRegionCustom from './es6/isValidNumberForRegion'
import findPhoneNumbersCustom, { searchPhoneNumbers as searchPhoneNumbersCustom, PhoneNumberSearch as PhoneNumberSearchCustom } from './es6/findPhoneNumbers'

@@ -61,2 +62,9 @@ import AsYouTypeCustom from './es6/AsYouType'

export function isValidNumberForRegion()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return isValidNumberForRegionCustom.apply(this, parameters)
}
export function findPhoneNumbers()

@@ -120,7 +128,7 @@ {

export { default as formatCustom } from './es6/format'
export { default as isValidNumberCustom } from './es6/validate'
export { default as isValidNumberCustom } from './es6/validate'
export { default as findPhoneNumbersCustom } from './es6/findPhoneNumbers'
export { searchPhoneNumbers as searchPhoneNumbersCustom } from './es6/findPhoneNumbers'
export { PhoneNumberSearch as PhoneNumberSearchCustom } from './es6/findPhoneNumbers'
export { default as getNumberTypeCustom } from './es6/types'
export { default as getNumberTypeCustom } from './es6/getNumberType'
export { default as getCountryCallingCodeCustom } from './es6/getCountryCallingCode'

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

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

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

@@ -435,5 +435,6 @@ # libphonenumber-js

The `number` argument can be either a result of the `parseNumber()` function call — `{ country, phone }` — or a string possibly accompanied with the second `defaultCountry` argument (the string is gonna be parsed in this case).
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.
```js
getNumberType('+79160151539') === 'MOBILE'
getNumberType('9160151539', 'RU') === 'MOBILE'

@@ -447,10 +448,10 @@ getNumberType({ phone: '9160151539', country: 'RU' }) === 'MOBILE'

The `number` argument can be either a result of the `parseNumber()` function call — `{ country, phone }` — or a string possibly accompanied with the second `defaultCountry` argument (the string is gonna be parsed in this case).
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.
```js
isValidNumber('+1-213-373-4253') === true
isValidNumber('+1-213-373') === false
isValidNumber('+12133734253') === true
isValidNumber('+1213373') === false
isValidNumber('(213) 373-4253', 'US') === true
isValidNumber('(213) 37', 'US') === false
isValidNumber('2133734253', 'US') === true
isValidNumber('21337', 'US') === false

@@ -466,2 +467,16 @@ isValidNumber({ phone: '2133734253', country: 'US' }) === true

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).
```js
// Even though '07624 369230' number belongs to the Isle of Man ("IM")
// the `defaultCountry` argument "GB" still works here because
// "GB" and "IM" both share the same phone numbering rules ("+44").
isValidNumber('07624369230', 'GB') === true
isValidNumber('07624369230', 'IM') === true
// Imposing country restrictions.
isValidNumberForRegion('07624369230', 'GB') === false
isValidNumberForRegion('07624369230', 'IM') === true
```
#### Using phone number validation feature

@@ -551,2 +566,4 @@

There is also a possibility of this library's demo metadata being outdated, or this library's metadata lagging behind Google's (I have to update it manually from time to time due to `ssh-agent` not working properly on Windows).
## TypeScript

@@ -553,0 +570,0 @@

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