michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: michael@0: // Don't modify this code. Please use: michael@0: // https://github.com/andreasgal/PhoneNumber.js michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["PhoneNumberNormalizer"]; michael@0: michael@0: this.PhoneNumberNormalizer = (function() { michael@0: const UNICODE_DIGITS = /[\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]/g; michael@0: const VALID_ALPHA_PATTERN = /[a-zA-Z]/g; michael@0: const LEADING_PLUS_CHARS_PATTERN = /^[+\uFF0B]+/g; michael@0: const NON_DIALABLE_CHARS = /[^,#+\*\d]/g; michael@0: michael@0: // Map letters to numbers according to the ITU E.161 standard michael@0: var E161 = { michael@0: 'a': 2, 'b': 2, 'c': 2, michael@0: 'd': 3, 'e': 3, 'f': 3, michael@0: 'g': 4, 'h': 4, 'i': 4, michael@0: 'j': 5, 'k': 5, 'l': 5, michael@0: 'm': 6, 'n': 6, 'o': 6, michael@0: 'p': 7, 'q': 7, 'r': 7, 's': 7, michael@0: 't': 8, 'u': 8, 'v': 8, michael@0: 'w': 9, 'x': 9, 'y': 9, 'z': 9 michael@0: }; michael@0: michael@0: // Normalize a number by converting unicode numbers and symbols to their michael@0: // ASCII equivalents and removing all non-dialable characters. michael@0: function NormalizeNumber(number, numbersOnly) { michael@0: if (typeof number !== 'string') { michael@0: return ''; michael@0: } michael@0: michael@0: number = number.replace(UNICODE_DIGITS, michael@0: function (ch) { michael@0: return String.fromCharCode(48 + (ch.charCodeAt(0) & 0xf)); michael@0: }); michael@0: if (!numbersOnly) { michael@0: number = number.replace(VALID_ALPHA_PATTERN, michael@0: function (ch) { michael@0: return String(E161[ch.toLowerCase()] || 0); michael@0: }); michael@0: } michael@0: number = number.replace(LEADING_PLUS_CHARS_PATTERN, "+"); michael@0: number = number.replace(NON_DIALABLE_CHARS, ""); michael@0: return number; michael@0: }; michael@0: michael@0: michael@0: return { michael@0: Normalize: NormalizeNumber michael@0: }; michael@0: })();