From 59bd2df076c57571b0e3db5a2c823281375b9ff7 Mon Sep 17 00:00:00 2001 From: lararennie Date: Thu, 26 Jan 2017 15:34:40 +0100 Subject: [PATCH] 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. --- .../i18n/phonenumbers/PhoneNumberUtil.java | 2 +- java/pending_code_changes.txt | 5 +++- .../i18n/phonenumbers/phonenumberutil.js | 30 ++++++++++++++----- .../i18n/phonenumbers/phonenumberutil_test.js | 11 +++++++ 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java index c652ad9c9..b03f068b7 100644 --- a/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +++ b/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java @@ -728,7 +728,7 @@ public class PhoneNumberUtil { * @param number a string of characters representing a 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 */); } diff --git a/java/pending_code_changes.txt b/java/pending_code_changes.txt index 8b1378917..646bd0afb 100644 --- a/java/pending_code_changes.txt +++ b/java/pending_code_changes.txt @@ -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. diff --git a/javascript/i18n/phonenumbers/phonenumberutil.js b/javascript/i18n/phonenumbers/phonenumberutil.js index f3727e86f..7b7b46c3d 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil.js +++ b/javascript/i18n/phonenumbers/phonenumberutil.js @@ -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 * keypad, but retains existing formatting. Also converts wide-ascii digits to @@ -1820,8 +1837,8 @@ i18n.phonenumbers.PhoneNumberUtil.prototype.formatNumberForMobileDialing = } return withFormatting ? 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) { /** @type {string} */ var normalizedFormattedNumber = - i18n.phonenumbers.PhoneNumberUtil.normalizeHelper_(formattedNumber, - i18n.phonenumbers.PhoneNumberUtil.DIALLABLE_CHAR_MAPPINGS_, - true /* remove non matches */); + i18n.phonenumbers.PhoneNumberUtil.normalizeDiallableCharsOnly( + formattedNumber); /** @type {string} */ 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) { formattedNumber = rawInput; } diff --git a/javascript/i18n/phonenumbers/phonenumberutil_test.js b/javascript/i18n/phonenumbers/phonenumberutil_test.js index c8d0e5c24..1e11bed93 100644 --- a/javascript/i18n/phonenumbers/phonenumberutil_test.js +++ b/javascript/i18n/phonenumbers/phonenumberutil_test.js @@ -546,6 +546,17 @@ function testNormaliseStripAlphaCharacters() { 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() { var PNF = i18n.phonenumbers.PhoneNumberFormat; assertEquals('650 253 0000',