UNPKG

3.34 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.parseDigit = parseDigit;
7exports["default"] = parseDigits;
8exports.DIGITS = void 0;
9// These mappings map a character (key) to a specific digit that should
10// replace it for normalization purposes. Non-European digits that
11// may be used in phone numbers are mapped to a European equivalent.
12//
13// E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
14//
15var DIGITS = {
16 '0': '0',
17 '1': '1',
18 '2': '2',
19 '3': '3',
20 '4': '4',
21 '5': '5',
22 '6': '6',
23 '7': '7',
24 '8': '8',
25 '9': '9',
26 "\uFF10": '0',
27 // Fullwidth digit 0
28 "\uFF11": '1',
29 // Fullwidth digit 1
30 "\uFF12": '2',
31 // Fullwidth digit 2
32 "\uFF13": '3',
33 // Fullwidth digit 3
34 "\uFF14": '4',
35 // Fullwidth digit 4
36 "\uFF15": '5',
37 // Fullwidth digit 5
38 "\uFF16": '6',
39 // Fullwidth digit 6
40 "\uFF17": '7',
41 // Fullwidth digit 7
42 "\uFF18": '8',
43 // Fullwidth digit 8
44 "\uFF19": '9',
45 // Fullwidth digit 9
46 "\u0660": '0',
47 // Arabic-indic digit 0
48 "\u0661": '1',
49 // Arabic-indic digit 1
50 "\u0662": '2',
51 // Arabic-indic digit 2
52 "\u0663": '3',
53 // Arabic-indic digit 3
54 "\u0664": '4',
55 // Arabic-indic digit 4
56 "\u0665": '5',
57 // Arabic-indic digit 5
58 "\u0666": '6',
59 // Arabic-indic digit 6
60 "\u0667": '7',
61 // Arabic-indic digit 7
62 "\u0668": '8',
63 // Arabic-indic digit 8
64 "\u0669": '9',
65 // Arabic-indic digit 9
66 "\u06F0": '0',
67 // Eastern-Arabic digit 0
68 "\u06F1": '1',
69 // Eastern-Arabic digit 1
70 "\u06F2": '2',
71 // Eastern-Arabic digit 2
72 "\u06F3": '3',
73 // Eastern-Arabic digit 3
74 "\u06F4": '4',
75 // Eastern-Arabic digit 4
76 "\u06F5": '5',
77 // Eastern-Arabic digit 5
78 "\u06F6": '6',
79 // Eastern-Arabic digit 6
80 "\u06F7": '7',
81 // Eastern-Arabic digit 7
82 "\u06F8": '8',
83 // Eastern-Arabic digit 8
84 "\u06F9": '9' // Eastern-Arabic digit 9
85
86};
87exports.DIGITS = DIGITS;
88
89function parseDigit(character) {
90 return DIGITS[character];
91}
92/**
93 * Parses phone number digits from a string.
94 * Drops all punctuation leaving only digits.
95 * Also converts wide-ascii and arabic-indic numerals to conventional numerals.
96 * E.g. in Iraq they don't write `+442323234` but rather `+٤٤٢٣٢٣٢٣٤`.
97 * @param {string} string
98 * @return {string}
99 * @example
100 * ```js
101 * parseDigits('8 (800) 555')
102 * // Outputs '8800555'.
103 * ```
104 */
105
106
107function parseDigits(string) {
108 var result = ''; // Using `.split('')` here instead of normal `for ... of`
109 // because the importing application doesn't neccessarily include an ES6 polyfill.
110 // The `.split('')` approach discards "exotic" UTF-8 characters
111 // (the ones consisting of four bytes) but digits
112 // (including non-European ones) don't fall into that range
113 // so such "exotic" characters would be discarded anyway.
114
115 for (var _iterator = string.split(''), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
116 var _ref;
117
118 if (_isArray) {
119 if (_i >= _iterator.length) break;
120 _ref = _iterator[_i++];
121 } else {
122 _i = _iterator.next();
123 if (_i.done) break;
124 _ref = _i.value;
125 }
126
127 var character = _ref;
128 var digit = parseDigit(character);
129
130 if (digit) {
131 result += digit;
132 }
133 }
134
135 return result;
136}
137//# sourceMappingURL=parseDigits.js.map
\No newline at end of file