Skip to content

Commit 79ad015

Browse files
authored
000 Short Code incorrectly formatting for E164 (#3937)
* 000 Short Code incorrectly formatting for E164
1 parent bc83c0c commit 79ad015

4 files changed

Lines changed: 46 additions & 12 deletions

File tree

java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,11 +1288,19 @@ private boolean hasValidCountryCallingCode(int countryCallingCode) {
12881288
*/
12891289
public String format(PhoneNumber number, PhoneNumberFormat numberFormat) {
12901290
if (number.getNationalNumber() == 0) {
1291-
// Unparseable numbers that kept their raw input just use that.
1292-
// This is the only case where a number can be formatted as E164 without a
1293-
// leading '+' symbol (but the original number wasn't parseable anyway).
1291+
// Unparseable numbers that kept their raw input just use that, unless default country was
1292+
// specified and the format is E164. In that case, we prepend the raw input with the country
1293+
// code
12941294
String rawInput = number.getRawInput();
1295-
if (rawInput.length() > 0 || !number.hasCountryCode()) {
1295+
if (rawInput.length() > 0
1296+
&& number.hasCountryCode()
1297+
&& number.getCountryCodeSource() == CountryCodeSource.FROM_DEFAULT_COUNTRY
1298+
&& numberFormat == PhoneNumberFormat.E164) {
1299+
int countryCallingCode = number.getCountryCode();
1300+
StringBuilder formattedNumber = new StringBuilder(rawInput);
1301+
prefixNumberWithCountryCallingCode(countryCallingCode, numberFormat, formattedNumber);
1302+
return formattedNumber.toString();
1303+
} else if (rawInput.length() > 0 || !number.hasCountryCode()) {
12961304
return rawInput;
12971305
}
12981306
}

java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,16 @@ public void testFormatUSNumber() {
509509
assertEquals("0", phoneUtil.format(US_SPOOF, PhoneNumberFormat.NATIONAL));
510510
}
511511

512+
public void testFormatAUShortCodeNumber() throws Exception {
513+
PhoneNumber auShortCodeNumber = phoneUtil.parse("000", RegionCode.AU);
514+
assertEquals("+61000", phoneUtil.format(auShortCodeNumber, PhoneNumberFormat.E164));
515+
516+
PhoneNumber pgShortCodeNumber =
517+
new PhoneNumber().setCountryCode(675).setNationalNumber(0L)
518+
.setRawInput("+675000");
519+
assertEquals("+675000", phoneUtil.format(pgShortCodeNumber, PhoneNumberFormat.E164));
520+
}
521+
512522
public void testFormatBSNumber() {
513523
assertEquals("242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.NATIONAL));
514524
assertEquals("+1 242 365 1234", phoneUtil.format(BS_NUMBER, PhoneNumberFormat.INTERNATIONAL));

javascript/i18n/phonenumbers/phonenumberutil.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,15 +1781,19 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.hasValidCountryCallingCode_ =
17811781
i18n.phonenumbers.PhoneNumberUtil.prototype.format =
17821782
function(number, numberFormat) {
17831783

1784-
if (number.getNationalNumber() == 0 && number.hasRawInput()) {
1785-
// Unparseable numbers that kept their raw input just use that.
1786-
// This is the only case where a number can be formatted as E164 without a
1787-
// leading '+' symbol (but the original number wasn't parseable anyway).
1788-
// TODO: Consider removing the 'if' above so that unparseable strings
1789-
// without raw input format to the empty string instead of "+00"
1784+
if (number.getNationalNumber() == 0 && number.hasRawInput()) {
1785+
// Unparseable numbers that kept their raw input just use that, unless
1786+
// default country was specified and the format is E164. In that case, we
1787+
// prepend the raw input with the country code.
17901788
/** @type {string} */
1791-
var rawInput = number.getRawInputOrDefault();
1792-
if (rawInput.length > 0) {
1789+
const rawInput = number.getRawInputOrDefault();
1790+
if (rawInput.length > 0 && number.hasCountryCode()
1791+
&& number.getCountryCodeSource() == i18n.phonenumbers.PhoneNumber.CountryCodeSource.FROM_DEFAULT_COUNTRY
1792+
&& numberFormat == i18n.phonenumbers.PhoneNumberFormat.E164) {
1793+
const countryCallingCode = number.getCountryCodeOrDefault();
1794+
let formattedNumber =rawInput;
1795+
return this.prefixNumberWithCountryCallingCode_(countryCallingCode, numberFormat, formattedNumber,'');
1796+
} else if (rawInput.length > 0 || !number.hasCountryCode()) {
17931797
return rawInput;
17941798
}
17951799
}

javascript/i18n/phonenumbers/phonenumberutil_test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,18 @@ function testFormatUSNumber() {
676676
assertEquals('0', phoneUtil.format(US_SPOOF, PNF.NATIONAL));
677677
}
678678

679+
function testFormatAUShortCodeNumber() {
680+
const auShortCodeNumber = phoneUtil.parse("000", RegionCode.AU);
681+
const PNF = i18n.phonenumbers.PhoneNumberFormat;
682+
assertEquals('+61000', phoneUtil.format(auShortCodeNumber, PNF.E164));
683+
684+
const pgShortCodeNumber = new i18n.phonenumbers.PhoneNumber();
685+
pgShortCodeNumber.setCountryCode(675);
686+
pgShortCodeNumber.setNationalNumber(0);
687+
pgShortCodeNumber.setRawInput('+675000');
688+
assertEquals('+675000', phoneUtil.format(pgShortCodeNumber, PNF.E164));
689+
}
690+
679691
function testFormatBSNumber() {
680692
var PNF = i18n.phonenumbers.PhoneNumberFormat;
681693
assertEquals('242 365 1234', phoneUtil.format(BS_NUMBER, PNF.NATIONAL));

0 commit comments

Comments
 (0)