Commits

jia....@ee073f10-1060-11df-b6a4-87a95322a99c  committed 0a0994e

Adding new functionality: formatUsingOriginalNumberFormat.

  • Participants
  • Parent commits ae82304
  • Branches default

Comments (0)

Files changed (2)

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

                                formattedExtension);
   }
 
+  /**
+   * Formats a phone number using the original phone number format that the number is parsed from.
+   * The original format is embedded in the country_code_source field of the PhoneNumber object
+   * passed in. If such information is missing, the number will be formatted into the NATIONAL
+   * format by default.
+   *
+   * @param number  The PhoneNumber that needs to be formatted in its original number format
+   * @param defaultCountry  the country whose IDD needs to be appended if the original number has
+   *                        one
+   * @return  The formatted phone number in its original number format
+   */
+  public String formatUsingOriginalNumberFormat(PhoneNumber number, String defaultCountry) {
+    if (!number.hasRawInput()) {
+      return format(number, PhoneNumberFormat.NATIONAL);
+    }
+    switch (number.getCountryCodeSource()) {
+      case FROM_DEFAULT_COUNTRY:
+        return format(number, PhoneNumberFormat.NATIONAL);
+      case FROM_NUMBER_WITH_PLUS_SIGN:
+        return format(number, PhoneNumberFormat.INTERNATIONAL);
+      case FROM_NUMBER_WITH_IDD:
+        return formatOutOfCountryCallingNumber(number, defaultCountry);
+      case FROM_NUMBER_WITHOUT_PLUS_SIGN:
+        return format(number, PhoneNumberFormat.INTERNATIONAL).substring(1);
+      default:
+        return number.getRawInput();
+    }
+  }
+
   
   /**
    * Gets the national significant number of the a phone number. Note a national significant number

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

                                   PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
   }
 
+  public void testFormatUsingOriginalNumberFormat() throws Exception {
+    PhoneNumber number1 = phoneUtil.parseAndKeepRawInput("+442087654321", "GB");
+    assertEquals("+44 20 8765 4321", phoneUtil.formatUsingOriginalNumberFormat(number1, "GB"));
+
+    PhoneNumber number2 = phoneUtil.parseAndKeepRawInput("02087654321", "GB");
+    assertEquals("(020) 8765 4321", phoneUtil.formatUsingOriginalNumberFormat(number2, "GB"));
+
+    PhoneNumber number3 = phoneUtil.parseAndKeepRawInput("011442087654321", "US");
+    assertEquals("011 44 20 8765 4321", phoneUtil.formatUsingOriginalNumberFormat(number3, "US"));
+
+    PhoneNumber number4 = phoneUtil.parseAndKeepRawInput("442087654321", "GB");
+    assertEquals("44 20 8765 4321", phoneUtil.formatUsingOriginalNumberFormat(number4, "GB"));
+
+    PhoneNumber number5 = phoneUtil.parse("+442087654321", "GB");
+    assertEquals("(020) 8765 4321", phoneUtil.formatUsingOriginalNumberFormat(number5, "GB"));
+  }
+
   public void testIsPremiumRate() {
     PhoneNumber premiumRateNumber1 =
         PhoneNumber.newBuilder().setCountryCode(1).setNationalNumber(9004433030L).build();