Browse Source

Fixed, 000 Short Code formatting for E164

mandlil_fix_formatting
mandlil 4 weeks ago
committed by GitHub
parent
commit
c3625a576a
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 43 deletions
  1. +11
    -15
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  2. +13
    -4
      java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java
  3. +12
    -17
      javascript/i18n/phonenumbers/phonenumberutil.js
  4. +12
    -7
      javascript/i18n/phonenumbers/phonenumberutil_test.js

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

@ -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;
}


+ 13
- 4
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

@ -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() {


+ 12
- 17
javascript/i18n/phonenumbers/phonenumberutil.js View File

@ -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;
}


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

@ -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() {


Loading…
Cancel
Save