|
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
3 |
|
4 // Don't modify this code. Please use: |
|
5 // https://github.com/andreasgal/PhoneNumber.js |
|
6 |
|
7 "use strict"; |
|
8 |
|
9 this.EXPORTED_SYMBOLS = ["PhoneNumberNormalizer"]; |
|
10 |
|
11 this.PhoneNumberNormalizer = (function() { |
|
12 const UNICODE_DIGITS = /[\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]/g; |
|
13 const VALID_ALPHA_PATTERN = /[a-zA-Z]/g; |
|
14 const LEADING_PLUS_CHARS_PATTERN = /^[+\uFF0B]+/g; |
|
15 const NON_DIALABLE_CHARS = /[^,#+\*\d]/g; |
|
16 |
|
17 // Map letters to numbers according to the ITU E.161 standard |
|
18 var E161 = { |
|
19 'a': 2, 'b': 2, 'c': 2, |
|
20 'd': 3, 'e': 3, 'f': 3, |
|
21 'g': 4, 'h': 4, 'i': 4, |
|
22 'j': 5, 'k': 5, 'l': 5, |
|
23 'm': 6, 'n': 6, 'o': 6, |
|
24 'p': 7, 'q': 7, 'r': 7, 's': 7, |
|
25 't': 8, 'u': 8, 'v': 8, |
|
26 'w': 9, 'x': 9, 'y': 9, 'z': 9 |
|
27 }; |
|
28 |
|
29 // Normalize a number by converting unicode numbers and symbols to their |
|
30 // ASCII equivalents and removing all non-dialable characters. |
|
31 function NormalizeNumber(number, numbersOnly) { |
|
32 if (typeof number !== 'string') { |
|
33 return ''; |
|
34 } |
|
35 |
|
36 number = number.replace(UNICODE_DIGITS, |
|
37 function (ch) { |
|
38 return String.fromCharCode(48 + (ch.charCodeAt(0) & 0xf)); |
|
39 }); |
|
40 if (!numbersOnly) { |
|
41 number = number.replace(VALID_ALPHA_PATTERN, |
|
42 function (ch) { |
|
43 return String(E161[ch.toLowerCase()] || 0); |
|
44 }); |
|
45 } |
|
46 number = number.replace(LEADING_PLUS_CHARS_PATTERN, "+"); |
|
47 number = number.replace(NON_DIALABLE_CHARS, ""); |
|
48 return number; |
|
49 }; |
|
50 |
|
51 |
|
52 return { |
|
53 Normalize: NormalizeNumber |
|
54 }; |
|
55 })(); |