libphonenumber-js
Advanced tools
Comparing version 0.2.18 to 0.2.19
@@ -464,5 +464,9 @@ 'use strict'; | ||
this.national_number = this.national_prefix + this.national_number; | ||
this.national_prefix = undefined; | ||
this.national_prefix = ''; | ||
} | ||
if (!this.validate_format(format)) { | ||
continue; | ||
} | ||
// To leave the formatter in a consistent state | ||
@@ -472,5 +476,5 @@ this.reset_format(); | ||
var number_pattern = this.validate_format(format); | ||
var number_pattern = this.validate_format_for_template(format); | ||
var formatted_number = (0, _format.format_national_number_using_format)(this.national_number, format, this.is_international(), this.national_prefix, this.country_metadata); | ||
var formatted_number = (0, _format.format_national_number_using_format)(this.national_number, format, this.is_international(), this.national_prefix.length > 0, this.country_metadata); | ||
@@ -598,4 +602,8 @@ if (number_pattern) { | ||
var number_pattern = this.validate_format(format); | ||
if (!this.validate_format(format)) { | ||
continue; | ||
} | ||
var number_pattern = this.validate_format_for_template(format); | ||
if (number_pattern) { | ||
@@ -639,2 +647,14 @@ this.create_formatting_template(format, number_pattern); | ||
value: function validate_format(format) { | ||
// If national prefix is mandatory for this phone number format | ||
// and the user didn't input the national prefix, | ||
// then this phone number format isn't suitable. | ||
if (!this.is_international() && !this.national_prefix && (0, _metadata3.get_format_national_prefix_is_mandatory_when_formatting)(format, this.country_metadata)) { | ||
return; | ||
} | ||
return true; | ||
} | ||
}, { | ||
key: 'validate_format_for_template', | ||
value: function validate_format_for_template(format) { | ||
// The formatter doesn't format numbers when numberPattern contains '|', e.g. | ||
@@ -650,13 +670,2 @@ // (20|3)\d{4}. In those cases we quickly return. | ||
// If national prefix formatting rule is set | ||
// for this phone number format | ||
if (national_prefix_formatting_rule) { | ||
// If national prefix is mandatory for this rule | ||
// and the user didn't input the national prefix | ||
// then this template isn't suitable. | ||
if (!(0, _metadata3.get_format_national_prefix_is_optional_when_formatting)(format, this.country_metadata) && !this.national_prefix) { | ||
return; | ||
} | ||
} | ||
// A very smart trick by the guys at Google | ||
@@ -717,3 +726,4 @@ var number_pattern = (0, _metadata3.get_format_pattern)(format) | ||
this.template = template; | ||
// This one is for national number only | ||
this.partially_populated_template = template; | ||
@@ -724,3 +734,3 @@ // For convenience, the public `.template` property | ||
if (this.is_international()) { | ||
this.template = DIGIT_PLACEHOLDER + repeat(DIGIT_PLACEHOLDER, this.country_phone_code.length) + ' ' + this.template; | ||
template = DIGIT_PLACEHOLDER + repeat(DIGIT_PLACEHOLDER, this.country_phone_code.length) + ' ' + template; | ||
} | ||
@@ -730,7 +740,7 @@ // For local numbers, replace national prefix | ||
else { | ||
this.template = this.template.replace(/\d/g, DIGIT_PLACEHOLDER); | ||
template = template.replace(/\d/g, DIGIT_PLACEHOLDER); | ||
} | ||
// This one is for national number only | ||
return this.partially_populated_template = template; | ||
// This one is for the full phone number | ||
this.template = template; | ||
} | ||
@@ -737,0 +747,0 @@ }, { |
@@ -103,6 +103,11 @@ 'use strict'; | ||
var national_prefix_may_be_omitted = !enforce_national_prefix && (0, _metadata3.get_format_national_prefix_is_optional_when_formatting)(format, country_metadata); | ||
var national_prefix_formatting_rule = (0, _metadata3.get_format_national_prefix_formatting_rule)(format, country_metadata); | ||
// National prefix is omitted if there's no national prefix formatting rule | ||
// set for this country, or when this rule is set but | ||
// national prefix is optional for this phone number format | ||
// (and it is not enforced explicitly) | ||
var national_prefix_may_be_omitted = !national_prefix_formatting_rule || national_prefix_formatting_rule && (0, _metadata3.get_format_national_prefix_is_optional_when_formatting)(format, country_metadata) && !enforce_national_prefix; | ||
if (!international && !national_prefix_may_be_omitted) { | ||
var national_prefix_formatting_rule = (0, _metadata3.get_format_national_prefix_formatting_rule)(format, country_metadata); | ||
@@ -109,0 +114,0 @@ // If national prefix formatting rule is set |
@@ -20,2 +20,3 @@ "use strict"; | ||
exports.get_format_national_prefix_is_optional_when_formatting = get_format_national_prefix_is_optional_when_formatting; | ||
exports.get_format_national_prefix_is_mandatory_when_formatting = get_format_national_prefix_is_mandatory_when_formatting; | ||
exports.get_format_international_format = get_format_international_format; | ||
@@ -98,2 +99,10 @@ exports.get_metadata_by_country_phone_code = get_metadata_by_country_phone_code; | ||
function get_format_national_prefix_is_mandatory_when_formatting(format_array, country_metadata) { | ||
// National prefix is omitted if there's no national prefix formatting rule | ||
// set for this country, or when this rule is set but | ||
// national prefix is optional for this phone number format | ||
// (and it is not enforced explicitly) | ||
return get_format_national_prefix_formatting_rule(format_array, country_metadata) && !get_format_national_prefix_is_optional_when_formatting(format_array, country_metadata); | ||
} | ||
function get_format_international_format(format_array) { | ||
@@ -100,0 +109,0 @@ return format_array[5] || get_format_format(format_array); |
@@ -0,1 +1,6 @@ | ||
0.2.19 / 25.12.2016 | ||
=================== | ||
* Small fix for "as you type" to not prepend national prefix to the number being typed | ||
0.2.13 / 23.12.2016 | ||
@@ -2,0 +7,0 @@ =================== |
{ | ||
"name": "libphonenumber-js", | ||
"version": "0.2.18", | ||
"version": "0.2.19", | ||
"description": "A simpler (and smaller) rewrite of Google Android's popular libphonenumber library", | ||
@@ -5,0 +5,0 @@ "main": "index.common.js", |
@@ -19,2 +19,3 @@ // This is an enhanced port of Google Android `libphonenumber`'s | ||
get_format_national_prefix_is_optional_when_formatting, | ||
get_format_national_prefix_is_mandatory_when_formatting, | ||
get_format_leading_digits_patterns, | ||
@@ -513,5 +514,10 @@ get_metadata_by_country_phone_code | ||
this.national_number = this.national_prefix + this.national_number | ||
this.national_prefix = undefined | ||
this.national_prefix = '' | ||
} | ||
if (!this.validate_format(format)) | ||
{ | ||
continue | ||
} | ||
// To leave the formatter in a consistent state | ||
@@ -521,3 +527,3 @@ this.reset_format() | ||
const number_pattern = this.validate_format(format) | ||
const number_pattern = this.validate_format_for_template(format) | ||
@@ -529,3 +535,3 @@ const formatted_number = format_national_number_using_format | ||
this.is_international(), | ||
this.national_prefix, | ||
this.national_prefix.length > 0, | ||
this.country_metadata | ||
@@ -637,4 +643,9 @@ ) | ||
const number_pattern = this.validate_format(format) | ||
if (!this.validate_format(format)) | ||
{ | ||
continue | ||
} | ||
const number_pattern = this.validate_format_for_template(format) | ||
if (number_pattern) | ||
@@ -664,2 +675,15 @@ { | ||
{ | ||
// If national prefix is mandatory for this phone number format | ||
// and the user didn't input the national prefix, | ||
// then this phone number format isn't suitable. | ||
if (!this.is_international() && !this.national_prefix && get_format_national_prefix_is_mandatory_when_formatting(format, this.country_metadata)) | ||
{ | ||
return | ||
} | ||
return true | ||
} | ||
validate_format_for_template(format) | ||
{ | ||
// The formatter doesn't format numbers when numberPattern contains '|', e.g. | ||
@@ -676,16 +700,2 @@ // (20|3)\d{4}. In those cases we quickly return. | ||
// If national prefix formatting rule is set | ||
// for this phone number format | ||
if (national_prefix_formatting_rule) | ||
{ | ||
// If national prefix is mandatory for this rule | ||
// and the user didn't input the national prefix | ||
// then this template isn't suitable. | ||
if (!get_format_national_prefix_is_optional_when_formatting(format, this.country_metadata) | ||
&& !this.national_prefix) | ||
{ | ||
return | ||
} | ||
} | ||
// A very smart trick by the guys at Google | ||
@@ -743,3 +753,3 @@ const number_pattern = get_format_pattern(format) | ||
// Create formatting template for this phone number format | ||
const template = dummy_phone_number_matching_format_pattern | ||
let template = dummy_phone_number_matching_format_pattern | ||
// Format the dummy phone number according to the format | ||
@@ -750,3 +760,4 @@ .replace(new RegExp(number_pattern, 'g'), number_format) | ||
this.template = template | ||
// This one is for national number only | ||
this.partially_populated_template = template | ||
@@ -758,3 +769,3 @@ // For convenience, the public `.template` property | ||
{ | ||
this.template = DIGIT_PLACEHOLDER + repeat(DIGIT_PLACEHOLDER, this.country_phone_code.length) + ' ' + this.template | ||
template = DIGIT_PLACEHOLDER + repeat(DIGIT_PLACEHOLDER, this.country_phone_code.length) + ' ' + template | ||
} | ||
@@ -765,7 +776,7 @@ // For local numbers, replace national prefix | ||
{ | ||
this.template = this.template.replace(/\d/g, DIGIT_PLACEHOLDER) | ||
template = template.replace(/\d/g, DIGIT_PLACEHOLDER) | ||
} | ||
// This one is for national number only | ||
return this.partially_populated_template = template | ||
// This one is for the full phone number | ||
this.template = template | ||
} | ||
@@ -772,0 +783,0 @@ |
@@ -103,7 +103,13 @@ // This is a port of Google Android `libphonenumber`'s | ||
const national_prefix_may_be_omitted = !enforce_national_prefix && get_format_national_prefix_is_optional_when_formatting(format, country_metadata) | ||
const national_prefix_formatting_rule = get_format_national_prefix_formatting_rule(format, country_metadata) | ||
// National prefix is omitted if there's no national prefix formatting rule | ||
// set for this country, or when this rule is set but | ||
// national prefix is optional for this phone number format | ||
// (and it is not enforced explicitly) | ||
const national_prefix_may_be_omitted = !national_prefix_formatting_rule || | ||
(national_prefix_formatting_rule && get_format_national_prefix_is_optional_when_formatting(format, country_metadata) && !enforce_national_prefix) | ||
if (!international && !national_prefix_may_be_omitted) | ||
{ | ||
const national_prefix_formatting_rule = get_format_national_prefix_formatting_rule(format, country_metadata) | ||
@@ -110,0 +116,0 @@ // If national prefix formatting rule is set |
@@ -80,2 +80,12 @@ export function get_phone_code(country_metadata) | ||
export function get_format_national_prefix_is_mandatory_when_formatting(format_array, country_metadata) | ||
{ | ||
// National prefix is omitted if there's no national prefix formatting rule | ||
// set for this country, or when this rule is set but | ||
// national prefix is optional for this phone number format | ||
// (and it is not enforced explicitly) | ||
return get_format_national_prefix_formatting_rule(format_array, country_metadata) && | ||
!get_format_national_prefix_is_optional_when_formatting(format_array, country_metadata) | ||
} | ||
export function get_format_international_format(format_array) | ||
@@ -82,0 +92,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 too big to display
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
1107939
4658