Skip to content

Commit

Permalink
Update libphonenumber@8.8.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ruimarinho committed Jun 21, 2024
1 parent b103985 commit f5665a2
Show file tree
Hide file tree
Showing 14 changed files with 5,964 additions and 12,693 deletions.
21 changes: 2 additions & 19 deletions src/asyoutypeformatter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*
* @author Nikolaos Trogkanis
*/
goog.provide('i18n.phonenumbers.AsYouTypeFormatterTest');
goog.setTestOnly();

goog.require('goog.testing.jsunit');
goog.require('i18n.phonenumbers.AsYouTypeFormatter');
Expand Down Expand Up @@ -75,7 +73,7 @@ function testInvalidPlusSign() {
}

function testTooLongNumberMatchingMultipleLeadingDigits() {
// See https://github.com/google/libphonenumber/issues/36
// See https://github.com/googlei18n/libphonenumber/issues/36
// The bug occurred last time for countries which have two formatting rules
// with exactly the same leading digits pattern but differ in length.
/** @type {i18n.phonenumbers.AsYouTypeFormatter} */
Expand Down Expand Up @@ -665,21 +663,6 @@ function testAYTF_MX() {
assertEquals('+52 800 123 45', f.inputDigit('5'));
assertEquals('+52 800 123 456', f.inputDigit('6'));
assertEquals('+52 800 123 4567', f.inputDigit('7'));

// +529011234567, proactively ensuring that no formatting is applied, where a format is chosen
// that would otherwise have led to some digits being dropped.
f.clear();
assertEquals('9', f.inputDigit('9'));
assertEquals('90', f.inputDigit('0'));
assertEquals('901', f.inputDigit('1'));
assertEquals('9011', f.inputDigit('1'));
assertEquals('90112', f.inputDigit('2'));
assertEquals('901123', f.inputDigit('3'));
assertEquals('9011234', f.inputDigit('4'));
assertEquals('90112345', f.inputDigit('5'));
assertEquals('901123456', f.inputDigit('6'));
assertEquals('9011234567', f.inputDigit('7'));


// +52 55 1234 5678
f.clear();
Expand Down Expand Up @@ -1216,7 +1199,7 @@ function testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss() {
// leading digit patterns; when we try again to extract a country code we
// should ensure we use the last leading digit pattern, rather than the first
// one such that it *thinks* it's found a valid formatting rule again.
// https://github.com/google/libphonenumber/issues/437
// https://github.com/googlei18n/libphonenumber/issues/437
assertEquals('+8698812', f.inputDigit('2'));
assertEquals('+86988123', f.inputDigit('3'));
assertEquals('+869881234', f.inputDigit('4'));
Expand Down
1,049 changes: 415 additions & 634 deletions src/demo-compiled.js

Large diffs are not rendered by default.

198 changes: 77 additions & 121 deletions src/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,144 +20,127 @@
*
* @author Nikolaos Trogkanis
*/
goog.provide('i18n.phonenumbers.demo');

goog.require('goog.dom');
goog.require('goog.json');
goog.require('goog.proto2.ObjectSerializer');
goog.require('goog.string.StringBuffer');
goog.require('i18n.phonenumbers.AsYouTypeFormatter');
goog.require('i18n.phonenumbers.PhoneNumberFormat');
goog.require('i18n.phonenumbers.PhoneNumberType');
goog.require('i18n.phonenumbers.PhoneNumberUtil');
goog.require('i18n.phonenumbers.PhoneNumberUtil.ValidationResult');
goog.require('i18n.phonenumbers.ShortNumberInfo');


/**
* @const
* @type {!i18n.phonenumbers.PhoneNumberUtil}
* @private
*/
var phoneUtil_ = i18n.phonenumbers.PhoneNumberUtil.getInstance();

function phoneNumberParser() {
var $ = goog.dom.getElement;
var phoneNumber = $('phoneNumber').value;
var regionCode = $('defaultCountry').value.toUpperCase();
var regionCode = $('defaultCountry').value;
var carrierCode = $('carrierCode').value;
var output = new goog.string.StringBuffer();
try {
var number = phoneUtil_.parseAndKeepRawInput(phoneNumber, regionCode);
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
var number = phoneUtil.parseAndKeepRawInput(phoneNumber, regionCode);
output.append('****Parsing Result:****\n');
output.append(JSON.stringify(
new goog.proto2
.ObjectSerializer(goog.proto2.ObjectSerializer.KeyOption.NAME)
.serialize(number)));
output.append(goog.json.serialize(new goog.proto2.ObjectSerializer(
goog.proto2.ObjectSerializer.KeyOption.NAME).serialize(number)));
output.append('\n\n****Validation Results:****');
var isPossible = phoneUtil_.isPossibleNumber(number);
var isPossible = phoneUtil.isPossibleNumber(number);
output.append('\nResult from isPossibleNumber(): ');
output.append(isPossible);
var validationResult = i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
var isPossibleReason = phoneUtil_.isPossibleNumberWithReason(number)
var hasRegionCode = regionCode && regionCode != 'ZZ';
if (isPossible) {
// Checking as isValid() fails if possible local only.
if (isPossibleReason == validationResult.IS_POSSIBLE_LOCAL_ONLY) {
output.append('\nResult from isPossibleNumberWithReason(): ');
output.append('IS_POSSIBLE_LOCAL_ONLY');
output.append(
'\nNumber is considered invalid as it is ' +
'not a possible national number.');
} else {
var isNumberValid = phoneUtil_.isValidNumber(number);
output.append('\nResult from isValidNumber(): ');
output.append(isNumberValid);
if (isNumberValid && hasRegionCode) {
output.append('\nResult from isValidNumberForRegion(): ');
output.append(phoneUtil_.isValidNumberForRegion(number, regionCode));
}
output.append('\nPhone Number region: ');
output.append(phoneUtil_.getRegionCodeForNumber(number));
output.append('\nResult from getNumberType(): ');
output.append(getNumberTypeString(number));
}
} else {
if (!isPossible) {
output.append('\nResult from isPossibleNumberWithReason(): ');
switch (isPossibleReason) {
case validationResult.INVALID_COUNTRY_CODE:
var PNV = i18n.phonenumbers.PhoneNumberUtil.ValidationResult;
switch (phoneUtil.isPossibleNumberWithReason(number)) {
case PNV.INVALID_COUNTRY_CODE:
output.append('INVALID_COUNTRY_CODE');
break;
case validationResult.TOO_SHORT:
case PNV.TOO_SHORT:
output.append('TOO_SHORT');
break;
case validationResult.TOO_LONG:
case PNV.TOO_LONG:
output.append('TOO_LONG');
break;
case validationResult.INVALID_LENGTH:
output.append('INVALID_LENGTH');
break;
}
// IS_POSSIBLE shouldn't happen, since we only call this if _not_
// possible.
output.append(
'\nNote: Numbers that are not possible have type UNKNOWN,' +
' an unknown region, and are considered invalid.');
}
if (!isNumberValid) {
var shortInfo = i18n.phonenumbers.ShortNumberInfo.getInstance();
output.append('\n\n****ShortNumberInfo Results:****');
output.append('\nResult from isPossibleShortNumber: ');
output.append(shortInfo.isPossibleShortNumber(number));
output.append('\nResult from isValidShortNumber: ');
output.append(shortInfo.isValidShortNumber(number));
if (hasRegionCode) {
output.append('\nResult from isPossibleShortNumberForRegion: ');
output.append(
shortInfo.isPossibleShortNumberForRegion(number, regionCode));
output.append('\nResult from isValidShortNumberForRegion: ');
output.append(
shortInfo.isValidShortNumberForRegion(number, regionCode));
output.append('\nNote: numbers that are not possible have type ' +
'UNKNOWN, an unknown region, and are considered invalid.');
} else {
var isNumberValid = phoneUtil.isValidNumber(number);
output.append('\nResult from isValidNumber(): ');
output.append(isNumberValid);
if (isNumberValid && regionCode && regionCode != 'ZZ') {
output.append('\nResult from isValidNumberForRegion(): ');
output.append(phoneUtil.isValidNumberForRegion(number, regionCode));
}
output.append('\nPhone Number region: ');
output.append(phoneUtil.getRegionCodeForNumber(number));
output.append('\nResult from getNumberType(): ');
var PNT = i18n.phonenumbers.PhoneNumberType;
switch (phoneUtil.getNumberType(number)) {
case PNT.FIXED_LINE:
output.append('FIXED_LINE');
break;
case PNT.MOBILE:
output.append('MOBILE');
break;
case PNT.FIXED_LINE_OR_MOBILE:
output.append('FIXED_LINE_OR_MOBILE');
break;
case PNT.TOLL_FREE:
output.append('TOLL_FREE');
break;
case PNT.PREMIUM_RATE:
output.append('PREMIUM_RATE');
break;
case PNT.SHARED_COST:
output.append('SHARED_COST');
break;
case PNT.VOIP:
output.append('VOIP');
break;
case PNT.PERSONAL_NUMBER:
output.append('PERSONAL_NUMBER');
break;
case PNT.PAGER:
output.append('PAGER');
break;
case PNT.UAN:
output.append('UAN');
break;
case PNT.UNKNOWN:
output.append('UNKNOWN');
break;
}
}

var PNF = i18n.phonenumbers.PhoneNumberFormat;
output.append('\n\n****Formatting Results:**** ');
output.append('\nE164 format: ');
output.append(
isNumberValid ? phoneUtil_.format(number, PNF.E164) : 'invalid');
output.append(isNumberValid ?
phoneUtil.format(number, PNF.E164) :
'invalid');
output.append('\nOriginal format: ');
output.append(phoneUtil_.formatInOriginalFormat(number, regionCode));
output.append(phoneUtil.formatInOriginalFormat(number, regionCode));
output.append('\nNational format: ');
output.append(phoneUtil_.format(number, PNF.NATIONAL));
output.append(phoneUtil.format(number, PNF.NATIONAL));
output.append('\nInternational format: ');
output.append(
isNumberValid ? phoneUtil_.format(number, PNF.INTERNATIONAL) :
'invalid');
output.append(isNumberValid ?
phoneUtil.format(number, PNF.INTERNATIONAL) :
'invalid');
output.append('\nOut-of-country format from US: ');
output.append(
isNumberValid ?
phoneUtil_.formatOutOfCountryCallingNumber(number, 'US') :
'invalid');
output.append(isNumberValid ?
phoneUtil.formatOutOfCountryCallingNumber(number, 'US') :
'invalid');
output.append('\nOut-of-country format from Switzerland: ');
output.append(
isNumberValid ?
phoneUtil_.formatOutOfCountryCallingNumber(number, 'CH') :
'invalid');
output.append(isNumberValid ?
phoneUtil.formatOutOfCountryCallingNumber(number, 'CH') :
'invalid');
if (carrierCode.length > 0) {
output.append('\nNational format with carrier code: ');
output.append(
phoneUtil_.formatNationalNumberWithCarrierCode(number, carrierCode));
output.append(phoneUtil.formatNationalNumberWithCarrierCode(number,
carrierCode));
}
output.append('\nFormat for mobile dialing (calling from US): ');
output.append(
isNumberValid ?
phoneUtil_.formatNumberForMobileDialing(number, 'US', true) :
'invalid');
output.append('\nFormat for national dialing with preferred carrier code and empty fallback carrier code: ');
output.append(
isNumberValid ?
phoneUtil_.formatNationalNumberWithPreferredCarrierCode(number, '') :
'invalid');
output.append('\n\n****AsYouTypeFormatter Results****');
var formatter = new i18n.phonenumbers.AsYouTypeFormatter(regionCode);
var phoneNumberLength = phoneNumber.length;
Expand All @@ -175,31 +158,4 @@ function phoneNumberParser() {
return false;
}

function getNumberTypeString(number) {
switch (phoneUtil_.getNumberType(number)) {
case i18n.phonenumbers.PhoneNumberType.FIXED_LINE:
return 'FIXED_LINE';
case i18n.phonenumbers.PhoneNumberType.MOBILE:
return 'MOBILE'
case i18n.phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE:
return 'FIXED_LINE_OR_MOBILE';
case i18n.phonenumbers.PhoneNumberType.TOLL_FREE:
return 'TOLL_FREE';
case i18n.phonenumbers.PhoneNumberType.PREMIUM_RATE:
return 'PREMIUM_RATE';
case i18n.phonenumbers.PhoneNumberType.SHARED_COST:
return 'SHARED_COST';
case i18n.phonenumbers.PhoneNumberType.VOIP:
return 'VOIP';
case i18n.phonenumbers.PhoneNumberType.PERSONAL_NUMBER:
return 'PERSONAL_NUMBER';
case i18n.phonenumbers.PhoneNumberType.PAGER:
return 'PAGER';
case i18n.phonenumbers.PhoneNumberType.UAN:
return 'UAN';
case i18n.phonenumbers.PhoneNumberType.UNKNOWN:
return 'UNKNOWN';
}
}

goog.exportSymbol('phoneNumberParser', phoneNumberParser);
Loading

0 comments on commit f5665a2

Please sign in to comment.