Browse Source

JS: libphonenumber 4.2.

pull/567/head
Nikolaos Trogkanis 14 years ago
committed by Mihaela Rosca
parent
commit
817cdf4fd4
10 changed files with 1667 additions and 1525 deletions
  1. +1
    -1
      javascript/README
  2. +714
    -699
      javascript/i18n/phonenumbers/metadata.js
  3. +55
    -55
      javascript/i18n/phonenumbers/metadatafortesting.js
  4. +714
    -699
      javascript/i18n/phonenumbers/metadatalite.js
  5. +56
    -0
      javascript/i18n/phonenumbers/phonemetadata.pb.js
  6. +91
    -63
      javascript/i18n/phonenumbers/phonenumberutil.js
  7. +22
    -8
      javascript/i18n/phonenumbers/phonenumberutil_test.js
  8. +1
    -0
      javascript/i18n/phonenumbers/regioncodefortesting.js
  9. +6
    -0
      tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java
  10. +7
    -0
      tools/java/java-build/src/com/google/i18n/phonenumbers/JSArrayBuilder.java

+ 1
- 1
javascript/README View File

@ -25,7 +25,7 @@ pages with your web browser:
How to update:
==============
The JavaScript library is ported from the Java implementation (revision 374).
The JavaScript library is ported from the Java implementation (revision 388).
When the Java project gets updated follow these steps to update the JavaScript
project:


+ 714
- 699
javascript/i18n/phonenumbers/metadata.js
File diff suppressed because it is too large
View File


+ 55
- 55
javascript/i18n/phonenumbers/metadatafortesting.js View File

@ -75,7 +75,7 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,[,,"NA","NA"]
,"AO",244,"00","0~0",,,"0~0",,,,[[,"(\\d{3})(\\d{3})(\\d{3})","$1 $2 $3",,"",""]
,"AO",244,"00","0~0",,,"0~0",,,,[[,"(\\d{3})(\\d{3})(\\d{3})","$1 $2 $3",,"","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -91,15 +91,15 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"AR",54,"00","0",,,"0(?:(11|343|3715)15)?","9$1",,,[[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2-$3",["11"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{4})(\\d{2})(\\d{4})","$1 $2-$3",["1[02-9]|[23]"]
,"0$1",""]
,"0$1","",0]
,[,"(9)(11)(\\d{4})(\\d{4})","$2 15 $3-$4",["911"]
,"0$1",""]
,"0$1","",0]
,[,"(9)(\\d{4})(\\d{2})(\\d{4})","$2 $3-$4",["9(?:1[02-9]|[23])"]
,"0$1","0$1 $CC"]
,"0$1","0$1 $CC",0]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1-$2-$3",["[68]"]
,"0$1",""]
,"0$1","",0]
]
,[[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2-$3",["11"]
]
@ -126,9 +126,9 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"AU",61,"001[12]","0",,,"0",,"0011",,[[,"(\\d{4})(\\d{3})(\\d{3})","$1 $2 $3",["1"]
,"$1",""]
,"$1","",0]
,[,"(\\d{1})(\\d{4})(\\d{4})","$1 $2 $3",["[2-478]"]
,"0$1",""]
,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -170,17 +170,17 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"DE",49,"00","0",,,"0",,,,[[,"(\\d{3})(\\d{3,8})","$1 $2",["2|3[3-9]|906|[4-9][1-9]1"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{4,9})","$1/$2",["[34]0|[68]9"]
,"0$1",""]
,"0$1","",0]
,[,"([4-9]\\d)(\\d{2})","$1 $2",["[4-9]","[4-6]|[7-9](?:\\d[1-9]|[1-9]\\d)"]
,"0$1",""]
,"0$1","",0]
,[,"([4-9]\\d{3})(\\d{2,7})","$1 $2",["[4-9]","[4-6]|[7-9](?:\\d[1-9]|[1-9]\\d)"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{1})(\\d{6})","$1 $2 $3",["800"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{3,4})(\\d{4})","$1 $2 $3",["900"]
,"0$1",""]
,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -196,13 +196,13 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"70\\d{8}","\\d{10}"]
,[,,"56\\d{8}","\\d{10}"]
,"GB",44,"00","0",,,"0",,,,[[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2 $3",["[1-59]|[78]0"]
,"(0$1)",""]
,"(0$1)","",0]
,[,"(\\d)(\\d{3})(\\d{3})(\\d{3})","$1 $2 $3 $4",["6"]
,"(0$1)",""]
,"(0$1)","",0]
,[,"(\\d{4})(\\d{3})(\\d{3})","$1 $2 $3",["7[1-57-9]"]
,"(0$1)",""]
,"(0$1)","",0]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",["8[47]"]
,"(0$1)",""]
,"(0$1)","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -218,13 +218,13 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"IT",39,"00",,,,,,,,[[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2 $3",["0[26]"]
,"",""]
,"","",0]
,[,"(\\d{3})(\\d{4})(\\d{3,4})","$1 $2 $3",["0[13-57-9]"]
,"",""]
,"","",0]
,[,"(\\d{3})(\\d{3})(\\d{3,4})","$1 $2 $3",["3"]
,"",""]
,"","",0]
,[,"(\\d{3})(\\d{3,6})","$1 $2",["8"]
,"",""]
,"","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -240,17 +240,17 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"JP",81,"010","0",,,"0",,,1,[[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2 $3",["[57-9]0"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{2})(\\d{3})(\\d{4})","$1 $2 $3 $4",["[57-9]0"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{3})(\\d{4})","$1 $2 $3",["222|333","(?:222|333)1","(?:222|333)11"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{4})(\\d)(\\d{4})","$1 $2 $3",["222|333","2221|3332","22212|3332","222120|3332"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{2})(\\d{4})","$1 $2 $3",["[23]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{4})","*$1",["[23]"]
,"0$1",""]
,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -266,29 +266,29 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"50\\d{8}","\\d{10}",,,"5012345678"]
,[,,"70\\d{8}","\\d{10}",,,"7012345678"]
,"KR",82,"00(?:[124-68]|[37]\\d{2})","0",,,"0(8[1-46-8]|85\\d{2})?",,,,[[,"(\\d{2})(\\d{4})(\\d{4})","$1-$2-$3",["1(?:0|1[19]|[69]9|5[458])|[57]0","1(?:0|1[19]|[69]9|5(?:44|59|8))|[57]0"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{3})(\\d{4})","$1-$2-$3",["1(?:[169][2-8]|[78]|5[1-4])|[68]0|[3-6][1-9][2-9]","1(?:[169][2-8]|[78]|5(?:[1-3]|4[56]))|[68]0|[3-6][1-9][2-9]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d)(\\d{4})","$1-$2-$3",["131","1312"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{2})(\\d{4})","$1-$2-$3",["131","131[13-9]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1-$2-$3",["13[2-9]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{2})(\\d{3})(\\d{4})","$1-$2-$3-$4",["30"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d)(\\d{4})(\\d{4})","$1-$2-$3",["2(?:[26]|3[0-467])","2(?:[26]|3(?:01|1[45]|2[17-9]|39|4|6[67]|7[078]))"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d)(\\d{3})(\\d{4})","$1-$2-$3",["2(?:3[0-35-9]|[457-9])","2(?:3(?:0[02-9]|1[0-36-9]|2[02-6]|3[0-8]|6[0-589]|7[1-69]|[589])|[457-9])"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d)(\\d{3})","$1-$2",["21[0-46-9]","21(?:[0-247-9]|3[124]|6[1269])"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d)(\\d{4})","$1-$2",["21[36]","21(?:3[035-9]|6[03-578])"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{3})","$1-$2",["[3-6][1-9]1","[3-6][1-9]1(?:[0-46-9])","[3-6][1-9]1(?:[0-247-9]|3[124]|6[1269])"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{2})(\\d{4})","$1-$2",["[3-6][1-9]1","[3-6][1-9]1[36]","[3-6][1-9]1(?:3[035-9]|6[03-578])"]
,"0$1",""]
,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -304,15 +304,15 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"MX",52,"00","01",,,"01|04[45](\\d{10})","1$1",,,[[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",["[89]00"]
,"01 $1",""]
,"01 $1","",1]
,[,"(\\d{2})(\\d{4})(\\d{4})","$1 $2 $3",["33|55|81"]
,"01 $1",""]
,"01 $1","",1]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",["[2467]|3[0-24-9]|5[0-46-9]|8[2-9]|9[1-9]"]
,"01 $1",""]
,"01 $1","",1]
,[,"(1)(\\d{2})(\\d{4})(\\d{4})","045 $2 $3 $4",["1(?:33|55|81)"]
,"$1",""]
,"$1","",0]
,[,"(1)(\\d{3})(\\d{3})(\\d{4})","045 $2 $3 $4",["1(?:[124579]|3[0-24-9]|5[0-46-9]|8[02-9])"]
,"$1",""]
,"$1","",0]
]
,[[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",["[89]00"]
]
@ -339,11 +339,11 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"NZ",64,"00","0",,,"0",,,,[[,"(\\d)(\\d{3})(\\d{4})","$1-$2 $3",["24|[34679]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d)(\\d{3})(\\d{3,5})","$1-$2 $3",["2[179]"]
,"0$1",""]
,"0$1","",0]
,[,"(\\d{3})(\\d{3})(\\d{3,4})","$1 $2 $3",["[89]"]
,"0$1",""]
,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -358,7 +358,7 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,[,,"NA","NA"]
,"PL",48,"00","0",,,"0",,,,[[,"(\\d{2})(\\d{3})(\\d{2})(\\d{2})","$1 $2 $3 $4",,"0$1",""]
,"PL",48,"00","0",,,"0",,,,[[,"(\\d{2})(\\d{3})(\\d{2})(\\d{2})","$1 $2 $3 $4",,"0$1","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -373,7 +373,7 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,[,,"NA","NA"]
,"RE",262,"00","0",,,"0",,,,[[,"([268]\\d{2})(\\d{2})(\\d{2})(\\d{2})","$1 $2 $3 $4",,"0$1",""]
,"RE",262,"00","0",,,"0",,,,[[,"([268]\\d{2})(\\d{2})(\\d{2})(\\d{2})","$1 $2 $3 $4",,"0$1","",0]
]
,,[,,"NA","NA"]
,,"262|6(?:9[23]|47)|8",[,,"NA","NA"]
@ -389,11 +389,11 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,"SG",65,"0[0-3][0-9]",,,,"777777",,,,[[,"(\\d{4})(\\d{4})","$1 $2",["[369]|8[1-9]"]
,"",""]
,"","",0]
,[,"(\\d{4})(\\d{3})(\\d{4})","$1 $2 $3",["1[89]"]
,"",""]
,"","",0]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",["800"]
,"",""]
,"","",0]
]
,,[,,"NA","NA"]
,,,[,,"NA","NA"]
@ -408,8 +408,8 @@ i18n.phonenumbers.metadata.countryToMetadata = {
,[,,"NA","NA"]
,[,,"NA","NA"]
,[,,"NA","NA"]
,"US",1,"011","1"," extn. ",,"1",,,1,[[,"(\\d{3})(\\d{4})","$1 $2",,"",""]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",,"",""]
,"US",1,"011","1"," extn. ",,"1",,,1,[[,"(\\d{3})(\\d{4})","$1 $2",,"","",0]
,[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3",,"","",0]
]
,[[,"(\\d{3})(\\d{3})(\\d{4})","$1 $2 $3"]
]


+ 714
- 699
javascript/i18n/phonenumbers/metadatalite.js
File diff suppressed because it is too large
View File


+ 56
- 0
javascript/i18n/phonenumbers/phonemetadata.pb.js View File

@ -265,6 +265,57 @@ i18n.phonenumbers.NumberFormat.prototype.clearNationalPrefixFormattingRule = fun
};
/**
* Gets the value of the national_prefix_optional_when_formatting field.
* @return {?boolean} The value.
*/
i18n.phonenumbers.NumberFormat.prototype.getNationalPrefixOptionalWhenFormatting = function() {
return /** @type {?boolean} */ (this.get$Value(6));
};
/**
* Gets the value of the national_prefix_optional_when_formatting field or the default value if not set.
* @return {boolean} The value.
*/
i18n.phonenumbers.NumberFormat.prototype.getNationalPrefixOptionalWhenFormattingOrDefault = function() {
return /** @type {boolean} */ (this.get$ValueOrDefault(6));
};
/**
* Sets the value of the national_prefix_optional_when_formatting field.
* @param {boolean} value The value.
*/
i18n.phonenumbers.NumberFormat.prototype.setNationalPrefixOptionalWhenFormatting = function(value) {
this.set$Value(6, value);
};
/**
* @return {boolean} Whether the national_prefix_optional_when_formatting field has a value.
*/
i18n.phonenumbers.NumberFormat.prototype.hasNationalPrefixOptionalWhenFormatting = function() {
return this.has$Value(6);
};
/**
* @return {number} The number of values in the national_prefix_optional_when_formatting field.
*/
i18n.phonenumbers.NumberFormat.prototype.nationalPrefixOptionalWhenFormattingCount = function() {
return this.count$Values(6);
};
/**
* Clears the values in the national_prefix_optional_when_formatting field.
*/
i18n.phonenumbers.NumberFormat.prototype.clearNationalPrefixOptionalWhenFormatting = function() {
this.clear$Field(6);
};
/**
* Gets the value of the domestic_carrier_code_formatting_rule field.
* @return {?string} The value.
@ -1967,6 +2018,11 @@ goog.proto2.Message.set$Metadata(i18n.phonenumbers.NumberFormat, {
fieldType: goog.proto2.Message.FieldType.STRING,
type: String
},
6: {
name: 'national_prefix_optional_when_formatting',
fieldType: goog.proto2.Message.FieldType.BOOL,
type: Boolean
},
5: {
name: 'domestic_carrier_code_formatting_rule',
fieldType: goog.proto2.Message.FieldType.STRING,


+ 91
- 63
javascript/i18n/phonenumbers/phonenumberutil.js View File

@ -1105,7 +1105,8 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.getLengthOfNationalDestinationCode =
return 0;
}
if (this.getRegionCodeForNumber(number) == 'AR' &&
if (this.getRegionCodeForCountryCode(number.getCountryCodeOrDefault()) ==
'AR' &&
this.getNumberType(number) == i18n.phonenumbers.PhoneNumberType.MOBILE) {
// Argentinian mobile numbers, when formatted in the international format,
// are in the form of +54 9 NDC XXXX.... As a result, we take the length of
@ -1814,21 +1815,15 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.formatNationalNumber_ =
/**
* Note that carrierCode is optional - if NULL or an empty string, no carrier
* code replacement will take place.
*
* @param {string} nationalNumber a string of characters representing a phone
* number.
* @param {Array.<i18n.phonenumbers.NumberFormat>} availableFormats the
* available formats the phone number could be formatted into.
* @param {i18n.phonenumbers.PhoneNumberFormat} numberFormat the format the
* phone number should be formatted into.
* @param {string=} opt_carrierCode
* @return {string} the formatted phone number.
* @param {string} nationalNumber a string of characters representing a phone
* number.
* @return {i18n.phonenumbers.NumberFormat}
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.formatAccordingToFormats_ =
function(nationalNumber, availableFormats, numberFormat, opt_carrierCode) {
i18n.phonenumbers.PhoneNumberUtil.prototype.chooseFormattingPatternForNumber_ =
function(availableFormats, nationalNumber) {
/** @type {i18n.phonenumbers.NumberFormat} */
var numFormat;
@ -1847,47 +1842,74 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.formatAccordingToFormats_ =
var patternToMatch = new RegExp(numFormat.getPattern());
if (i18n.phonenumbers.PhoneNumberUtil.matchesEntirely_(patternToMatch,
nationalNumber)) {
/** @type {string} */
var numberFormatRule = numFormat.getFormatOrDefault();
/** @type {string} */
var domesticCarrierCodeFormattingRule =
numFormat.getDomesticCarrierCodeFormattingRuleOrDefault();
if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL &&
opt_carrierCode != null && opt_carrierCode.length > 0 &&
domesticCarrierCodeFormattingRule.length > 0) {
// Replace the $CC in the formatting rule with the desired carrier
// code.
/** @type {string} */
var carrierCodeFormattingRule = domesticCarrierCodeFormattingRule
.replace(i18n.phonenumbers.PhoneNumberUtil.CC_PATTERN_,
opt_carrierCode);
// Now replace the $FG in the formatting rule with the first group and
// the carrier code combined in the appropriate way.
numberFormatRule = numberFormatRule.replace(
i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_PATTERN_,
carrierCodeFormattingRule);
return nationalNumber.replace(patternToMatch, numberFormatRule);
} else {
// Use the national prefix formatting rule instead.
/** @type {string} */
var nationalPrefixFormattingRule =
numFormat.getNationalPrefixFormattingRuleOrDefault();
if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL &&
nationalPrefixFormattingRule != null &&
nationalPrefixFormattingRule.length > 0) {
return nationalNumber.replace(patternToMatch, numberFormatRule
.replace(i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_PATTERN_,
nationalPrefixFormattingRule));
} else {
return nationalNumber.replace(patternToMatch, numberFormatRule);
}
}
return numFormat;
}
}
}
return null;
};
// If no pattern above is matched, we format the number as a whole.
return nationalNumber;
/**
* Note that carrierCode is optional - if NULL or an empty string, no carrier
* code replacement will take place.
*
* @param {string} nationalNumber a string of characters representing a phone
* number.
* @param {Array.<i18n.phonenumbers.NumberFormat>} availableFormats the
* available formats the phone number could be formatted into.
* @param {i18n.phonenumbers.PhoneNumberFormat} numberFormat the format the
* phone number should be formatted into.
* @param {string=} opt_carrierCode
* @return {string} the formatted phone number.
* @private
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.formatAccordingToFormats_ =
function(nationalNumber, availableFormats, numberFormat, opt_carrierCode) {
/** @type {i18n.phonenumbers.NumberFormat} */
var numFormat = this.chooseFormattingPatternForNumber_(availableFormats,
nationalNumber);
if (numFormat == null) {
// If no pattern above is matched, we format the number as a whole.
return nationalNumber;
}
/** @type {string} */
var numberFormatRule = numFormat.getFormatOrDefault();
/** @type {!RegExp} */
var patternToMatch = new RegExp(numFormat.getPattern());
/** @type {string} */
var domesticCarrierCodeFormattingRule =
numFormat.getDomesticCarrierCodeFormattingRuleOrDefault();
if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL &&
opt_carrierCode != null && opt_carrierCode.length > 0 &&
domesticCarrierCodeFormattingRule.length > 0) {
// Replace the $CC in the formatting rule with the desired carrier code.
/** @type {string} */
var carrierCodeFormattingRule = domesticCarrierCodeFormattingRule
.replace(i18n.phonenumbers.PhoneNumberUtil.CC_PATTERN_,
opt_carrierCode);
// Now replace the $FG in the formatting rule with the first group and
// the carrier code combined in the appropriate way.
numberFormatRule = numberFormatRule.replace(
i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_PATTERN_,
carrierCodeFormattingRule);
return nationalNumber.replace(patternToMatch, numberFormatRule);
} else {
// Use the national prefix formatting rule instead.
/** @type {string} */
var nationalPrefixFormattingRule =
numFormat.getNationalPrefixFormattingRuleOrDefault();
if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL &&
nationalPrefixFormattingRule != null &&
nationalPrefixFormattingRule.length > 0) {
return nationalNumber.replace(patternToMatch, numberFormatRule
.replace(i18n.phonenumbers.PhoneNumberUtil.FIRST_GROUP_PATTERN_,
nationalPrefixFormattingRule));
} else {
return nationalNumber.replace(patternToMatch, numberFormatRule);
}
}
};
@ -2737,8 +2759,9 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.maybeExtractCountryCode =
/** @type {!RegExp} */
var validNumberPattern =
new RegExp(generalDesc.getNationalNumberPatternOrDefault());
// Passing null since we don't need the carrier code.
this.maybeStripNationalPrefixAndCarrierCode(
potentialNationalNumber, defaultRegionMetadata);
potentialNationalNumber, defaultRegionMetadata, null);
/** @type {string} */
var potentialNationalNumberStr = potentialNationalNumber.toString();
/** @type {string} */
@ -2862,14 +2885,15 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.
* that we wish to strip any national dialing prefix from.
* @param {i18n.phonenumbers.PhoneMetadata} metadata the metadata for the
* region that we think this number is from.
* @return {string} the carrier code extracted if it is present, otherwise
* return an empty string.
* @param {goog.string.StringBuffer} carrierCode a place to insert the carrier
* code if one is extracted.
* @return {boolean} true if a national prefix or carrier code (or both) could
* be extracted.
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.
maybeStripNationalPrefixAndCarrierCode = function(number, metadata) {
maybeStripNationalPrefixAndCarrierCode = function(number, metadata,
carrierCode) {
/** @type {string} */
var carrierCode = '';
/** @type {string} */
var numberStr = number.toString();
/** @type {number} */
@ -2879,7 +2903,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.
if (numberLength == 0 || possibleNationalPrefix == null ||
possibleNationalPrefix.length == 0) {
// Early return for numbers of zero length.
return '';
return false;
}
// Attempt to parse the first digits as a national prefix.
/** @type {!RegExp} */
@ -2914,16 +2938,19 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.
nationalNumberRule, numberStr) &&
!i18n.phonenumbers.PhoneNumberUtil.matchesEntirely_(
nationalNumberRule, transformedNumber)) {
return '';
return false;
}
if ((noTransform && numOfGroups > 0 && prefixMatcher[1] != null) ||
(!noTransform && numOfGroups > 1)) {
carrierCode = prefixMatcher[1];
if (carrierCode != null) {
carrierCode.append(prefixMatcher[1]);
}
}
number.clear();
number.append(transformedNumber);
return true;
}
return carrierCode;
return false;
};
@ -3158,11 +3185,12 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.parseHelper_ =
}
if (regionMetadata != null) {
/** @type {string} */
var carrierCode = this.maybeStripNationalPrefixAndCarrierCode(
normalizedNationalNumber, regionMetadata);
/** @type {goog.string.StringBuffer} */
var carrierCode = new goog.string.StringBuffer();
this.maybeStripNationalPrefixAndCarrierCode(
normalizedNationalNumber, regionMetadata, carrierCode);
if (keepRawInput) {
phoneNumber.setPreferredDomesticCarrierCode(carrierCode);
phoneNumber.setPreferredDomesticCarrierCode(carrierCode.toString());
}
}
/** @type {string} */


+ 22
- 8
javascript/i18n/phonenumbers/phonenumberutil_test.js View File

@ -665,6 +665,13 @@ function testFormatOutOfCountryCallingNumber() {
RegionCode.AR));
}
function testFormatOutOfCountryWithInvalidRegion() {
// AQ/Antarctica isn't a valid region code for phone number formatting,
// so this falls back to intl formatting.
assertEquals('+1 650 253 0000',
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, 'AQ'));
}
function testFormatOutOfCountryWithPreferredIntlPrefix() {
// This should use 0011, since that is the preferred international prefix
// (both 0011 and 0012 are accepted as possible international prefixes in our
@ -1453,17 +1460,20 @@ function testMaybeStripNationalPrefix() {
var numberToStrip = new goog.string.StringBuffer('34356778');
/** @type {string} */
var strippedNumber = '356778';
phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata);
assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, null));
assertEquals('Should have had national prefix stripped.',
strippedNumber, numberToStrip.toString());
// Retry stripping - now the number should not start with the national prefix,
// so no more stripping should occur.
phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata);
assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, null));
assertEquals('Should have had no change - no national prefix present.',
strippedNumber, numberToStrip.toString());
// Some countries have no national prefix. Repeat test with none specified.
metadata.setNationalPrefixForParsing('');
phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata);
assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, null));
assertEquals('Should not strip anything with empty national prefix.',
strippedNumber, numberToStrip.toString());
// If the resultant number doesn't match the national rule, it shouldn't be
@ -1471,7 +1481,8 @@ function testMaybeStripNationalPrefix() {
metadata.setNationalPrefixForParsing('3');
numberToStrip = new goog.string.StringBuffer('3123');
strippedNumber = '3123';
phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata);
assertFalse(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, null));
assertEquals('Should have had no change - after stripping, it would not ' +
'have matched the national rule.',
strippedNumber, numberToStrip.toString());
@ -1479,9 +1490,11 @@ function testMaybeStripNationalPrefix() {
metadata.setNationalPrefixForParsing('0(81)?');
numberToStrip = new goog.string.StringBuffer('08122123456');
strippedNumber = '22123456';
assertEquals('81',
phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata));
/** @type {!goog.string.StringBuffer} */
var carrierCode = new goog.string.StringBuffer();
assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, carrierCode));
assertEquals('81', carrierCode.toString());
assertEquals('Should have had national prefix and carrier code stripped.',
strippedNumber, numberToStrip.toString());
// If there was a transform rule, check it was applied.
@ -1491,7 +1504,8 @@ function testMaybeStripNationalPrefix() {
numberToStrip = new goog.string.StringBuffer('031123');
/** @type {string} */
var transformedNumber = '5315123';
phoneUtil.maybeStripNationalPrefixAndCarrierCode(numberToStrip, metadata);
assertTrue(phoneUtil.maybeStripNationalPrefixAndCarrierCode(
numberToStrip, metadata, null));
assertEquals('Should transform the 031 to a 5315.',
transformedNumber, numberToStrip.toString());
}


