Browse Source

000 Short Code incorrectly formatting for E164 and National.

mandlil_fix_formatting
mandlil 1 month ago
committed by GitHub
parent
commit
7151540336
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
4 changed files with 46 additions and 10 deletions
  1. +15
    -3
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  2. +8
    -1
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  3. +16
    -6
      javascript/i18n/phonenumbers/phonenumberutil.js
  4. +7
    -0
      javascript/i18n/phonenumbers/phonenumberutil_test.js

+ 15
- 3
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

@ -1289,10 +1289,22 @@ 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).
String rawInput = number.getRawInput();
if (rawInput.length() > 0 || !number.hasCountryCode()) {
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;
}
} else if (rawInput.length() > 0 || !number.hasCountryCode()) {
return rawInput;
}
}


+ 8
- 1
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -108,7 +108,10 @@ 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 US_SPOOF_WITH_RAW_INPUT =
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");
private static final PhoneNumber UZ_FIXED_LINE =
@ -507,6 +510,10 @@ 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));
PhoneNumber number = PhoneNumber.getDefaultInstance();
assertThat(phoneUtil.format(number, PhoneNumberFormat.NATIONAL)).isEmpty();
}
public void testFormatBSNumber() {


+ 16
- 6
javascript/i18n/phonenumbers/phonenumberutil.js View File

@ -1781,15 +1781,25 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.hasValidCountryCallingCode_ =
i18n.phonenumbers.PhoneNumberUtil.prototype.format =
function(number, numberFormat) {
if (number.getNationalNumber() == 0 && number.hasRawInput()) {
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).
// TODO: Consider removing the 'if' above so that unparseable strings
// without raw input format to the empty string instead of "+00"
/** @type {string} */
var rawInput = number.getRawInputOrDefault();
if (rawInput.length > 0) {
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;
}
} else if (rawInput.length > 0 || !number.hasCountryCode()) {
return rawInput;
}
}


+ 7
- 0
javascript/i18n/phonenumbers/phonenumberutil_test.js View File

@ -225,6 +225,11 @@ 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();
@ -674,6 +679,8 @@ 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 testFormatBSNumber() {


Loading…
Cancel
Save