Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1288,11 +1288,19 @@ private boolean hasValidCountryCallingCode(int countryCallingCode) {
*/
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,16 @@ public void testFormatUSNumber() {
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));
Expand Down
20 changes: 12 additions & 8 deletions javascript/i18n/phonenumbers/phonenumberutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions javascript/i18n/phonenumbers/phonenumberutil_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading