From c3625a576aba0686240b525fa2a21357c4be5486 Mon Sep 17 00:00:00 2001 From: mandlil <138015259+mandlil@users.noreply.github.com> Date: Fri, 14 Nov 2025 03:33:45 +0000 Subject: [PATCH] Fixed, 000 Short Code formatting for E164 --- .../i18n/phonenumbers/PhoneNumberUtil.java | 26 +++++++---------- .../phonenumbers/PhoneNumberUtilTest.java | 17 ++++++++--- .../i18n/phonenumbers/phonenumberutil.js | 29 ++++++++----------- .../i18n/phonenumbers/phonenumberutil_test.js | 19 +++++++----- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index 0622d3760..22727d2a9 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -1288,22 +1288,18 @@ public class PhoneNumberUtil { */ public String format(PhoneNumber number, PhoneNumberFormat numberFormat) { if (number.getNationalNumber() == 0) { - // Unparseable numbers that kept their raw input just use that. + // 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()) { - String rawInputwithoutCountryCode = rawInput; - // The country calling code must be stripped from the rawInput if it starts with '+'. - // This is necessary because the national formatting function will add the country calling - // code by default." - if (rawInput.startsWith("+")) { - int countryCallingCode = String.valueOf(number.getCountryCode()).length(); - rawInputwithoutCountryCode = rawInput.substring(countryCallingCode + 1); - } - if (numberFormat == PhoneNumberFormat.NATIONAL) { - return rawInputwithoutCountryCode; - } else { - return "+" + number.getCountryCode() + rawInputwithoutCountryCode; - } + 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 75d3e2d61..0bfbfb859 100644 --- a/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java +++ b/java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java @@ -108,9 +108,6 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { new PhoneNumber().setCountryCode(1).setNationalNumber(8002530000L); private static final PhoneNumber US_SPOOF = new PhoneNumber().setCountryCode(1).setNationalNumber(0L); - private static final PhoneNumber AU_SHORT_CODE_WITH_RAW_INPUT = - new PhoneNumber().setCountryCode(61).setNationalNumber(0L) - .setRawInput("000"); private static final PhoneNumber US_SPOOF_WITH_RAW_INPUT = new PhoneNumber().setCountryCode(1).setNationalNumber(0L) .setRawInput("000-000-0000"); @@ -510,7 +507,19 @@ public class PhoneNumberUtilTest extends TestMetadataTestCase { assertEquals("000-000-0000", phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PhoneNumberFormat.NATIONAL)); assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL)); - assertEquals("+61000", phoneUtil.format(AU_SHORT_CODE_WITH_RAW_INPUT, PhoneNumberFormat.E164)); + } + + public void testFormatAUShortCodeNumber() throws Exception { + PhoneNumber auShortCodeNumber = phoneUtil.parse("000", RegionCode.AU); + assertEquals("+61000", phoneUtil.format(auShortCodeNumber, PhoneNumberFormat.E164)); + + PhoneNumber pgShortCodeNumber = + PhoneNumber.newBuilder() + .setCountryCode(675) + .setNationalNumber(0L) + .setRawInput("+675000") + .build(); + assertEquals("+675000", phoneUtil.format(pgShortCodeNumber, PhoneNumberFormat.E164)); } public void testFormatBSNumber() { diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index 0df9b6117..3db9dbc10 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -1781,24 +1781,19 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.hasValidCountryCallingCode_ = i18n.phonenumbers.PhoneNumberUtil.prototype.format = function(number, numberFormat) { - if (number.getNationalNumber() == 0 ) { - // Unparseable numbers that kept their raw input just use that. + 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 && number.hasCountryCode()) { - /** @type {string} */ - let rawInputwithoutCountryCode = rawInput; - //The country calling code must be stripped from the rawInput if it starts with '+'. - //This is necessary because the national formatting function will add the country calling code by default." - if (rawInput.startsWith('+')) { - const countryCallingCode = String.valueOf(number.getCountryCode()).length(); - rawInputwithoutCountryCode = rawInput.substring(countryCallingCode + 1); - } - if (numberFormat == i18n.phonenumbers.PhoneNumberFormat.NATIONAL) { - return rawInputwithoutCountryCode; - } else { - return '+' + number.getCountryCode() + rawInputwithoutCountryCode; - } + 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.getCountryCode(); + let formattedNumber = new StringBuilder(rawInput); + this.prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber); + return formattedNumber.toString(); } 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 a26bea618..f146d7be5 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil_test.js +++ b/javascript/i18n/phonenumbers/phonenumberutil_test.js @@ -225,11 +225,6 @@ 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 AU_SHORT_CODE_WITH_RAW_INPUT = new i18n.phonenumbers.PhoneNumber(); -AU_SHORT_CODE_WITH_RAW_INPUT.setCountryCode(61); -AU_SHORT_CODE_WITH_RAW_INPUT.setNationalNumber(0); -AU_SHORT_CODE_WITH_RAW_INPUT.setRawInput('000'); /** @type {!i18n.phonenumbers.PhoneNumber} */ var UZ_FIXED_LINE = new i18n.phonenumbers.PhoneNumber(); @@ -679,8 +674,18 @@ function testFormatUSNumber() { assertEquals( '000-000-0000', phoneUtil.format(US_SPOOF_WITH_RAW_INPUT, PNF.NATIONAL)); assertEquals('0', phoneUtil.format(US_SPOOF, PNF.NATIONAL)); - assertEquals('+61000', phoneUtil.format(AU_SHORT_CODE_WITH_RAW_INPUT, PNF.E164)); - assertEquals('', phoneUtil.format(new i18n.phonenumbers.PhoneNumber(), 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() {