Browse Source

Replace goog.array API calls with native API equivalents. (#2627)

pull/2667/head
Kelvin Jin 4 years ago
committed by GitHub
parent
commit
5c309c56a9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 109 deletions
  1. +41
    -47
      javascript/i18n/phonenumbers/phonenumberutil.js
  2. +36
    -44
      javascript/i18n/phonenumbers/phonenumberutil_test.js
  3. +15
    -18
      javascript/i18n/phonenumbers/shortnumberinfo.js

+ 41
- 47
javascript/i18n/phonenumbers/phonenumberutil.js View File

@ -33,7 +33,6 @@ goog.provide('i18n.phonenumbers.PhoneNumberUtil');
goog.provide('i18n.phonenumbers.PhoneNumberUtil.MatchType');
goog.provide('i18n.phonenumbers.PhoneNumberUtil.ValidationResult');
goog.require('goog.array');
goog.require('goog.object');
goog.require('goog.proto2.PbLiteSerializer');
goog.require('goog.string');
@ -1406,9 +1405,8 @@ i18n.phonenumbers.PhoneNumberUtil.getCountryMobileToken =
* region the library supports.
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.getSupportedRegions = function() {
return goog.array.filter(
Object.keys(i18n.phonenumbers.metadata.countryToMetadata),
function(regionCode) {
return Object.keys(i18n.phonenumbers.metadata.countryToMetadata)
.filter(function(regionCode) {
return isNaN(regionCode);
});
};
@ -1420,17 +1418,16 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.getSupportedRegions = function() {
* @return {!Array.<number>} the country calling codes for every
* non-geographical entity the library supports.
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.
getSupportedGlobalNetworkCallingCodes = function() {
var callingCodesAsStrings = goog.array.filter(
Object.keys(i18n.phonenumbers.metadata.countryToMetadata),
function(regionCode) {
return !isNaN(regionCode);
});
return goog.array.map(callingCodesAsStrings,
function(callingCode) {
return parseInt(callingCode, 10);
});
i18n.phonenumbers.PhoneNumberUtil.prototype
.getSupportedGlobalNetworkCallingCodes = function() {
var callingCodesAsStrings =
Object.keys(i18n.phonenumbers.metadata.countryToMetadata)
.filter(function(regionCode) {
return !isNaN(regionCode);
});
return callingCodesAsStrings.map(function(callingCode) {
return parseInt(callingCode, 10);
});
};
@ -1447,12 +1444,12 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.getSupportedCallingCodes =
function() {
var countryCodesAsStrings =
Object.keys(i18n.phonenumbers.metadata.countryCodeToRegionCodeMap);
return goog.array.join(
this.getSupportedGlobalNetworkCallingCodes(),
goog.array.map(countryCodesAsStrings,
function(callingCode) {
return parseInt(callingCode, 10);
}));
return [
...this.getSupportedGlobalNetworkCallingCodes(),
...countryCodesAsStrings.map(function(callingCode) {
return parseInt(callingCode, 10);
})
];
};
@ -1645,9 +1642,8 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.isNumberGeographical =
return numberType == i18n.phonenumbers.PhoneNumberType.FIXED_LINE ||
numberType == i18n.phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE ||
(goog.array.contains(
i18n.phonenumbers.PhoneNumberUtil.GEO_MOBILE_COUNTRIES_,
phoneNumber.getCountryCodeOrDefault()) &&
(i18n.phonenumbers.PhoneNumberUtil.GEO_MOBILE_COUNTRIES_.includes(
phoneNumber.getCountryCodeOrDefault()) &&
numberType == i18n.phonenumbers.PhoneNumberType.MOBILE);
};
@ -2793,16 +2789,15 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.getExampleNumberForNonGeoEntity =
var metadata =
this.getMetadataForNonGeographicalRegion(countryCallingCode);
if (metadata != null) {
/** @type {i18n.phonenumbers.PhoneNumberDesc} */
var numberTypeWithExampleNumber = goog.array.find(
[metadata.getMobile(), metadata.getTollFree(),
metadata.getSharedCost(), metadata.getVoip(),
metadata.getVoicemail(), metadata.getUan(),
metadata.getPremiumRate()],
function(desc, index) {
return (desc.hasExampleNumber());
});
if (numberTypeWithExampleNumber != null) {
/** @type {!i18n.phonenumbers.PhoneNumberDesc|undefined} */
var numberTypeWithExampleNumber = [
metadata.getMobile(), metadata.getTollFree(), metadata.getSharedCost(),
metadata.getVoip(), metadata.getVoicemail(), metadata.getUan(),
metadata.getPremiumRate()
].find(function(desc, index) {
return desc.hasExampleNumber();
});
if (numberTypeWithExampleNumber !== undefined) {
try {
return this.parse('+' + countryCallingCode +
numberTypeWithExampleNumber.getExampleNumber(), 'ZZ');
@ -3031,8 +3026,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.isNumberMatchingDesc_ =
// already checked before a specific number type.
var actualLength = nationalNumber.length;
if (numberDesc.possibleLengthCount() > 0 &&
goog.array.indexOf(numberDesc.possibleLengthArray(),
actualLength) == -1) {
numberDesc.possibleLengthArray().indexOf(actualLength) == -1) {
return false;
}
return i18n.phonenumbers.PhoneNumberUtil.matchesEntirely(
@ -3304,13 +3298,13 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.getNddPrefixForRegion = function(
* @param {?string} regionCode the CLDR two-letter region code.
* @return {boolean} true if regionCode is one of the regions under NANPA.
*/
i18n.phonenumbers.PhoneNumberUtil.prototype.isNANPACountry =
function(regionCode) {
return regionCode != null && goog.array.contains(
i18n.phonenumbers.metadata.countryCodeToRegionCodeMap[
i18n.phonenumbers.PhoneNumberUtil.NANPA_COUNTRY_CODE_],
regionCode.toUpperCase());
i18n.phonenumbers.PhoneNumberUtil.prototype.isNANPACountry = function(
regionCode) {
return regionCode != null &&
i18n.phonenumbers.metadata
.countryCodeToRegionCodeMap[i18n.phonenumbers.PhoneNumberUtil
.NANPA_COUNTRY_CODE_]
.includes(regionCode.toUpperCase());
};
@ -3462,14 +3456,14 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.testNumberLengthForType_ =
// The current list is sorted; we need to merge in the new list and
// re-sort (duplicates are okay). Sorting isn't so expensive because the
// lists are very small.
goog.array.sort(possibleLengths);
possibleLengths.sort();
if (localLengths.length == 0) {
localLengths = mobileDesc.possibleLengthLocalOnlyArray();
} else {
localLengths = localLengths.concat(
mobileDesc.possibleLengthLocalOnlyArray());
goog.array.sort(localLengths);
localLengths.sort();
}
}
}
@ -3483,7 +3477,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.testNumberLengthForType_ =
var actualLength = number.length;
// This is safe because there is never an overlap beween the possible lengths
// and the local-only lengths; this is checked at build time.
if (goog.array.indexOf(localLengths, actualLength) > -1) {
if (localLengths.indexOf(actualLength) > -1) {
return i18n.phonenumbers.PhoneNumberUtil.ValidationResult
.IS_POSSIBLE_LOCAL_ONLY;
}
@ -3496,7 +3490,7 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.testNumberLengthForType_ =
return i18n.phonenumbers.PhoneNumberUtil.ValidationResult.TOO_LONG;
}
// We skip the first element since we've already checked it.
return (goog.array.indexOf(possibleLengths, actualLength, 1) > -1) ?
return (possibleLengths.indexOf(actualLength, 1) > -1) ?
i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE :
i18n.phonenumbers.PhoneNumberUtil.ValidationResult.INVALID_LENGTH;
};


+ 36
- 44
javascript/i18n/phonenumbers/phonenumberutil_test.js View File

@ -27,7 +27,6 @@
goog.provide('i18n.phonenumbers.PhoneNumberUtilTest');
goog.setTestOnly();
goog.require('goog.array');
goog.require('goog.string.StringBuffer');
goog.require('goog.testing.jsunit');
goog.require('i18n.phonenumbers.Error');
@ -449,63 +448,56 @@ function testGetCountryMobileToken() {
function testGetSupportedRegions() {
assertTrue(phoneUtil.getSupportedRegions().length > 0);
assertTrue(
goog.array.contains(phoneUtil.getSupportedRegions(), RegionCode.US));
assertFalse(
goog.array.contains(phoneUtil.getSupportedRegions(), RegionCode.UN001));
assertFalse(goog.array.contains(phoneUtil.getSupportedRegions(), '800'));
assertTrue(phoneUtil.getSupportedRegions().includes(RegionCode.US));
assertFalse(phoneUtil.getSupportedRegions().includes(RegionCode.UN001));
assertFalse(phoneUtil.getSupportedRegions().includes('800'));
}
function testGetSupportedGlobalNetworkCallingCodes() {
assertTrue(phoneUtil.getSupportedGlobalNetworkCallingCodes().length > 0);
assertFalse(goog.array.contains(
phoneUtil.getSupportedGlobalNetworkCallingCodes(), RegionCode.US));
assertTrue(goog.array.contains(
phoneUtil.getSupportedGlobalNetworkCallingCodes(), 800));
goog.array.forEach(
phoneUtil.getSupportedGlobalNetworkCallingCodes(),
function(countryCallingCode) {
assertEquals(
RegionCode.UN001,
phoneUtil.getRegionCodeForCountryCode(countryCallingCode));
});
assertFalse(phoneUtil.getSupportedGlobalNetworkCallingCodes().includes(
RegionCode.US));
assertTrue(phoneUtil.getSupportedGlobalNetworkCallingCodes().includes(800));
phoneUtil.getSupportedGlobalNetworkCallingCodes().forEach(function(
countryCallingCode) {
assertEquals(
RegionCode.UN001,
phoneUtil.getRegionCodeForCountryCode(countryCallingCode));
});
}
function testGetSupportedCallingCodes() {
assertTrue(phoneUtil.getSupportedCallingCodes().length > 0);
goog.array.forEach(
phoneUtil.getSupportedCallingCodes(), function(callingCode) {
assertTrue(callingCode > 0);
assertFalse(
phoneUtil.getRegionCodeForCountryCode(callingCode) ==
RegionCode.ZZ);
});
phoneUtil.getSupportedCallingCodes().forEach(function(callingCode) {
assertTrue(callingCode > 0);
assertFalse(
phoneUtil.getRegionCodeForCountryCode(callingCode) == RegionCode.ZZ);
});
// There should be more than just the global network calling codes in this
// set.
assertTrue(
phoneUtil.getSupportedCallingCodes().length >
phoneUtil.getSupportedGlobalNetworkCallingCodes().length);
// But they should be included. Testing one of them.
assertTrue(goog.array.contains(
phoneUtil.getSupportedGlobalNetworkCallingCodes(), 979));
assertTrue(phoneUtil.getSupportedGlobalNetworkCallingCodes().includes(979));
}
function testGetSupportedTypesForRegion() {
var PNT = i18n.phonenumbers.PhoneNumberType;
var types = phoneUtil.getSupportedTypesForRegion(RegionCode.BR);
assertTrue(goog.array.contains(types, PNT.FIXED_LINE));
assertTrue(types.includes(PNT.FIXED_LINE));
// Our test data has no mobile numbers for Brazil.
assertFalse(goog.array.contains(types, PNT.MOBILE));
assertFalse(types.includes(PNT.MOBILE));
// UNKNOWN should never be returned.
assertFalse(goog.array.contains(types, PNT.UNKNOWN));
assertFalse(types.includes(PNT.UNKNOWN));
// In the US, many numbers are classified as FIXED_LINE_OR_MOBILE; but we
// don't want to expose this as a supported type, instead we say FIXED_LINE
// and MOBILE are both present.
types = phoneUtil.getSupportedTypesForRegion(RegionCode.US);
assertTrue(goog.array.contains(types, PNT.FIXED_LINE));
assertTrue(goog.array.contains(types, PNT.MOBILE));
assertFalse(goog.array.contains(types, PNT.FIXED_LINE_OR_MOBILE));
assertTrue(types.includes(PNT.FIXED_LINE));
assertTrue(types.includes(PNT.MOBILE));
assertFalse(types.includes(PNT.FIXED_LINE_OR_MOBILE));
types = phoneUtil.getSupportedTypesForRegion(RegionCode.ZZ);
assertTrue(types.length == 0);
@ -518,11 +510,11 @@ function testGetSupportedTypesForNonGeoEntity() {
assertTrue(types.length == 0);
types = phoneUtil.getSupportedTypesForNonGeoEntity(979);
assertTrue(goog.array.contains(types, PNT.PREMIUM_RATE));
assertTrue(types.includes(PNT.PREMIUM_RATE));
// Our test data has no mobile numbers for Brazil.
assertFalse(goog.array.contains(types, PNT.MOBILE));
assertFalse(types.includes(PNT.MOBILE));
// UNKNOWN should never be returned.
assertFalse(goog.array.contains(types, PNT.UNKNOWN));
assertFalse(types.includes(PNT.UNKNOWN));
}
function testGetNationalSignificantNumber() {
@ -1833,16 +1825,16 @@ function testGetRegionCodeForNumber() {
function testGetRegionCodesForCountryCode() {
/** @type {!Array.<string>} */
var regionCodesForNANPA = phoneUtil.getRegionCodesForCountryCode(1);
assertTrue(goog.array.contains(regionCodesForNANPA, RegionCode.US));
assertTrue(goog.array.contains(regionCodesForNANPA, RegionCode.BS));
assertTrue(goog.array.contains(
phoneUtil.getRegionCodesForCountryCode(44), RegionCode.GB));
assertTrue(goog.array.contains(
phoneUtil.getRegionCodesForCountryCode(49), RegionCode.DE));
assertTrue(goog.array.contains(
phoneUtil.getRegionCodesForCountryCode(800), RegionCode.UN001));
assertTrue(regionCodesForNANPA.includes(RegionCode.US));
assertTrue(regionCodesForNANPA.includes(RegionCode.BS));
assertTrue(
phoneUtil.getRegionCodesForCountryCode(44).includes(RegionCode.GB));
assertTrue(
phoneUtil.getRegionCodesForCountryCode(49).includes(RegionCode.DE));
assertTrue(
phoneUtil.getRegionCodesForCountryCode(800).includes(RegionCode.UN001));
// Test with invalid country calling code.
assertTrue(goog.array.isEmpty(phoneUtil.getRegionCodesForCountryCode(-1)));
assertTrue((phoneUtil.getRegionCodesForCountryCode(-1).length === 0));
}
function testGetCountryCodeForRegion() {


+ 15
- 18
javascript/i18n/phonenumbers/shortnumberinfo.js View File

@ -30,7 +30,6 @@
goog.provide('i18n.phonenumbers.ShortNumberInfo');
goog.require('goog.array');
goog.require('goog.proto2.PbLiteSerializer');
goog.require('i18n.phonenumbers.PhoneMetadata');
goog.require('i18n.phonenumbers.PhoneNumber');
@ -110,7 +109,7 @@ i18n.phonenumbers.ShortNumberInfo.prototype.regionDialingFromMatchesNumber_ =
function(number, regionDialingFrom) {
var regionCodes = this.getRegionCodesForCountryCode_(
number.getCountryCodeOrDefault());
return goog.array.contains(regionCodes, regionDialingFrom);
return regionDialingFrom != null && regionCodes.includes(regionDialingFrom);
};
@ -133,8 +132,8 @@ i18n.phonenumbers.ShortNumberInfo.prototype.isPossibleShortNumberForRegion =
return false;
}
var numberLength = this.getNationalSignificantNumber_(number).length;
return goog.array.contains(
phoneMetadata.getGeneralDesc().possibleLengthArray(), numberLength);
return phoneMetadata.getGeneralDesc().possibleLengthArray().includes(
numberLength);
};
@ -159,7 +158,7 @@ i18n.phonenumbers.ShortNumberInfo.prototype.isPossibleShortNumber =
continue;
}
var possibleLengths = phoneMetadata.getGeneralDesc().possibleLengthArray();
if (goog.array.contains(possibleLengths, shortNumberLength)) {
if (possibleLengths.includes(shortNumberLength)) {
return true;
}
}
@ -266,8 +265,8 @@ i18n.phonenumbers.ShortNumberInfo.prototype.getExpectedCostForRegion =
}
var shortNumber = this.getNationalSignificantNumber_(number);
if (!goog.array.contains(phoneMetadata.getGeneralDesc().possibleLengthArray(),
shortNumber.length)) {
if (!phoneMetadata.getGeneralDesc().possibleLengthArray().includes(
shortNumber.length)) {
return ShortNumberCost.UNKNOWN_COST;
}
if (this.matchesPossibleNumberAndNationalNumber_(
@ -389,9 +388,8 @@ i18n.phonenumbers.ShortNumberInfo.prototype.getRegionCodeForShortNumberFromRegio
* @package
*/
i18n.phonenumbers.ShortNumberInfo.prototype.getSupportedRegions = function() {
return goog.array.filter(
Object.keys(i18n.phonenumbers.shortnumbermetadata.countryToMetadata),
function(regionCode) {
return Object.keys(i18n.phonenumbers.shortnumbermetadata.countryToMetadata)
.filter(function(regionCode) {
return isNaN(regionCode);
});
};
@ -559,10 +557,9 @@ i18n.phonenumbers.ShortNumberInfo.prototype.matchesEmergencyNumberHelper_ =
var normalizedNumber = i18n.phonenumbers.PhoneNumberUtil
.normalizeDigitsOnly(possibleNumber);
var allowPrefixMatchForRegion = allowPrefixMatch && !goog.array.contains(
i18n.phonenumbers.ShortNumberInfo.
REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT_,
regionCode);
var allowPrefixMatchForRegion = allowPrefixMatch &&
!i18n.phonenumbers.ShortNumberInfo
.REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT_.includes(regionCode);
var emergencyNumberPattern = metadata.getEmergency()
.getNationalNumberPatternOrDefault();
var result = i18n.phonenumbers.PhoneNumberUtil.matchesEntirely(
@ -700,10 +697,10 @@ i18n.phonenumbers.ShortNumberInfo.prototype.getNationalSignificantNumber_ =
* @return {boolean}
* @private
*/
i18n.phonenumbers.ShortNumberInfo.prototype.matchesPossibleNumberAndNationalNumber_ =
function(number, numberDesc) {
if (numberDesc.possibleLengthArray().length > 0 && !goog.array.contains(
numberDesc.possibleLengthArray(), number.length)) {
i18n.phonenumbers.ShortNumberInfo.prototype
.matchesPossibleNumberAndNationalNumber_ = function(number, numberDesc) {
if (numberDesc.possibleLengthArray().length > 0 &&
!numberDesc.possibleLengthArray().includes(number.length)) {
return false;
}
return i18n.phonenumbers.PhoneNumberUtil.matchesEntirely(


Loading…
Cancel
Save