Browse Source

Make normalizeDiallableCharsOnly public and port it to javascript (#1526)

* Make normalizeDiallableCharsOnly public and port it to javascript,
making use of it in the code in parallel places to the Java
implementation.
pull/1543/head
lararennie 9 years ago
committed by GitHub
parent
commit
59bd2df076
4 changed files with 38 additions and 10 deletions
  1. +1
    -1
      java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java
  2. +4
    -1
      java/pending_code_changes.txt
  3. +22
    -8
      javascript/i18n/phonenumbers/phonenumberutil.js
  4. +11
    -0
      javascript/i18n/phonenumbers/phonenumberutil_test.js

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

@ -728,7 +728,7 @@ public class PhoneNumberUtil {
* @param number a string of characters representing a phone number * @param number a string of characters representing a phone number
* @return the normalized string version of the phone number * @return the normalized string version of the phone number
*/ */
static String normalizeDiallableCharsOnly(String number) {
public static String normalizeDiallableCharsOnly(String number) {
return normalizeHelper(number, DIALLABLE_CHAR_MAPPINGS, true /* remove non matches */); return normalizeHelper(number, DIALLABLE_CHAR_MAPPINGS, true /* remove non matches */);
} }


+ 4
- 1
java/pending_code_changes.txt View File

@ -1 +1,4 @@
Code changes:
- Made normalizeDiallableCharsOnly() API in PhoneNumberUtil (Java) public. This
method is already public in the C++ implementation. It has also now been
added to the Javascript implementation.

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

@ -1095,6 +1095,23 @@ i18n.phonenumbers.PhoneNumberUtil.normalizeDigitsOnly = function(number) {
}; };
/**
* Normalizes a string of characters representing a phone number. This strips
* all characters which are not diallable on a mobile phone keypad (including
* all non-ASCII digits).
*
* @param {string} number a string of characters representing a phone number.
* @return {string} the normalized string version of the phone number.
*/
i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly =
function(number) {
return i18n.phonenumbers.PhoneNumberUtil.normalizeHelper_(number,
i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_,
true /* remove non matches */);
};
/** /**
* Converts all alpha characters in a number to their respective digits on a * Converts all alpha characters in a number to their respective digits on a
* keypad, but retains existing formatting. Also converts wide-ascii digits to * keypad, but retains existing formatting. Also converts wide-ascii digits to
@ -1820,8 +1837,8 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.formatNumberForMobileDialing =
} }
return withFormatting ? return withFormatting ?
formattedNumber : formattedNumber :
i18n.phonenumbers.PhoneNumberUtil.normalizeHelper_(formattedNumber,
i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_, true);
i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly(
formattedNumber);
}; };
@ -2058,14 +2075,11 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.formatInOriginalFormat =
if (formattedNumber != null && rawInput.length > 0) { if (formattedNumber != null && rawInput.length > 0) {
/** @type {string} */ /** @type {string} */
var normalizedFormattedNumber = var normalizedFormattedNumber =
i18n.phonenumbers.PhoneNumberUtil.normalizeHelper_(formattedNumber,
i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_,
true /* remove non matches */);
i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly(
formattedNumber);
/** @type {string} */ /** @type {string} */
var normalizedRawInput = var normalizedRawInput =
i18n.phonenumbers.PhoneNumberUtil.normalizeHelper_(rawInput,
i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_,
true /* remove non matches */);
i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly(rawInput);
if (normalizedFormattedNumber != normalizedRawInput) { if (normalizedFormattedNumber != normalizedRawInput) {
formattedNumber = rawInput; formattedNumber = rawInput;
} }


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

@ -546,6 +546,17 @@ function testNormaliseStripAlphaCharacters() {
i18n.phonenumbers.PhoneNumberUtil.normalizeDigitsOnly(inputNumber)); i18n.phonenumbers.PhoneNumberUtil.normalizeDigitsOnly(inputNumber));
} }
function testNormaliseStripNonDiallableCharacters() {
/** @type {string} */
var inputNumber = '03*4-56&+1a#234';
/** @type {string} */
var expectedOutput = '03*456+1#234';
assertEquals('Conversion did not correctly remove non-diallable characters',
expectedOutput,
i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly(
inputNumber));
}
function testFormatUSNumber() { function testFormatUSNumber() {
var PNF = i18n.phonenumbers.PhoneNumberFormat; var PNF = i18n.phonenumbers.PhoneNumberFormat;
assertEquals('650 253 0000', assertEquals('650 253 0000',


Loading…
Cancel
Save