libphonenumber-js
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -44,2 +44,3 @@ 'use strict'; | ||
var DIGIT_PLACEHOLDER_MATCHER = new RegExp(DIGIT_PLACEHOLDER); | ||
var DIGIT_PLACEHOLDER_MATCHER_GLOBAL = new RegExp(DIGIT_PLACEHOLDER, 'g'); | ||
@@ -252,19 +253,11 @@ // A pattern that is used to match character classes in regular expressions. | ||
// If no new phone number format could be chosen, | ||
// then can't format the phone. | ||
if (!this.current_format) { | ||
this.able_to_format = false; | ||
// Return raw phone number | ||
return this.parsed_input; | ||
} | ||
// If could format the next (current) digit | ||
// using the previously chosen phone number format | ||
// then return the formatted number so far. | ||
if (national_number_formatted_with_previous_format) { | ||
if (this.current_format) { | ||
return this.full_phone_number(national_number_formatted_with_previous_format); | ||
} | ||
// Couldn't format the supplied national number | ||
// If no new phone number format could be chosen, | ||
// And couldn't format the supplied national number | ||
// using the selected phone number pattern. | ||
@@ -296,3 +289,3 @@ // Return raw phone number | ||
this.country_metadata = undefined; | ||
this.available_formats = undefined; | ||
this.available_formats = []; | ||
} | ||
@@ -355,6 +348,2 @@ | ||
value: function initialize_phone_number_formats_for_this_country() { | ||
if (!this.country_metadata) { | ||
return; | ||
} | ||
// Get all "eligible" phone number formats for this country | ||
@@ -507,66 +496,33 @@ this.available_formats = (0, _metadata3.get_formats)(this.country_metadata).filter(function (format) { | ||
} | ||
}, { | ||
key: 'extract_national_prefix', | ||
value: function extract_national_prefix() { | ||
this.national_prefix = ''; | ||
// Some national prefixes are a substring of others. If extracting the shorter | ||
// national prefix doesn't result in a number we can format, | ||
// we try to see if we can extract a longer version here. | ||
if (!this.country_metadata) { | ||
return; | ||
} | ||
}, { | ||
key: 'extract_longer_national_prefix', | ||
value: function extract_longer_national_prefix() { | ||
if (!this.national_prefix) { | ||
var national_prefix_for_parsing = (0, _metadata3.get_national_prefix_for_parsing)(this.country_metadata); | ||
if (!national_prefix_for_parsing) { | ||
return; | ||
} | ||
// Put the extracted national prefix back to the national number | ||
// before attempting to extract a new national prefix. | ||
this.national_number = this.national_prefix + this.national_number; | ||
var matches = this.national_number.match(new RegExp('^(?:' + national_prefix_for_parsing + ')')); | ||
var previously_extracted_national_prefix = this.national_prefix; | ||
this.extract_national_prefix(); | ||
return this.national_prefix !== previously_extracted_national_prefix; | ||
} | ||
}, { | ||
key: 'extract_national_prefix', | ||
value: function extract_national_prefix() { | ||
var national_number_starts_at = 0; | ||
if (this.country_metadata) { | ||
// Small performance optimization for NANPA countries | ||
// which can't have `1` (national prefix) as the | ||
// first digit of a national (significant) number | ||
if (this.is_NANPA_number_with_international_prefix()) { | ||
national_number_starts_at = 1; | ||
} else if ((0, _metadata3.get_national_prefix_for_parsing)(this.country_metadata)) { | ||
var national_prefix_for_parsing = new RegExp('^(?:' + (0, _metadata3.get_national_prefix_for_parsing)(this.country_metadata) + ')'); | ||
var matches = this.national_number.match(national_prefix_for_parsing); | ||
// Since some national prefix patterns are entirely optional, check that a | ||
// national prefix could actually be extracted. | ||
if (matches && matches[0]) { | ||
national_number_starts_at = matches[0].length; | ||
} | ||
} | ||
// Since some national prefix patterns are entirely optional, check that a | ||
// national prefix could actually be extracted. | ||
if (!matches || !matches[0]) { | ||
return; | ||
} | ||
var national_number_starts_at = matches[0].length; | ||
this.national_prefix = this.national_number.slice(0, national_number_starts_at); | ||
this.national_number = this.national_number.slice(national_number_starts_at); | ||
return this.national_prefix; | ||
} | ||
// Returns `true` if the current country is a NANPA country and the | ||
// national number begins with the national prefix. | ||
}, { | ||
key: 'is_NANPA_number_with_international_prefix', | ||
value: function is_NANPA_number_with_international_prefix() { | ||
// For NANPA numbers beginning with 1[2-9], treat the 1 as the national | ||
// prefix. The reason is that national significant numbers in NANPA always | ||
// start with [2-9] after the national prefix. Numbers beginning with 1[01] | ||
// can only be short/emergency numbers, which don't need the national prefix. | ||
if ((0, _metadata3.get_phone_code)(this.country_metadata) !== 1) { | ||
return false; | ||
} | ||
return this.national_number[0] === '1' && this.national_number[1] !== '0' && this.national_number[1] !== '1'; | ||
} | ||
}, { | ||
key: 'choose_another_format', | ||
@@ -625,2 +581,4 @@ value: function choose_another_format() { | ||
// (20|3)\d{4}. In those cases we quickly return. | ||
// (Though there's no such format in current metadata) | ||
/* istanbul ignore if */ | ||
if (number_pattern.indexOf('|') >= 0) { | ||
@@ -685,3 +643,3 @@ return; | ||
// Return the formatted phone number so far | ||
return close_dangling_braces(this.formatting_template, digit_pattern_start + 1); | ||
return close_dangling_braces(this.formatting_template, digit_pattern_start + 1).replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' '); | ||
} | ||
@@ -688,0 +646,0 @@ |
@@ -46,4 +46,8 @@ 'use strict'; | ||
var country_metadata = _metadata2.default.countries[input.country]; | ||
var country_metadata = void 0; | ||
if (input.country) { | ||
country_metadata = _metadata2.default.countries[input.country]; | ||
} | ||
var _parse_phone_number_a = (0, _parse.parse_phone_number_and_country_phone_code)(input.phone); | ||
@@ -50,0 +54,0 @@ |
@@ -95,3 +95,3 @@ 'use strict'; | ||
// and national prefix is always `0`, | ||
// then `national_prefix_for_parsing` could be `0([\d]{6})` | ||
// then `national_prefix_for_parsing` could be `0(\d{6})` | ||
// and the corresponding `national_prefix_transform_rule` would be `$1` | ||
@@ -98,0 +98,0 @@ // (which is the default behaviour). |
@@ -206,2 +206,7 @@ 'use strict'; | ||
// If the phone number is not viable, then abort. | ||
if (!formatted_phone_number) { | ||
return {}; | ||
} | ||
var _parse_phone_number_a = parse_phone_number_and_country_phone_code(formatted_phone_number); | ||
@@ -228,14 +233,19 @@ | ||
country_metadata = (0, _metadata3.get_metadata_by_country_phone_code)(country_phone_code, _metadata2.default); | ||
// `country` will be set later, | ||
// because, for example, for NANPA countries | ||
// there are several countries corresponding | ||
// to the same `1` country phone code. | ||
// Therefore, to reliably determine the exact country, | ||
// national (significant) number should be parsed first. | ||
} else if (options.country.default || options.country.restrict) { | ||
country = options.country.default || options.country.restrict; | ||
country_metadata = _metadata2.default.countries[country]; | ||
number = normalize(text); | ||
} | ||
if (!country_metadata) { | ||
return {}; | ||
} | ||
if (!country_metadata) { | ||
return {}; | ||
} | ||
// Sanity check | ||
if (number.length < MIN_LENGTH_FOR_NSN) { | ||
number = normalize(text); | ||
} else { | ||
return {}; | ||
@@ -246,16 +256,23 @@ } | ||
// 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. | ||
// | ||
if (!country) { | ||
country = find_country_code(country_phone_code, national_number); | ||
// Check country restriction | ||
if (options.country.restrict && country !== options.country.restrict) { | ||
return {}; | ||
} | ||
} | ||
// Validate national (significant) number length. | ||
// | ||
// A sidenote: | ||
// | ||
// They say that sometimes national (significant) numbers | ||
// can be longer than `MAX_LENGTH_FOR_NSN` (e.g. in Germany). | ||
// https://github.com/googlei18n/libphonenumber/blob/7e1748645552da39c4e1ba731e47969d97bdb539/resources/phonenumber.proto#L36 | ||
if (national_number.length < MIN_LENGTH_FOR_NSN || national_number.length > MAX_LENGTH_FOR_NSN) { | ||
return { phone: number }; | ||
// Such numbers will just be discarded. | ||
// | ||
if (national_number.length > MAX_LENGTH_FOR_NSN) { | ||
return {}; | ||
} | ||
@@ -398,3 +415,3 @@ | ||
// Country codes do not begin with a '0' | ||
// Fast abortion: country codes do not begin with a '0' | ||
if (number[0] === '0') { | ||
@@ -469,50 +486,46 @@ return {}; | ||
function find_country_code(country_phone_code, national_phone_number) { | ||
if (!country_phone_code) { | ||
return; | ||
} | ||
// Is always defined, because `country_phone_code` is always valid | ||
var possible_country_codes = _metadata2.default.country_phone_code_to_countries[country_phone_code]; | ||
if (!possible_country_codes) { | ||
return; | ||
} | ||
// Iterate possible countries backwards | ||
// because the first one is the default (main) one. | ||
var i = void 0; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
// Look for leading digits for countries | ||
i = possible_country_codes.length - 1; | ||
while (i > 0) { | ||
var country_code = possible_country_codes[i]; | ||
try { | ||
for (var _iterator2 = (0, _getIterator3.default)(possible_country_codes), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var country_code = _step2.value; | ||
var country = _metadata2.default.countries[country_code]; | ||
var country = _metadata2.default.countries[country_code]; | ||
if ((0, _metadata3.get_leading_digits)(country)) { | ||
if (national_phone_number && national_phone_number.search((0, _metadata3.get_leading_digits)(country)) === 0) { | ||
return country_code; | ||
} | ||
} else if (is_national_phone_number(national_phone_number, country)) { | ||
if ((0, _metadata3.get_leading_digits)(country)) { | ||
if (national_phone_number && national_phone_number.search((0, _metadata3.get_leading_digits)(country)) === 0) { | ||
return country_code; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
i--; | ||
} | ||
// Leading digits not matched, | ||
// just phone number validation will do. | ||
// Now start from the default one. | ||
i = 0; | ||
while (i < possible_country_codes.length) { | ||
var _country_code = possible_country_codes[i]; | ||
var _country = _metadata2.default.countries[_country_code]; | ||
if (is_national_phone_number(national_phone_number, _country)) { | ||
return _country_code; | ||
} | ||
i++; | ||
} | ||
// No country matched this national phone number | ||
} | ||
function is_national_phone_number(national_number, country_metadata) { | ||
if (!national_number) { | ||
return false; | ||
} | ||
// `national_number` must be defined | ||
@@ -519,0 +532,0 @@ // Faster false positives |
@@ -0,1 +1,6 @@ | ||
0.1.9 / 30.11.2016 | ||
=================== | ||
* Small fix for "as you type" formatter: replacing digit placeholders (punctuation spaces) with regular spaces in the output | ||
0.1.8 / 29.11.2016 | ||
@@ -2,0 +7,0 @@ =================== |
{ | ||
"name": "libphonenumber-js", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A simpler (and smaller) rewrite of Google Android's famous libphonenumber library", | ||
@@ -5,0 +5,0 @@ "main": "index.common.js", |
@@ -53,2 +53,3 @@ // This is an enhanced port of Google Android `libphonenumber`'s | ||
const DIGIT_PLACEHOLDER_MATCHER = new RegExp(DIGIT_PLACEHOLDER) | ||
const DIGIT_PLACEHOLDER_MATCHER_GLOBAL = new RegExp(DIGIT_PLACEHOLDER, 'g') | ||
@@ -262,16 +263,6 @@ // A pattern that is used to match character classes in regular expressions. | ||
// If no new phone number format could be chosen, | ||
// then can't format the phone. | ||
if (!this.current_format) | ||
{ | ||
this.able_to_format = false | ||
// Return raw phone number | ||
return this.parsed_input | ||
} | ||
// If could format the next (current) digit | ||
// using the previously chosen phone number format | ||
// then return the formatted number so far. | ||
if (national_number_formatted_with_previous_format) | ||
if (this.current_format) | ||
{ | ||
@@ -281,3 +272,4 @@ return this.full_phone_number(national_number_formatted_with_previous_format) | ||
// Couldn't format the supplied national number | ||
// If no new phone number format could be chosen, | ||
// And couldn't format the supplied national number | ||
// using the selected phone number pattern. | ||
@@ -310,3 +302,3 @@ // Return raw phone number | ||
this.country_metadata = undefined | ||
this.available_formats = undefined | ||
this.available_formats = [] | ||
} | ||
@@ -347,7 +339,2 @@ | ||
{ | ||
if (!this.country_metadata) | ||
{ | ||
return | ||
} | ||
// Get all "eligible" phone number formats for this country | ||
@@ -489,8 +476,7 @@ this.available_formats = get_formats(this.country_metadata).filter((format) => | ||
// Some national prefixes are a substring of others. If extracting the shorter | ||
// national prefix doesn't result in a number we can format, | ||
// we try to see if we can extract a longer version here. | ||
extract_longer_national_prefix() | ||
extract_national_prefix() | ||
{ | ||
if (!this.national_prefix) | ||
this.national_prefix = '' | ||
if (!this.country_metadata) | ||
{ | ||
@@ -500,60 +486,26 @@ return | ||
// Put the extracted national prefix back to the national number | ||
// before attempting to extract a new national prefix. | ||
this.national_number = this.national_prefix + this.national_number | ||
const national_prefix_for_parsing = get_national_prefix_for_parsing(this.country_metadata) | ||
const previously_extracted_national_prefix = this.national_prefix | ||
this.extract_national_prefix() | ||
return this.national_prefix !== previously_extracted_national_prefix | ||
} | ||
if (!national_prefix_for_parsing) | ||
{ | ||
return | ||
} | ||
extract_national_prefix() | ||
{ | ||
let national_number_starts_at = 0 | ||
const matches = this.national_number.match(new RegExp('^(?:' + national_prefix_for_parsing + ')')) | ||
if (this.country_metadata) | ||
// Since some national prefix patterns are entirely optional, check that a | ||
// national prefix could actually be extracted. | ||
if (!matches || !matches[0]) | ||
{ | ||
// Small performance optimization for NANPA countries | ||
// which can't have `1` (national prefix) as the | ||
// first digit of a national (significant) number | ||
if (this.is_NANPA_number_with_international_prefix()) | ||
{ | ||
national_number_starts_at = 1 | ||
} | ||
else if (get_national_prefix_for_parsing(this.country_metadata)) | ||
{ | ||
var national_prefix_for_parsing = new RegExp('^(?:' + get_national_prefix_for_parsing(this.country_metadata) + ')') | ||
var matches = this.national_number.match(national_prefix_for_parsing) | ||
// Since some national prefix patterns are entirely optional, check that a | ||
// national prefix could actually be extracted. | ||
if (matches && matches[0]) | ||
{ | ||
national_number_starts_at = matches[0].length | ||
} | ||
} | ||
return | ||
} | ||
const national_number_starts_at = matches[0].length | ||
this.national_prefix = this.national_number.slice(0, national_number_starts_at) | ||
this.national_number = this.national_number.slice(national_number_starts_at) | ||
return this.national_prefix | ||
} | ||
// Returns `true` if the current country is a NANPA country and the | ||
// national number begins with the national prefix. | ||
is_NANPA_number_with_international_prefix() | ||
{ | ||
// For NANPA numbers beginning with 1[2-9], treat the 1 as the national | ||
// prefix. The reason is that national significant numbers in NANPA always | ||
// start with [2-9] after the national prefix. Numbers beginning with 1[01] | ||
// can only be short/emergency numbers, which don't need the national prefix. | ||
if (get_phone_code(this.country_metadata) !== 1) | ||
{ | ||
return false | ||
} | ||
return this.national_number[0] === '1' && | ||
this.national_number[1] !== '0' && | ||
this.national_number[1] !== '1' | ||
} | ||
choose_another_format() | ||
@@ -594,2 +546,4 @@ { | ||
// (20|3)\d{4}. In those cases we quickly return. | ||
// (Though there's no such format in current metadata) | ||
/* istanbul ignore if */ | ||
if (number_pattern.indexOf('|') >= 0) | ||
@@ -660,2 +614,3 @@ { | ||
return close_dangling_braces(this.formatting_template, digit_pattern_start + 1) | ||
.replace(DIGIT_PLACEHOLDER_MATCHER_GLOBAL, ' ') | ||
} | ||
@@ -662,0 +617,0 @@ |
@@ -46,4 +46,9 @@ // This is a port of Google Android `libphonenumber`'s | ||
let country_metadata = metadata.countries[input.country] | ||
let country_metadata | ||
if (input.country) | ||
{ | ||
country_metadata = metadata.countries[input.country] | ||
} | ||
const { country_phone_code, number } = parse_phone_number_and_country_phone_code(input.phone) | ||
@@ -50,0 +55,0 @@ |
@@ -143,3 +143,3 @@ import { parseString } from 'xml2js' | ||
// and national prefix is always `0`, | ||
// then `national_prefix_for_parsing` could be `0([\d]{6})` | ||
// then `national_prefix_for_parsing` could be `0(\d{6})` | ||
// and the corresponding `national_prefix_transform_rule` would be `$1` | ||
@@ -146,0 +146,0 @@ // (which is the default behaviour). |
@@ -209,2 +209,8 @@ // This is a port of Google Android `libphonenumber`'s | ||
// If the phone number is not viable, then abort. | ||
if (!formatted_phone_number) | ||
{ | ||
return {} | ||
} | ||
let { country_phone_code, number } = parse_phone_number_and_country_phone_code(formatted_phone_number) | ||
@@ -231,2 +237,9 @@ | ||
country_metadata = get_metadata_by_country_phone_code(country_phone_code, metadata) | ||
// `country` will be set later, | ||
// because, for example, for NANPA countries | ||
// there are several countries corresponding | ||
// to the same `1` country phone code. | ||
// Therefore, to reliably determine the exact country, | ||
// national (significant) number should be parsed first. | ||
} | ||
@@ -237,6 +250,11 @@ else if (options.country.default || options.country.restrict) | ||
country_metadata = metadata.countries[country] | ||
if (!country_metadata) | ||
{ | ||
return {} | ||
} | ||
number = normalize(text) | ||
} | ||
if (!country_metadata) | ||
else | ||
{ | ||
@@ -246,28 +264,27 @@ return {} | ||
// Sanity check | ||
if (number.length < MIN_LENGTH_FOR_NSN) | ||
{ | ||
return {} | ||
} | ||
const national_number = strip_national_prefix(number, country_metadata) | ||
// 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. | ||
// | ||
if (!country) | ||
{ | ||
country = find_country_code(country_phone_code, national_number) | ||
// Check country restriction | ||
if (options.country.restrict && country !== options.country.restrict) | ||
{ | ||
return {} | ||
} | ||
} | ||
// Validate national (significant) number length. | ||
// | ||
// A sidenote: | ||
// | ||
// They say that sometimes national (significant) numbers | ||
// can be longer than `MAX_LENGTH_FOR_NSN` (e.g. in Germany). | ||
// https://github.com/googlei18n/libphonenumber/blob/7e1748645552da39c4e1ba731e47969d97bdb539/resources/phonenumber.proto#L36 | ||
if (national_number.length < MIN_LENGTH_FOR_NSN | ||
|| national_number.length > MAX_LENGTH_FOR_NSN) | ||
// Such numbers will just be discarded. | ||
// | ||
if (national_number.length > MAX_LENGTH_FOR_NSN) | ||
{ | ||
return { phone: number } | ||
return {} | ||
} | ||
@@ -400,3 +417,3 @@ | ||
// Country codes do not begin with a '0' | ||
// Fast abortion: country codes do not begin with a '0' | ||
if (number[0] === '0') | ||
@@ -483,16 +500,15 @@ { | ||
{ | ||
if (!country_phone_code) | ||
{ | ||
return | ||
} | ||
// Is always defined, because `country_phone_code` is always valid | ||
const possible_country_codes = metadata.country_phone_code_to_countries[country_phone_code] | ||
if (!possible_country_codes) | ||
// Iterate possible countries backwards | ||
// because the first one is the default (main) one. | ||
let i | ||
// Look for leading digits for countries | ||
i = possible_country_codes.length - 1 | ||
while (i > 0) | ||
{ | ||
return | ||
} | ||
const country_code = possible_country_codes[i] | ||
for (let country_code of possible_country_codes) | ||
{ | ||
const country = metadata.countries[country_code] | ||
@@ -508,7 +524,25 @@ | ||
} | ||
else if (is_national_phone_number(national_phone_number, country)) | ||
i-- | ||
} | ||
// Leading digits not matched, | ||
// just phone number validation will do. | ||
// Now start from the default one. | ||
i = 0 | ||
while (i < possible_country_codes.length) | ||
{ | ||
const country_code = possible_country_codes[i] | ||
const country = metadata.countries[country_code] | ||
if (is_national_phone_number(national_phone_number, country)) | ||
{ | ||
return country_code | ||
} | ||
i++ | ||
} | ||
// No country matched this national phone number | ||
} | ||
@@ -518,6 +552,3 @@ | ||
{ | ||
if (!national_number) | ||
{ | ||
return false | ||
} | ||
// `national_number` must be defined | ||
@@ -524,0 +555,0 @@ // Faster false positives |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1180356
3262