Browse Source

Merge 97ecea6769 into cf446565d9

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

+ 12
- 4
java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java View File

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


+ 10
- 0
java/libphonenumber/test/com/google/i18n/phonenumbers/PhoneNumberUtilTest.java View File

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


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

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


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

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


Loading…
Cancel
Save