google-libphonenumber
Version:
A browserify-compatible wrapper for Google's libphonenumber.
1,203 lines (1,020 loc) • 146 kB
JavaScript
/**
* @license
* Copyright (C) 2010 The Libphonenumber Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Unit tests for the PhoneNumberUtil.
*
* Note that these tests use the metadata contained in metadatafortesting.js,
* not the normal metadata files, so should not be used for regression test
* purposes - these tests are illustrative only and test functionality.
*
* @author Nikolaos Trogkanis
*/
goog.require('goog.array');
goog.require('goog.testing.jsunit');
goog.require('i18n.phonenumbers.PhoneNumberUtil');
goog.require('i18n.phonenumbers.RegionCode');
/** @type {i18n.phonenumbers.PhoneNumberUtil} */
var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
// Set up some test numbers to re-use.
// TODO: Rewrite this as static functions that return new numbers each time to
// avoid any risk of accidental changes to mutable static state affecting many
// tests.
/** @type {i18n.phonenumbers.PhoneNumber} */
var ALPHA_NUMERIC_NUMBER = new i18n.phonenumbers.PhoneNumber();
ALPHA_NUMERIC_NUMBER.setCountryCode(1);
ALPHA_NUMERIC_NUMBER.setNationalNumber(80074935247);
/** @type {i18n.phonenumbers.PhoneNumber} */
var AE_UAN = new i18n.phonenumbers.PhoneNumber();
AE_UAN.setCountryCode(971);
AE_UAN.setNationalNumber(600123456);
/** @type {i18n.phonenumbers.PhoneNumber} */
var AR_MOBILE = new i18n.phonenumbers.PhoneNumber();
AR_MOBILE.setCountryCode(54);
AR_MOBILE.setNationalNumber(91187654321);
/** @type {i18n.phonenumbers.PhoneNumber} */
var AR_NUMBER = new i18n.phonenumbers.PhoneNumber();
AR_NUMBER.setCountryCode(54);
AR_NUMBER.setNationalNumber(1187654321);
/** @type {i18n.phonenumbers.PhoneNumber} */
var AU_NUMBER = new i18n.phonenumbers.PhoneNumber();
AU_NUMBER.setCountryCode(61);
AU_NUMBER.setNationalNumber(236618300);
/** @type {i18n.phonenumbers.PhoneNumber} */
var BS_MOBILE = new i18n.phonenumbers.PhoneNumber();
BS_MOBILE.setCountryCode(1);
BS_MOBILE.setNationalNumber(2423570000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var BS_NUMBER = new i18n.phonenumbers.PhoneNumber();
BS_NUMBER.setCountryCode(1);
BS_NUMBER.setNationalNumber(2423651234);
// Note that this is the same as the example number for DE in the metadata.
/** @type {i18n.phonenumbers.PhoneNumber} */
var DE_NUMBER = new i18n.phonenumbers.PhoneNumber();
DE_NUMBER.setCountryCode(49);
DE_NUMBER.setNationalNumber(30123456);
/** @type {i18n.phonenumbers.PhoneNumber} */
var DE_SHORT_NUMBER = new i18n.phonenumbers.PhoneNumber();
DE_SHORT_NUMBER.setCountryCode(49);
DE_SHORT_NUMBER.setNationalNumber(1234);
/** @type {i18n.phonenumbers.PhoneNumber} */
var GB_MOBILE = new i18n.phonenumbers.PhoneNumber();
GB_MOBILE.setCountryCode(44);
GB_MOBILE.setNationalNumber(7912345678);
/** @type {i18n.phonenumbers.PhoneNumber} */
var GB_NUMBER = new i18n.phonenumbers.PhoneNumber();
GB_NUMBER.setCountryCode(44);
GB_NUMBER.setNationalNumber(2070313000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var IT_MOBILE = new i18n.phonenumbers.PhoneNumber();
IT_MOBILE.setCountryCode(39);
IT_MOBILE.setNationalNumber(345678901);
/** @type {i18n.phonenumbers.PhoneNumber} */
var IT_NUMBER = new i18n.phonenumbers.PhoneNumber();
IT_NUMBER.setCountryCode(39);
IT_NUMBER.setNationalNumber(236618300);
IT_NUMBER.setItalianLeadingZero(true);
/** @type {i18n.phonenumbers.PhoneNumber} */
var JP_STAR_NUMBER = new i18n.phonenumbers.PhoneNumber();
JP_STAR_NUMBER.setCountryCode(81);
JP_STAR_NUMBER.setNationalNumber(2345);
// Numbers to test the formatting rules from Mexico.
/** @type {i18n.phonenumbers.PhoneNumber} */
var MX_MOBILE1 = new i18n.phonenumbers.PhoneNumber();
MX_MOBILE1.setCountryCode(52);
MX_MOBILE1.setNationalNumber(12345678900);
/** @type {i18n.phonenumbers.PhoneNumber} */
var MX_MOBILE2 = new i18n.phonenumbers.PhoneNumber();
MX_MOBILE2.setCountryCode(52);
MX_MOBILE2.setNationalNumber(15512345678);
/** @type {i18n.phonenumbers.PhoneNumber} */
var MX_NUMBER1 = new i18n.phonenumbers.PhoneNumber();
MX_NUMBER1.setCountryCode(52);
MX_NUMBER1.setNationalNumber(3312345678);
/** @type {i18n.phonenumbers.PhoneNumber} */
var MX_NUMBER2 = new i18n.phonenumbers.PhoneNumber();
MX_NUMBER2.setCountryCode(52);
MX_NUMBER2.setNationalNumber(8211234567);
/** @type {i18n.phonenumbers.PhoneNumber} */
var NZ_NUMBER = new i18n.phonenumbers.PhoneNumber();
NZ_NUMBER.setCountryCode(64);
NZ_NUMBER.setNationalNumber(33316005);
/** @type {i18n.phonenumbers.PhoneNumber} */
var SG_NUMBER = new i18n.phonenumbers.PhoneNumber();
SG_NUMBER.setCountryCode(65);
SG_NUMBER.setNationalNumber(65218000);
// A too-long and hence invalid US number.
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_LONG_NUMBER = new i18n.phonenumbers.PhoneNumber();
US_LONG_NUMBER.setCountryCode(1);
US_LONG_NUMBER.setNationalNumber(65025300001);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_NUMBER = new i18n.phonenumbers.PhoneNumber();
US_NUMBER.setCountryCode(1);
US_NUMBER.setNationalNumber(6502530000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_PREMIUM = new i18n.phonenumbers.PhoneNumber();
US_PREMIUM.setCountryCode(1);
US_PREMIUM.setNationalNumber(9002530000);
// Too short, but still possible US numbers.
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_LOCAL_NUMBER = new i18n.phonenumbers.PhoneNumber();
US_LOCAL_NUMBER.setCountryCode(1);
US_LOCAL_NUMBER.setNationalNumber(2530000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_SHORT_BY_ONE_NUMBER = new i18n.phonenumbers.PhoneNumber();
US_SHORT_BY_ONE_NUMBER.setCountryCode(1);
US_SHORT_BY_ONE_NUMBER.setNationalNumber(650253000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_TOLLFREE = new i18n.phonenumbers.PhoneNumber();
US_TOLLFREE.setCountryCode(1);
US_TOLLFREE.setNationalNumber(8002530000);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_SPOOF = new i18n.phonenumbers.PhoneNumber();
US_SPOOF.setCountryCode(1);
US_SPOOF.setNationalNumber(0);
/** @type {i18n.phonenumbers.PhoneNumber} */
var US_SPOOF_WITH_RAW_INPUT = new i18n.phonenumbers.PhoneNumber();
US_SPOOF_WITH_RAW_INPUT.setCountryCode(1);
US_SPOOF_WITH_RAW_INPUT.setNationalNumber(0);
US_SPOOF_WITH_RAW_INPUT.setRawInput('000-000-0000');
/** @type {i18n.phonenumbers.PhoneNumber} */
var INTERNATIONAL_TOLL_FREE = new i18n.phonenumbers.PhoneNumber();
INTERNATIONAL_TOLL_FREE.setCountryCode(800);
INTERNATIONAL_TOLL_FREE.setNationalNumber(12345678);
// We set this to be the same length as numbers for the other non-geographical
// country prefix that we have in our test metadata. However, this is not
// considered valid because they differ in their country calling code.
/** @type {i18n.phonenumbers.PhoneNumber} */
var INTERNATIONAL_TOLL_FREE_TOO_LONG = new i18n.phonenumbers.PhoneNumber();
INTERNATIONAL_TOLL_FREE_TOO_LONG.setCountryCode(800);
INTERNATIONAL_TOLL_FREE_TOO_LONG.setNationalNumber(123456789);
/** @type {i18n.phonenumbers.PhoneNumber} */
var UNIVERSAL_PREMIUM_RATE = new i18n.phonenumbers.PhoneNumber();
UNIVERSAL_PREMIUM_RATE.setCountryCode(979);
UNIVERSAL_PREMIUM_RATE.setNationalNumber(123456789);
/** @type {i18n.phonenumbers.PhoneNumber} */
var UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT = new i18n.phonenumbers.PhoneNumber();
UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT.setCountryCode(2);
UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT.setNationalNumber(12345);
var RegionCode = i18n.phonenumbers.RegionCode;
function testGetInstanceLoadUSMetadata() {
/** @type {i18n.phonenumbers.PhoneMetadata} */
var metadata = phoneUtil.getMetadataForRegion(RegionCode.US);
assertEquals(RegionCode.US, metadata.getId());
assertEquals(1, metadata.getCountryCode());
assertEquals('011', metadata.getInternationalPrefix());
assertTrue(metadata.hasNationalPrefix());
assertEquals(2, metadata.numberFormatCount());
assertEquals('(\\d{3})(\\d{3})(\\d{4})',
metadata.getNumberFormat(1).getPattern());
assertEquals('$1 $2 $3', metadata.getNumberFormat(1).getFormat());
assertEquals('[13-689]\\d{9}|2[0-35-9]\\d{8}',
metadata.getGeneralDesc().getNationalNumberPattern());
assertEquals('\\d{7}(?:\\d{3})?',
metadata.getGeneralDesc().getPossibleNumberPattern());
assertTrue(metadata.getGeneralDesc().equals(metadata.getFixedLine()));
assertEquals('\\d{10}',
metadata.getTollFree().getPossibleNumberPattern());
assertEquals('900\\d{7}',
metadata.getPremiumRate().getNationalNumberPattern());
// No shared-cost data is available, so it should be initialised to 'NA'.
assertEquals('NA', metadata.getSharedCost().getNationalNumberPattern());
assertEquals('NA', metadata.getSharedCost().getPossibleNumberPattern());
}
function testGetInstanceLoadDEMetadata() {
/** @type {i18n.phonenumbers.PhoneMetadata} */
var metadata = phoneUtil.getMetadataForRegion(RegionCode.DE);
assertEquals(RegionCode.DE, metadata.getId());
assertEquals(49, metadata.getCountryCode());
assertEquals('00', metadata.getInternationalPrefix());
assertEquals('0', metadata.getNationalPrefix());
assertEquals(6, metadata.numberFormatCount());
assertEquals(1, metadata.getNumberFormat(5).leadingDigitsPatternCount());
assertEquals('900', metadata.getNumberFormat(5).getLeadingDigitsPattern(0));
assertEquals('(\\d{3})(\\d{3,4})(\\d{4})',
metadata.getNumberFormat(5).getPattern());
assertEquals('$1 $2 $3', metadata.getNumberFormat(5).getFormat());
assertEquals('(?:[24-6]\\d{2}|3[03-9]\\d|[789](?:[1-9]\\d|0[2-9]))\\d{1,8}',
metadata.getFixedLine().getNationalNumberPattern());
assertEquals('\\d{2,14}', metadata.getFixedLine().getPossibleNumberPattern());
assertEquals('30123456', metadata.getFixedLine().getExampleNumber());
assertEquals('\\d{10}', metadata.getTollFree().getPossibleNumberPattern());
assertEquals('900([135]\\d{6}|9\\d{7})',
metadata.getPremiumRate().getNationalNumberPattern());
}
function testGetInstanceLoadARMetadata() {
/** @type {i18n.phonenumbers.PhoneMetadata} */
var metadata = phoneUtil.getMetadataForRegion(RegionCode.AR);
assertEquals(RegionCode.AR, metadata.getId());
assertEquals(54, metadata.getCountryCode());
assertEquals('00', metadata.getInternationalPrefix());
assertEquals('0', metadata.getNationalPrefix());
assertEquals('0(?:(11|343|3715)15)?', metadata.getNationalPrefixForParsing());
assertEquals('9$1', metadata.getNationalPrefixTransformRule());
assertEquals('$2 15 $3-$4', metadata.getNumberFormat(2).getFormat());
assertEquals('(9)(\\d{4})(\\d{2})(\\d{4})',
metadata.getNumberFormat(3).getPattern());
assertEquals('(9)(\\d{4})(\\d{2})(\\d{4})',
metadata.getIntlNumberFormat(3).getPattern());
assertEquals('$1 $2 $3 $4', metadata.getIntlNumberFormat(3).getFormat());
}
function testGetInstanceLoadInternationalTollFreeMetadata() {
/** @type {i18n.phonenumbers.PhoneMetadata} */
var metadata = phoneUtil.getMetadataForNonGeographicalRegion(800);
assertEquals('001', metadata.getId());
assertEquals(800, metadata.getCountryCode());
assertEquals('$1 $2', metadata.getNumberFormat(0).getFormat());
assertEquals('(\\d{4})(\\d{4})', metadata.getNumberFormat(0).getPattern());
assertEquals('12345678', metadata.getGeneralDesc().getExampleNumber());
assertEquals('12345678', metadata.getTollFree().getExampleNumber());
}
function testIsNumberGeographical() {
// Bahamas, mobile phone number.
assertFalse(phoneUtil.isNumberGeographical(BS_MOBILE));
// Australian fixed line number.
assertTrue(phoneUtil.isNumberGeographical(AU_NUMBER));
// International toll free number.
assertFalse(phoneUtil.isNumberGeographical(INTERNATIONAL_TOLL_FREE));
// We test that mobile phone numbers in relevant regions are indeed considered
// geographical.
// Argentina, mobile phone number.
assertTrue(phoneUtil.isNumberGeographical(AR_MOBILE));
// Mexico, mobile phone number.
assertTrue(phoneUtil.isNumberGeographical(MX_MOBILE1));
// Mexico, another mobile phone number.
assertTrue(phoneUtil.isNumberGeographical(MX_MOBILE2));
}
function testIsLeadingZeroPossible() {
// Italy
assertTrue(phoneUtil.isLeadingZeroPossible(39));
// USA
assertFalse(phoneUtil.isLeadingZeroPossible(1));
// International toll free
assertTrue(phoneUtil.isLeadingZeroPossible(800));
// International premium-rate
assertFalse(phoneUtil.isLeadingZeroPossible(979));
// Not in metadata file, just default to false.
assertFalse(phoneUtil.isLeadingZeroPossible(888));
}
function testGetLengthOfGeographicalAreaCode() {
// Google MTV, which has area code '650'.
assertEquals(3, phoneUtil.getLengthOfGeographicalAreaCode(US_NUMBER));
// A North America toll-free number, which has no area code.
assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(US_TOLLFREE));
// Google London, which has area code '20'.
assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(GB_NUMBER));
// A UK mobile phone, which has no area code.
assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(GB_MOBILE));
// Google Buenos Aires, which has area code '11'.
assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(AR_NUMBER));
// Google Sydney, which has area code '2'.
assertEquals(1, phoneUtil.getLengthOfGeographicalAreaCode(AU_NUMBER));
// Italian numbers - there is no national prefix, but it still has an area
// code.
assertEquals(2, phoneUtil.getLengthOfGeographicalAreaCode(IT_NUMBER));
// Google Singapore. Singapore has no area code and no national prefix.
assertEquals(0, phoneUtil.getLengthOfGeographicalAreaCode(SG_NUMBER));
// An invalid US number (1 digit shorter), which has no area code.
assertEquals(0,
phoneUtil.getLengthOfGeographicalAreaCode(US_SHORT_BY_ONE_NUMBER));
// An international toll free number, which has no area code.
assertEquals(0,
phoneUtil.getLengthOfGeographicalAreaCode(INTERNATIONAL_TOLL_FREE));
}
function testGetLengthOfNationalDestinationCode() {
// Google MTV, which has national destination code (NDC) '650'.
assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_NUMBER));
// A North America toll-free number, which has NDC '800'.
assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(US_TOLLFREE));
// Google London, which has NDC '20'.
assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(GB_NUMBER));
// A UK mobile phone, which has NDC '7912'.
assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(GB_MOBILE));
// Google Buenos Aires, which has NDC '11'.
assertEquals(2, phoneUtil.getLengthOfNationalDestinationCode(AR_NUMBER));
// An Argentinian mobile which has NDC '911'.
assertEquals(3, phoneUtil.getLengthOfNationalDestinationCode(AR_MOBILE));
// Google Sydney, which has NDC '2'.
assertEquals(1, phoneUtil.getLengthOfNationalDestinationCode(AU_NUMBER));
// Google Singapore, which has NDC '6521'.
assertEquals(4, phoneUtil.getLengthOfNationalDestinationCode(SG_NUMBER));
// An invalid US number (1 digit shorter), which has no NDC.
assertEquals(0,
phoneUtil.getLengthOfNationalDestinationCode(US_SHORT_BY_ONE_NUMBER));
// A number containing an invalid country calling code, which shouldn't have
// any NDC.
/** @type {i18n.phonenumbers.PhoneNumber} */
var number = new i18n.phonenumbers.PhoneNumber();
number.setCountryCode(123);
number.setNationalNumber(6502530000);
assertEquals(0, phoneUtil.getLengthOfNationalDestinationCode(number));
// An international toll free number, which has NDC '1234'.
assertEquals(4,
phoneUtil.getLengthOfNationalDestinationCode(INTERNATIONAL_TOLL_FREE));
}
function testGetCountryMobileToken() {
assertEquals('1', i18n.phonenumbers.PhoneNumberUtil.getCountryMobileToken(
phoneUtil.getCountryCodeForRegion(RegionCode.MX)));
// Country calling code for Sweden, which has no mobile token.
assertEquals('', i18n.phonenumbers.PhoneNumberUtil.getCountryMobileToken(
phoneUtil.getCountryCodeForRegion(RegionCode.SE)));
}
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'));
}
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));
});
}
function testGetNationalSignificantNumber() {
assertEquals('6502530000',
phoneUtil.getNationalSignificantNumber(US_NUMBER));
// An Italian mobile number.
assertEquals('345678901',
phoneUtil.getNationalSignificantNumber(IT_MOBILE));
// An Italian fixed line number.
assertEquals('0236618300',
phoneUtil.getNationalSignificantNumber(IT_NUMBER));
assertEquals('12345678',
phoneUtil.getNationalSignificantNumber(INTERNATIONAL_TOLL_FREE));
}
function testGetExampleNumber() {
var PNT = i18n.phonenumbers.PhoneNumberType;
assertTrue(DE_NUMBER.equals(phoneUtil.getExampleNumber(RegionCode.DE)));
assertTrue(DE_NUMBER.equals(
phoneUtil.getExampleNumberForType(RegionCode.DE, PNT.FIXED_LINE)));
assertNull(phoneUtil.getExampleNumberForType(RegionCode.DE, PNT.MOBILE));
// For the US, the example number is placed under general description, and
// hence should be used for both fixed line and mobile, so neither of these
// should return null.
assertNotNull(
phoneUtil.getExampleNumberForType(RegionCode.US, PNT.FIXED_LINE));
assertNotNull(phoneUtil.getExampleNumberForType(RegionCode.US, PNT.MOBILE));
// CS is an invalid region, so we have no data for it.
assertNull(phoneUtil.getExampleNumberForType(RegionCode.CS, PNT.MOBILE));
// RegionCode 001 is reserved for supporting non-geographical country calling
// code. We don't support getting an example number for it with this method.
assertNull(phoneUtil.getExampleNumber(RegionCode.UN001));
}
function testGetExampleNumberForNonGeoEntity() {
assertTrue(INTERNATIONAL_TOLL_FREE.equals(
phoneUtil.getExampleNumberForNonGeoEntity(800)));
assertTrue(UNIVERSAL_PREMIUM_RATE.equals(
phoneUtil.getExampleNumberForNonGeoEntity(979)));
}
function testConvertAlphaCharactersInNumber() {
/** @type {string} */
var input = '1800-ABC-DEF';
// Alpha chars are converted to digits; everything else is left untouched.
/** @type {string} */
var expectedOutput = '1800-222-333';
assertEquals(expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.convertAlphaCharactersInNumber(input));
}
function testNormaliseRemovePunctuation() {
/** @type {string} */
var inputNumber = '034-56&+#2\u00AD34';
/** @type {string} */
var expectedOutput = '03456234';
assertEquals('Conversion did not correctly remove punctuation',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalize(inputNumber));
}
function testNormaliseReplaceAlphaCharacters() {
/** @type {string} */
var inputNumber = '034-I-am-HUNGRY';
/** @type {string} */
var expectedOutput = '034426486479';
assertEquals('Conversion did not correctly replace alpha characters',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalize(inputNumber));
}
function testNormaliseOtherDigits() {
/** @type {string} */
var inputNumber = '\uFF125\u0665';
/** @type {string} */
var expectedOutput = '255';
assertEquals('Conversion did not correctly replace non-latin digits',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalize(inputNumber));
// Eastern-Arabic digits.
inputNumber = '\u06F52\u06F0';
expectedOutput = '520';
assertEquals('Conversion did not correctly replace non-latin digits',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalize(inputNumber));
}
function testNormaliseStripAlphaCharacters() {
/** @type {string} */
var inputNumber = '034-56&+a#234';
/** @type {string} */
var expectedOutput = '03456234';
assertEquals('Conversion did not correctly remove alpha character',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalizeDigitsOnly(inputNumber));
}
function testFormatUSNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('650 253 0000',
phoneUtil.format(US_NUMBER, PNF.NATIONAL));
assertEquals('+1 650 253 0000',
phoneUtil.format(US_NUMBER, PNF.INTERNATIONAL));
assertEquals('800 253 0000',
phoneUtil.format(US_TOLLFREE, PNF.NATIONAL));
assertEquals('+1 800 253 0000',
phoneUtil.format(US_TOLLFREE, PNF.INTERNATIONAL));
assertEquals('900 253 0000',
phoneUtil.format(US_PREMIUM, PNF.NATIONAL));
assertEquals('+1 900 253 0000',
phoneUtil.format(US_PREMIUM, PNF.INTERNATIONAL));
assertEquals('tel:+1-900-253-0000',
phoneUtil.format(US_PREMIUM, PNF.RFC3966));
// Numbers with all zeros in the national number part will be formatted by
// using the raw_input if that is available no matter which format is
// specified.
assertEquals('000-000-0000',
phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PNF.NATIONAL));
assertEquals('0', phoneUtil.format(US_SPOOF, PNF.NATIONAL));
}
function testFormatBSNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('242 365 1234',
phoneUtil.format(BS_NUMBER, PNF.NATIONAL));
assertEquals('+1 242 365 1234',
phoneUtil.format(BS_NUMBER, PNF.INTERNATIONAL));
}
function testFormatGBNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('(020) 7031 3000',
phoneUtil.format(GB_NUMBER, PNF.NATIONAL));
assertEquals('+44 20 7031 3000',
phoneUtil.format(GB_NUMBER, PNF.INTERNATIONAL));
assertEquals('(07912) 345 678',
phoneUtil.format(GB_MOBILE, PNF.NATIONAL));
assertEquals('+44 7912 345 678',
phoneUtil.format(GB_MOBILE, PNF.INTERNATIONAL));
}
function testFormatDENumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
/** @type {i18n.phonenumbers.PhoneNumber} */
var deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(301234);
assertEquals('030/1234',
phoneUtil.format(deNumber, PNF.NATIONAL));
assertEquals('+49 30/1234',
phoneUtil.format(deNumber, PNF.INTERNATIONAL));
assertEquals('tel:+49-30-1234',
phoneUtil.format(deNumber, PNF.RFC3966));
deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(291123);
assertEquals('0291 123',
phoneUtil.format(deNumber, PNF.NATIONAL));
assertEquals('+49 291 123',
phoneUtil.format(deNumber, PNF.INTERNATIONAL));
deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(29112345678);
assertEquals('0291 12345678',
phoneUtil.format(deNumber, PNF.NATIONAL));
assertEquals('+49 291 12345678',
phoneUtil.format(deNumber, PNF.INTERNATIONAL));
deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(912312345);
assertEquals('09123 12345',
phoneUtil.format(deNumber, PNF.NATIONAL));
assertEquals('+49 9123 12345',
phoneUtil.format(deNumber, PNF.INTERNATIONAL));
deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(80212345);
assertEquals('08021 2345',
phoneUtil.format(deNumber, PNF.NATIONAL));
assertEquals('+49 8021 2345',
phoneUtil.format(deNumber, PNF.INTERNATIONAL));
// Note this number is correctly formatted without national prefix. Most of
// the numbers that are treated as invalid numbers by the library are short
// numbers, and they are usually not dialed with national prefix.
assertEquals('1234',
phoneUtil.format(DE_SHORT_NUMBER, PNF.NATIONAL));
assertEquals('+49 1234',
phoneUtil.format(DE_SHORT_NUMBER, PNF.INTERNATIONAL));
deNumber = new i18n.phonenumbers.PhoneNumber();
deNumber.setCountryCode(49);
deNumber.setNationalNumber(41341234);
assertEquals('04134 1234',
phoneUtil.format(deNumber, PNF.NATIONAL));
}
function testFormatITNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('02 3661 8300',
phoneUtil.format(IT_NUMBER, PNF.NATIONAL));
assertEquals('+39 02 3661 8300',
phoneUtil.format(IT_NUMBER, PNF.INTERNATIONAL));
assertEquals('+390236618300',
phoneUtil.format(IT_NUMBER, PNF.E164));
assertEquals('345 678 901',
phoneUtil.format(IT_MOBILE, PNF.NATIONAL));
assertEquals('+39 345 678 901',
phoneUtil.format(IT_MOBILE, PNF.INTERNATIONAL));
assertEquals('+39345678901',
phoneUtil.format(IT_MOBILE, PNF.E164));
}
function testFormatAUNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('02 3661 8300',
phoneUtil.format(AU_NUMBER, PNF.NATIONAL));
assertEquals('+61 2 3661 8300',
phoneUtil.format(AU_NUMBER, PNF.INTERNATIONAL));
assertEquals('+61236618300',
phoneUtil.format(AU_NUMBER, PNF.E164));
/** @type {i18n.phonenumbers.PhoneNumber} */
var auNumber = new i18n.phonenumbers.PhoneNumber();
auNumber.setCountryCode(61);
auNumber.setNationalNumber(1800123456);
assertEquals('1800 123 456',
phoneUtil.format(auNumber, PNF.NATIONAL));
assertEquals('+61 1800 123 456',
phoneUtil.format(auNumber, PNF.INTERNATIONAL));
assertEquals('+611800123456',
phoneUtil.format(auNumber, PNF.E164));
}
function testFormatARNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('011 8765-4321',
phoneUtil.format(AR_NUMBER, PNF.NATIONAL));
assertEquals('+54 11 8765-4321',
phoneUtil.format(AR_NUMBER, PNF.INTERNATIONAL));
assertEquals('+541187654321',
phoneUtil.format(AR_NUMBER, PNF.E164));
assertEquals('011 15 8765-4321',
phoneUtil.format(AR_MOBILE, PNF.NATIONAL));
assertEquals('+54 9 11 8765 4321',
phoneUtil.format(AR_MOBILE, PNF.INTERNATIONAL));
assertEquals('+5491187654321',
phoneUtil.format(AR_MOBILE, PNF.E164));
}
function testFormatMXNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('045 234 567 8900',
phoneUtil.format(MX_MOBILE1, PNF.NATIONAL));
assertEquals('+52 1 234 567 8900',
phoneUtil.format(MX_MOBILE1, PNF.INTERNATIONAL));
assertEquals('+5212345678900',
phoneUtil.format(MX_MOBILE1, PNF.E164));
assertEquals('045 55 1234 5678',
phoneUtil.format(MX_MOBILE2, PNF.NATIONAL));
assertEquals('+52 1 55 1234 5678',
phoneUtil.format(MX_MOBILE2, PNF.INTERNATIONAL));
assertEquals('+5215512345678',
phoneUtil.format(MX_MOBILE2, PNF.E164));
assertEquals('01 33 1234 5678',
phoneUtil.format(MX_NUMBER1, PNF.NATIONAL));
assertEquals('+52 33 1234 5678',
phoneUtil.format(MX_NUMBER1, PNF.INTERNATIONAL));
assertEquals('+523312345678',
phoneUtil.format(MX_NUMBER1, PNF.E164));
assertEquals('01 821 123 4567',
phoneUtil.format(MX_NUMBER2, PNF.NATIONAL));
assertEquals('+52 821 123 4567',
phoneUtil.format(MX_NUMBER2, PNF.INTERNATIONAL));
assertEquals('+528211234567',
phoneUtil.format(MX_NUMBER2, PNF.E164));
}
function testFormatOutOfCountryCallingNumber() {
assertEquals('00 1 900 253 0000',
phoneUtil.formatOutOfCountryCallingNumber(US_PREMIUM, RegionCode.DE));
assertEquals('1 650 253 0000',
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.BS));
assertEquals('00 1 650 253 0000',
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.PL));
assertEquals('011 44 7912 345 678',
phoneUtil.formatOutOfCountryCallingNumber(GB_MOBILE, RegionCode.US));
assertEquals('00 49 1234',
phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER,
RegionCode.GB));
// Note this number is correctly formatted without national prefix. Most of
// the numbers that are treated as invalid numbers by the library are short
// numbers, and they are usually not dialed with national prefix.
assertEquals('1234',
phoneUtil.formatOutOfCountryCallingNumber(DE_SHORT_NUMBER,
RegionCode.DE));
assertEquals('011 39 02 3661 8300',
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.US));
assertEquals('02 3661 8300',
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.IT));
assertEquals('+39 02 3661 8300',
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER, RegionCode.SG));
assertEquals('6521 8000',
phoneUtil.formatOutOfCountryCallingNumber(SG_NUMBER, RegionCode.SG));
assertEquals('011 54 9 11 8765 4321',
phoneUtil.formatOutOfCountryCallingNumber(AR_MOBILE, RegionCode.US));
assertEquals('011 800 1234 5678',
phoneUtil.formatOutOfCountryCallingNumber(INTERNATIONAL_TOLL_FREE,
RegionCode.US));
/** @type {i18n.phonenumbers.PhoneNumber} */
var arNumberWithExtn = AR_MOBILE.clone();
arNumberWithExtn.setExtension('1234');
assertEquals('011 54 9 11 8765 4321 ext. 1234',
phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn,
RegionCode.US));
assertEquals('0011 54 9 11 8765 4321 ext. 1234',
phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn,
RegionCode.AU));
assertEquals('011 15 8765-4321 ext. 1234',
phoneUtil.formatOutOfCountryCallingNumber(arNumberWithExtn,
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, RegionCode.AQ));
// For region code 001, the out-of-country format always turns into the
// international format.
assertEquals('+1 650 253 0000',
phoneUtil.formatOutOfCountryCallingNumber(US_NUMBER, RegionCode.UN001));
}
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
// test metadta.)
assertEquals('0011 39 02 3661 8300',
phoneUtil.formatOutOfCountryCallingNumber(IT_NUMBER,
RegionCode.AU));
}
function testFormatOutOfCountryKeepingAlphaChars() {
/** @type {i18n.phonenumbers.PhoneNumber} */
var alphaNumericNumber = new i18n.phonenumbers.PhoneNumber();
alphaNumericNumber.setCountryCode(1);
alphaNumericNumber.setNationalNumber(8007493524);
alphaNumericNumber.setRawInput('1800 six-flag');
assertEquals('0011 1 800 SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
alphaNumericNumber.setRawInput('1-800-SIX-flag');
assertEquals('0011 1 800-SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
alphaNumericNumber.setRawInput('Call us from UK: 00 1 800 SIX-flag');
assertEquals('0011 1 800 SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
alphaNumericNumber.setRawInput('800 SIX-flag');
assertEquals('0011 1 800 SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
// Formatting from within the NANPA region.
assertEquals('1 800 SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.US));
assertEquals('1 800 SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.BS));
// Testing that if the raw input doesn't exist, it is formatted using
// formatOutOfCountryCallingNumber.
alphaNumericNumber.clearRawInput();
assertEquals('00 1 800 749 3524',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.DE));
// Testing AU alpha number formatted from Australia.
alphaNumericNumber.setCountryCode(61);
alphaNumericNumber.setNationalNumber(827493524);
alphaNumericNumber.setRawInput('+61 82749-FLAG');
// This number should have the national prefix fixed.
assertEquals('082749-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
alphaNumericNumber.setRawInput('082749-FLAG');
assertEquals('082749-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
alphaNumericNumber.setNationalNumber(18007493524);
alphaNumericNumber.setRawInput('1-800-SIX-flag');
// This number should not have the national prefix prefixed, in accordance
// with the override for this specific formatting rule.
assertEquals('1-800-SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AU));
// The metadata should not be permanently changed, since we copied it before
// modifying patterns. Here we check this.
alphaNumericNumber.setNationalNumber(1800749352);
assertEquals('1800 749 352',
phoneUtil.formatOutOfCountryCallingNumber(alphaNumericNumber,
RegionCode.AU));
// Testing a region with multiple international prefixes.
assertEquals('+61 1-800-SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.SG));
// Testing the case of calling from a non-supported region.
assertEquals('+61 1-800-SIX-FLAG',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AQ));
// Testing the case with an invalid country calling code.
alphaNumericNumber.setCountryCode(0);
alphaNumericNumber.setNationalNumber(18007493524);
alphaNumericNumber.setRawInput('1-800-SIX-flag');
// Uses the raw input only.
assertEquals('1-800-SIX-flag',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.DE));
// Testing the case of an invalid alpha number.
alphaNumericNumber.setCountryCode(1);
alphaNumericNumber.setNationalNumber(80749);
alphaNumericNumber.setRawInput('180-SIX');
// No country-code stripping can be done.
assertEquals('00 1 180-SIX',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.DE));
// Testing the case of calling from a non-supported region.
alphaNumericNumber.setCountryCode(1);
alphaNumericNumber.setNationalNumber(80749);
alphaNumericNumber.setRawInput('180-SIX');
// No country-code stripping can be done since the number is invalid.
assertEquals('+1 180-SIX',
phoneUtil.formatOutOfCountryKeepingAlphaChars(alphaNumericNumber,
RegionCode.AQ));
}
function testFormatWithCarrierCode() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
// We only support this for AR in our test metadata, and only for mobile
// numbers starting with certain values.
/** @type {i18n.phonenumbers.PhoneNumber} */
var arMobile = new i18n.phonenumbers.PhoneNumber();
arMobile.setCountryCode(54);
arMobile.setNationalNumber(92234654321);
assertEquals('02234 65-4321', phoneUtil.format(arMobile, PNF.NATIONAL));
// Here we force 14 as the carrier code.
assertEquals('02234 14 65-4321',
phoneUtil.formatNationalNumberWithCarrierCode(arMobile, '14'));
// Here we force the number to be shown with no carrier code.
assertEquals('02234 65-4321',
phoneUtil.formatNationalNumberWithCarrierCode(arMobile, ''));
// Here the international rule is used, so no carrier code should be present.
assertEquals('+5492234654321', phoneUtil.format(arMobile, PNF.E164));
// We don't support this for the US so there should be no change.
assertEquals('650 253 0000',
phoneUtil.formatNationalNumberWithCarrierCode(US_NUMBER, '15'));
// Invalid country code should just get the NSN.
assertEquals('12345', phoneUtil.formatNationalNumberWithCarrierCode(
UNKNOWN_COUNTRY_CODE_NO_RAW_INPUT, '89'));
}
function testFormatWithPreferredCarrierCode() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
// We only support this for AR in our test metadata.
/** @type {i18n.phonenumbers.PhoneNumber} */
var arNumber = new i18n.phonenumbers.PhoneNumber();
arNumber.setCountryCode(54);
arNumber.setNationalNumber(91234125678);
// Test formatting with no preferred carrier code stored in the number itself.
assertEquals('01234 15 12-5678',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, '15'));
assertEquals('01234 12-5678',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, ''));
// Test formatting with preferred carrier code present.
arNumber.setPreferredDomesticCarrierCode('19');
assertEquals('01234 12-5678', phoneUtil.format(arNumber, PNF.NATIONAL));
assertEquals('01234 19 12-5678',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, '15'));
assertEquals('01234 19 12-5678',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, ''));
// When the preferred_domestic_carrier_code is present (even when it contains
// an empty string), use it instead of the default carrier code passed in.
arNumber.setPreferredDomesticCarrierCode('');
assertEquals('01234 12-5678',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(arNumber, '15'));
// We don't support this for the US so there should be no change.
/** @type {i18n.phonenumbers.PhoneNumber} */
var usNumber = new i18n.phonenumbers.PhoneNumber();
usNumber.setCountryCode(1);
usNumber.setNationalNumber(4241231234);
usNumber.setPreferredDomesticCarrierCode('99');
assertEquals('424 123 1234', phoneUtil.format(usNumber, PNF.NATIONAL));
assertEquals('424 123 1234',
phoneUtil.formatNationalNumberWithPreferredCarrierCode(usNumber, '15'));
}
function testFormatNumberForMobileDialing() {
// Numbers are normally dialed in national format in-country, and
// international format from outside the country.
assertEquals('030123456',
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.DE, false));
assertEquals('+4930123456',
phoneUtil.formatNumberForMobileDialing(DE_NUMBER, RegionCode.CH, false));
var deNumberWithExtn = DE_NUMBER.clone();
deNumberWithExtn.setExtension('1234');
assertEquals('030123456',
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.DE,
false));
assertEquals('+4930123456',
phoneUtil.formatNumberForMobileDialing(deNumberWithExtn, RegionCode.CH,
false));
// US toll free numbers are marked as noInternationalDialling in the test
// metadata for testing purposes. For such numbers, we expect nothing to be
// returned when the region code is not the same one.
assertEquals('800 253 0000',
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.US, true));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE, RegionCode.CN, true));
assertEquals('+1 650 253 0000',
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, true));
/** @type {i18n.phonenumbers.PhoneNumber} */
var usNumberWithExtn = US_NUMBER.clone();
usNumberWithExtn.setExtension('1234');
assertEquals('+1 650 253 0000',
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn,
RegionCode.US, true));
assertEquals('8002530000',
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE,
RegionCode.US, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(US_TOLLFREE,
RegionCode.CN, false));
assertEquals('+16502530000',
phoneUtil.formatNumberForMobileDialing(US_NUMBER, RegionCode.US, false));
assertEquals('+16502530000',
phoneUtil.formatNumberForMobileDialing(usNumberWithExtn,
RegionCode.US, false));
// An invalid US number, which is one digit too long.
assertEquals('+165025300001',
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER,
RegionCode.US, false));
assertEquals('+1 65025300001',
phoneUtil.formatNumberForMobileDialing(US_LONG_NUMBER,
RegionCode.US, true));
// Star numbers. In real life they appear in Israel, but we have them in JP
// in our test metadata.
assertEquals('*2345',
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER,
RegionCode.JP, false));
assertEquals('*2345',
phoneUtil.formatNumberForMobileDialing(JP_STAR_NUMBER,
RegionCode.JP, true));
assertEquals('+80012345678',
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE,
RegionCode.JP, false));
assertEquals('+800 1234 5678',
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE,
RegionCode.JP, true));
// UAE numbers beginning with 600 (classified as UAN) need to be dialled
// without +971 locally.
assertEquals('+971600123456',
phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.JP, false));
assertEquals('600123456',
phoneUtil.formatNumberForMobileDialing(AE_UAN, RegionCode.AE, false));
assertEquals('+523312345678',
phoneUtil.formatNumberForMobileDialing(MX_NUMBER1, RegionCode.MX,
false));
assertEquals('+523312345678',
phoneUtil.formatNumberForMobileDialing(MX_NUMBER1, RegionCode.US,
false));
// Non-geographical numbers should always be dialed in international format.
assertEquals('+80012345678',
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE,
RegionCode.US, false));
assertEquals('+80012345678',
phoneUtil.formatNumberForMobileDialing(INTERNATIONAL_TOLL_FREE,
RegionCode.UN001, false));
// Test that a short number is formatted correctly for mobile dialing within
// the region, and is not diallable from outside the region.
var deShortNumber = new i18n.phonenumbers.PhoneNumber();
deShortNumber.setCountryCode(49);
deShortNumber.setNationalNumber(123);
assertEquals('123',
phoneUtil.formatNumberForMobileDialing(deShortNumber,
RegionCode.DE, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(deShortNumber,
RegionCode.IT, false));
// Test the special logic for Hungary, where the national prefix must be added
// before dialing from a mobile phone for regular length numbers, but not for
// short numbers.
var huRegularNumber = new i18n.phonenumbers.PhoneNumber();
huRegularNumber.setCountryCode(36);
huRegularNumber.setNationalNumber(301234567);
assertEquals('06301234567',
phoneUtil.formatNumberForMobileDialing(huRegularNumber,
RegionCode.HU, false));
assertEquals('+36301234567',
phoneUtil.formatNumberForMobileDialing(huRegularNumber,
RegionCode.JP, false));
var huShortNumber = new i18n.phonenumbers.PhoneNumber();
huShortNumber.setCountryCode(36);
huShortNumber.setNationalNumber(104);
assertEquals('104',
phoneUtil.formatNumberForMobileDialing(huShortNumber,
RegionCode.HU, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(huShortNumber,
RegionCode.JP, false));
// Test the special logic for NANPA countries, for which regular length phone
// numbers are always output in international format, but short numbers are in
// national format.
assertEquals('+16502530000',
phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.US, false));
assertEquals('+16502530000',
phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.CA, false));
assertEquals('+16502530000',
phoneUtil.formatNumberForMobileDialing(US_NUMBER,
RegionCode.BR, false));
var usShortNumber = new i18n.phonenumbers.PhoneNumber();
usShortNumber.setCountryCode(1);
usShortNumber.setNationalNumber(911);
assertEquals('911',
phoneUtil.formatNumberForMobileDialing(usShortNumber,
RegionCode.US, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(usShortNumber,
RegionCode.CA, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(usShortNumber,
RegionCode.BR, false));
// Test that the Australian emergency number 000 is formatted correctly.
var auShortNumber = new i18n.phonenumbers.PhoneNumber();
auShortNumber.setCountryCode(61);
auShortNumber.setNationalNumber(0);
auShortNumber.setItalianLeadingZero(true);
auShortNumber.setNumberOfLeadingZeros(2);
assertEquals('000',
phoneUtil.formatNumberForMobileDialing(auShortNumber,
RegionCode.AU, false));
assertEquals('',
phoneUtil.formatNumberForMobileDialing(auShortNumber,
RegionCode.NZ, false));
}
function testFormatByPattern() {
var PNF = i18n.phonenumbers.PhoneNumberFormat;
/** @type {i18n.phonenumbers.NumberFormat} */
var newNumFormat = new i18n.phonenumbers.NumberFormat();
newNumFormat.setPattern('(\\d{3})(\\d{3})(\\d{4})');
newNumFormat.setFormat('($1) $2-$3');
assertEquals('(650) 253-0000',
phoneUtil.formatByPattern(US_NUMBER,
PNF.NATIONAL,
[newNumFormat]));
assertEquals('+1 (650) 253-0000',
phoneUtil.formatByPattern(US_NUMBER,
PNF.INTERNATIONAL,
[newNumFormat]));
assertEquals('tel:+1-650-253-0000',
phoneUtil.formatByPattern(US_NUMBER,
PNF.RFC3966,
[newNumFormat]));
// $NP is set to '1' for the US. Here we check that for other NANPA countries
// the US rules are followed.
newNumFormat.setNationalPrefixFormattingRule('$NP ($FG)');
newNumFormat.setFormat('$1 $2-$3');
assertEquals('1 (242) 365-1234',
phoneUtil.formatByPattern(BS_NUMBER,
PNF.NATIONAL,
[newNumFormat]));
assertEquals('+1 242 365-1234',
phoneUtil.formatByPattern(BS_NUMBER,
PNF.INTERNATIONAL,
[newNumFormat]));
newNumFormat.setPattern('(\\d{2})(\\d{5})(\\d{3})');
newNumFormat.setFormat('$1-$2 $3');
assertEquals('02-36618 300',
phoneUtil.formatByPattern(IT_NUMBER,
PNF.NATIONAL,
[newNumFormat]));
assertEquals('+39 02-36618 300',
phoneUtil.formatByPattern(IT_NUMBER,
PNF.INTERNATIONAL,
[newNumFormat]));
newNumFormat.setNationalPrefixFormattingRule('$NP$FG');
newNumFormat.setPattern('(\\d{2})(\\d{4})(\\d{4})');
newNumFormat.setFormat('$1 $2 $3');
assertEquals('020 7031 3000',
phoneUtil.formatByPattern(GB_NUMBER,