diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 6d8004f18..22727d2a9 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -1288,11 +1288,19 @@ public class PhoneNumberUtil { */ public String format(PhoneNumber number, PhoneNumberFormat numberFormat) { if (number.getNationalNumber() == 0) { - // Unparseable numbers that kept their raw input just use that. - // This is the only case where a number can be formatted as E164 without a - // leading '+' symbol (but the original number wasn't parseable anyway). + // Unparseable numbers that kept their raw input just use that, unless default country was + // specified and the format is E164. In that case, we prepend the raw input with the country + // code String rawInput = number.getRawInput(); - if (rawInput.length() > 0 || !number.hasCountryCode()) { + if (rawInput.length() > 0 + && number.hasCountryCode() + && number.getCountryCodeSource() == CountryCodeSource.FROM_DEFAULT_COUNTRY + && numberFormat == PhoneNumberFormat.E164) { + int countryCallingCode = number.getCountryCode(); + StringBuilder formattedNumber = new StringBuilder(rawInput); + prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber); + return formattedNumber.toString(); + } else if (rawInput.length() > 0 || !number.hasCountryCode()) { return rawInput; } } diff --git a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java index 2b9345c4c..4da210ec1 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -509,6 +509,16 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL)); } + public void testFormatAUShortCodeNumber() throws Exception { + PhoneNumber auShortCodeNumber = phoneUtil.parse("000", RegionCode.AU); + assertEquals("+61000", phoneUtil.format(auShortCodeNumber, PhoneNumberFormat.E164)); + + PhoneNumber pgShortCodeNumber = + new PhoneNumber().setCountryCode(675).setNationalNumber(0L) + .setRawInput("+675000"); + assertEquals("+675000", phoneUtil.format(pgShortCodeNumber, PhoneNumberFormat.E164)); + } + public void testFormatBSNumber() { assertEquals("242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.NATIONAL)); assertEquals("+1 242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL)); diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index 8812d3aa4..db90566a4 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -1781,15 +1781,19 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.hasValidCountryCallingCode_ = i18n.phonenumbers.PhoneNumberUtil.prototype.format = function(number, numberFormat) { - if (number.getNationalNumber() == 0 && number.hasRawInput()) { - // Unparseable numbers that kept their raw input just use that. - // This is the only case where a number can be formatted as E164 without a - // leading '+' symbol (but the original number wasn't parseable anyway). - // TODO: Consider removing the 'if' above so that unparseable strings - // without raw input format to the empty string instead of "+00" + if (number.getNationalNumber() == 0 && number.hasRawInput()) { + // Unparseable numbers that kept their raw input just use that, unless + // default country was specified and the format is E164. In that case, we + // prepend the raw input with the country code. /** @type {string} */ - var rawInput = number.getRawInputOrDefault(); - if (rawInput.length > 0) { + const rawInput = number.getRawInputOrDefault(); + if (rawInput.length > 0 && number.hasCountryCode() + && number.getCountryCodeSource() == i18n.phonenumbers.PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY + && numberFormat == i18n.phonenumbers.PhoneNumberFormat.E164) { + const countryCallingCode = number.getCountryCodeOrDefault(); + let formattedNumber =rawInput; + return this.prefixNumberWithCountryCallingCode_(countryCallingCode, numberFormat, formattedNumber,''); + } else if (rawInput.length > 0 || !number.hasCountryCode()) { return rawInput; } } diff --git a/javascript/i18n/phonenumbers/phonenumberutil_test.js b/javascript/i18n/phonenumbers/phonenumberutil_test.js index 4070dd08a..f146d7be5 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil_test.js +++ b/javascript/i18n/phonenumbers/phonenumberutil_test.js @@ -676,6 +676,18 @@ function testFormatUSNumber() { assertEquals('0', phoneUtil.format(US_SPOOF, PNF.NATIONAL)); } +function testFormatAUShortCodeNumber() { + const auShortCodeNumber = phoneUtil.parse("000", RegionCode.AU); + const PNF = i18n.phonenumbers.PhoneNumberFormat; + assertEquals('+61000', phoneUtil.format(auShortCodeNumber, PNF.E164)); + + const pgShortCodeNumber = new i18n.phonenumbers.PhoneNumber(); + pgShortCodeNumber.setCountryCode(675); + pgShortCodeNumber.setNationalNumber(0); + pgShortCodeNumber.setRawInput('+675000'); + assertEquals('+675000', phoneUtil.format(pgShortCodeNumber, PNF.E164)); +} + function testFormatBSNumber() { var PNF = i18n.phonenumbers.PhoneNumberFormat; assertEquals('242 365 1234', phoneUtil.format(BS_NUMBER, PNF.NATIONAL));