+ 1
- 0
javascript/i18n/phonenumbers/regioncodefortesting.js View File

@ -49,6 +49,7 @@ i18n.phonenumbers.RegionCode = {
SG: 'SG',
US: 'US',
YT: 'YT',
ZW: 'ZW',
// Official code for the unknown region.
ZZ: 'ZZ'
};

+ 6
- 0
tools/java/java-build/src/com/google/i18n/phonenumbers/BuildMetadataJsonFromXml.java View File

@ -202,6 +202,12 @@ public class BuildMetadataJsonFromXml extends Command {
} else {
jsArrayBuilder.append(null);
}
// optional bool national_prefix_optional_when_formatting = 6;
if (format.hasNationalPrefixOptionalWhenFormatting()) {
jsArrayBuilder.append(format.isNationalPrefixOptionalWhenFormatting());
} else {
jsArrayBuilder.append(null);
}
jsArrayBuilder.endArray();
}


+ 7
- 0
tools/java/java-build/src/com/google/i18n/phonenumbers/JSArrayBuilder.java View File

@ -75,6 +75,13 @@ public class JSArrayBuilder implements CharSequence {
return append(string, true);
}
/**
* Add a boolean to the array.
*/
public JSArrayBuilder append(boolean b) {
return append(b ? 1 : 0);
}
/**
* Add a collection of strings to the array.
*/


Loading…
Cancel
Save