1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.extractFormattedDigitsAndPlus = extractFormattedDigitsAndPlus;
|
7 | exports["default"] = void 0;
|
8 |
|
9 | var _extractCountryCallingCode2 = _interopRequireDefault(require("./helpers/extractCountryCallingCode"));
|
10 |
|
11 | var _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = _interopRequireDefault(require("./helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign"));
|
12 |
|
13 | var _extractNationalNumberFromPossiblyIncompleteNumber = _interopRequireDefault(require("./helpers/extractNationalNumberFromPossiblyIncompleteNumber"));
|
14 |
|
15 | var _stripIddPrefix = _interopRequireDefault(require("./helpers/stripIddPrefix"));
|
16 |
|
17 | var _parseDigits = _interopRequireDefault(require("./helpers/parseDigits"));
|
18 |
|
19 | var _constants = require("./constants");
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
22 |
|
23 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
|
24 |
|
25 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
|
26 |
|
27 | function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
28 |
|
29 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
34 |
|
35 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
36 |
|
37 | var VALID_FORMATTED_PHONE_NUMBER_PART = '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+';
|
38 | var VALID_FORMATTED_PHONE_NUMBER_PART_PATTERN = new RegExp('^' + VALID_FORMATTED_PHONE_NUMBER_PART + '$', 'i');
|
39 | var VALID_PHONE_NUMBER = '(?:' + '[' + _constants.PLUS_CHARS + ']' + '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']*' + '|' + '[' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+' + ')';
|
40 | var AFTER_PHONE_NUMBER_DIGITS_END_PATTERN = new RegExp('[^' + _constants.VALID_PUNCTUATION + _constants.VALID_DIGITS + ']+' + '.*' + '$');
|
41 |
|
42 |
|
43 |
|
44 | var COMPLEX_NATIONAL_PREFIX = /[^\d\[\]]/;
|
45 |
|
46 | var AsYouTypeParser =
|
47 |
|
48 | function () {
|
49 | function AsYouTypeParser(_ref) {
|
50 | var defaultCountry = _ref.defaultCountry,
|
51 | defaultCallingCode = _ref.defaultCallingCode,
|
52 | metadata = _ref.metadata,
|
53 | onNationalSignificantNumberChange = _ref.onNationalSignificantNumberChange;
|
54 |
|
55 | _classCallCheck(this, AsYouTypeParser);
|
56 |
|
57 | this.defaultCountry = defaultCountry;
|
58 | this.defaultCallingCode = defaultCallingCode;
|
59 | this.metadata = metadata;
|
60 | this.onNationalSignificantNumberChange = onNationalSignificantNumberChange;
|
61 | }
|
62 |
|
63 | _createClass(AsYouTypeParser, [{
|
64 | key: "input",
|
65 | value: function input(text, state) {
|
66 | var _extractFormattedDigi = extractFormattedDigitsAndPlus(text),
|
67 | _extractFormattedDigi2 = _slicedToArray(_extractFormattedDigi, 2),
|
68 | formattedDigits = _extractFormattedDigi2[0],
|
69 | hasPlus = _extractFormattedDigi2[1];
|
70 |
|
71 | var digits = (0, _parseDigits["default"])(formattedDigits);
|
72 |
|
73 | var justLeadingPlus;
|
74 |
|
75 | if (hasPlus) {
|
76 | if (!state.digits) {
|
77 | state.startInternationalNumber();
|
78 |
|
79 | if (!digits) {
|
80 | justLeadingPlus = true;
|
81 | }
|
82 | }
|
83 | }
|
84 |
|
85 | if (digits) {
|
86 | this.inputDigits(digits, state);
|
87 | }
|
88 |
|
89 | return {
|
90 | digits: digits,
|
91 | justLeadingPlus: justLeadingPlus
|
92 | };
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | }, {
|
101 | key: "inputDigits",
|
102 | value: function inputDigits(nextDigits, state) {
|
103 | var digits = state.digits;
|
104 | var hasReceivedThreeLeadingDigits = digits.length < 3 && digits.length + nextDigits.length >= 3;
|
105 |
|
106 | state.appendDigits(nextDigits);
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | if (hasReceivedThreeLeadingDigits) {
|
122 | this.extractIddPrefix(state);
|
123 | }
|
124 |
|
125 | if (this.isWaitingForCountryCallingCode(state)) {
|
126 | if (!this.extractCountryCallingCode(state)) {
|
127 | return;
|
128 | }
|
129 | } else {
|
130 | state.appendNationalSignificantNumberDigits(nextDigits);
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | if (!state.international) {
|
139 | if (!this.hasExtractedNationalSignificantNumber) {
|
140 | this.extractNationalSignificantNumber(state.getNationalDigits(), state.update);
|
141 | }
|
142 | }
|
143 | }
|
144 | }, {
|
145 | key: "isWaitingForCountryCallingCode",
|
146 | value: function isWaitingForCountryCallingCode(_ref2) {
|
147 | var international = _ref2.international,
|
148 | callingCode = _ref2.callingCode;
|
149 | return international && !callingCode;
|
150 | }
|
151 |
|
152 |
|
153 | }, {
|
154 | key: "extractCountryCallingCode",
|
155 | value: function extractCountryCallingCode(state) {
|
156 | var _extractCountryCallin = (0, _extractCountryCallingCode2["default"])('+' + state.getDigitsWithoutInternationalPrefix(), this.defaultCountry, this.defaultCallingCode, this.metadata.metadata),
|
157 | countryCallingCode = _extractCountryCallin.countryCallingCode,
|
158 | number = _extractCountryCallin.number;
|
159 |
|
160 | if (countryCallingCode) {
|
161 | state.setCallingCode(countryCallingCode);
|
162 | state.update({
|
163 | nationalSignificantNumber: number
|
164 | });
|
165 | return true;
|
166 | }
|
167 | }
|
168 | }, {
|
169 | key: "reset",
|
170 | value: function reset(numberingPlan) {
|
171 | if (numberingPlan) {
|
172 | this.hasSelectedNumberingPlan = true;
|
173 |
|
174 | var nationalPrefixForParsing = numberingPlan._nationalPrefixForParsing();
|
175 |
|
176 | this.couldPossiblyExtractAnotherNationalSignificantNumber = nationalPrefixForParsing && COMPLEX_NATIONAL_PREFIX.test(nationalPrefixForParsing);
|
177 | } else {
|
178 | this.hasSelectedNumberingPlan = undefined;
|
179 | this.couldPossiblyExtractAnotherNationalSignificantNumber = undefined;
|
180 | }
|
181 | }
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | }, {
|
191 | key: "extractNationalSignificantNumber",
|
192 | value: function extractNationalSignificantNumber(nationalDigits, setState) {
|
193 | if (!this.hasSelectedNumberingPlan) {
|
194 | return;
|
195 | }
|
196 |
|
197 | var _extractNationalNumbe = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(nationalDigits, this.metadata),
|
198 | nationalPrefix = _extractNationalNumbe.nationalPrefix,
|
199 | nationalNumber = _extractNationalNumbe.nationalNumber,
|
200 | carrierCode = _extractNationalNumbe.carrierCode;
|
201 |
|
202 | if (nationalNumber === nationalDigits) {
|
203 | return;
|
204 | }
|
205 |
|
206 | this.onExtractedNationalNumber(nationalPrefix, carrierCode, nationalNumber, nationalDigits, setState);
|
207 | return true;
|
208 | }
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | }, {
|
216 | key: "extractAnotherNationalSignificantNumber",
|
217 | value: function extractAnotherNationalSignificantNumber(nationalDigits, prevNationalSignificantNumber, setState) {
|
218 | if (!this.hasExtractedNationalSignificantNumber) {
|
219 | return this.extractNationalSignificantNumber(nationalDigits, setState);
|
220 | }
|
221 |
|
222 | if (!this.couldPossiblyExtractAnotherNationalSignificantNumber) {
|
223 | return;
|
224 | }
|
225 |
|
226 | var _extractNationalNumbe2 = (0, _extractNationalNumberFromPossiblyIncompleteNumber["default"])(nationalDigits, this.metadata),
|
227 | nationalPrefix = _extractNationalNumbe2.nationalPrefix,
|
228 | nationalNumber = _extractNationalNumbe2.nationalNumber,
|
229 | carrierCode = _extractNationalNumbe2.carrierCode;
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 | if (nationalNumber === prevNationalSignificantNumber) {
|
239 | return;
|
240 | }
|
241 |
|
242 | this.onExtractedNationalNumber(nationalPrefix, carrierCode, nationalNumber, nationalDigits, setState);
|
243 | return true;
|
244 | }
|
245 | }, {
|
246 | key: "onExtractedNationalNumber",
|
247 | value: function onExtractedNationalNumber(nationalPrefix, carrierCode, nationalSignificantNumber, nationalDigits, setState) {
|
248 | var complexPrefixBeforeNationalSignificantNumber;
|
249 | var nationalSignificantNumberMatchesInput;
|
250 |
|
251 | var nationalSignificantNumberIndex = nationalDigits.lastIndexOf(nationalSignificantNumber);
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | if (nationalSignificantNumberIndex >= 0 && nationalSignificantNumberIndex === nationalDigits.length - nationalSignificantNumber.length) {
|
258 | nationalSignificantNumberMatchesInput = true;
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 | var prefixBeforeNationalNumber = nationalDigits.slice(0, nationalSignificantNumberIndex);
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | if (prefixBeforeNationalNumber !== nationalPrefix) {
|
271 | complexPrefixBeforeNationalSignificantNumber = prefixBeforeNationalNumber;
|
272 | }
|
273 | }
|
274 |
|
275 | setState({
|
276 | nationalPrefix: nationalPrefix,
|
277 | carrierCode: carrierCode,
|
278 | nationalSignificantNumber: nationalSignificantNumber,
|
279 | nationalSignificantNumberMatchesInput: nationalSignificantNumberMatchesInput,
|
280 | complexPrefixBeforeNationalSignificantNumber: complexPrefixBeforeNationalSignificantNumber
|
281 | });
|
282 |
|
283 |
|
284 | this.hasExtractedNationalSignificantNumber = true;
|
285 | this.onNationalSignificantNumberChange();
|
286 | }
|
287 | }, {
|
288 | key: "reExtractNationalSignificantNumber",
|
289 | value: function reExtractNationalSignificantNumber(state) {
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | if (this.extractAnotherNationalSignificantNumber(state.getNationalDigits(), state.nationalSignificantNumber, state.update)) {
|
324 | return true;
|
325 | }
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 | if (this.extractIddPrefix(state)) {
|
342 | this.extractCallingCodeAndNationalSignificantNumber(state);
|
343 | return true;
|
344 | }
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | if (this.fixMissingPlus(state)) {
|
354 | this.extractCallingCodeAndNationalSignificantNumber(state);
|
355 | return true;
|
356 | }
|
357 | }
|
358 | }, {
|
359 | key: "extractIddPrefix",
|
360 | value: function extractIddPrefix(state) {
|
361 |
|
362 |
|
363 | var international = state.international,
|
364 | IDDPrefix = state.IDDPrefix,
|
365 | digits = state.digits,
|
366 | nationalSignificantNumber = state.nationalSignificantNumber;
|
367 |
|
368 | if (international || IDDPrefix) {
|
369 | return;
|
370 | }
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | var numberWithoutIDD = (0, _stripIddPrefix["default"])(digits, this.defaultCountry, this.defaultCallingCode, this.metadata.metadata);
|
377 |
|
378 | if (numberWithoutIDD !== undefined && numberWithoutIDD !== digits) {
|
379 |
|
380 |
|
381 | state.update({
|
382 | IDDPrefix: digits.slice(0, digits.length - numberWithoutIDD.length)
|
383 | });
|
384 | this.startInternationalNumber(state);
|
385 | return true;
|
386 | }
|
387 | }
|
388 | }, {
|
389 | key: "fixMissingPlus",
|
390 | value: function fixMissingPlus(state) {
|
391 | if (!state.international) {
|
392 | var _extractCountryCallin2 = (0, _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign["default"])(state.digits, this.defaultCountry, this.defaultCallingCode, this.metadata.metadata),
|
393 | newCallingCode = _extractCountryCallin2.countryCallingCode,
|
394 | number = _extractCountryCallin2.number;
|
395 |
|
396 | if (newCallingCode) {
|
397 | state.update({
|
398 | missingPlus: true
|
399 | });
|
400 | this.startInternationalNumber(state);
|
401 | return true;
|
402 | }
|
403 | }
|
404 | }
|
405 | }, {
|
406 | key: "startInternationalNumber",
|
407 | value: function startInternationalNumber(state) {
|
408 | state.startInternationalNumber();
|
409 |
|
410 | if (state.nationalSignificantNumber) {
|
411 | state.resetNationalSignificantNumber();
|
412 | this.onNationalSignificantNumberChange();
|
413 | this.hasExtractedNationalSignificantNumber = undefined;
|
414 | }
|
415 | }
|
416 | }, {
|
417 | key: "extractCallingCodeAndNationalSignificantNumber",
|
418 | value: function extractCallingCodeAndNationalSignificantNumber(state) {
|
419 | if (this.extractCountryCallingCode(state)) {
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | this.extractNationalSignificantNumber(state.getNationalDigits(), state.update);
|
427 | }
|
428 | }
|
429 | }]);
|
430 |
|
431 | return AsYouTypeParser;
|
432 | }();
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 | exports["default"] = AsYouTypeParser;
|
441 |
|
442 | function extractFormattedPhoneNumber(text) {
|
443 |
|
444 | var startsAt = text.search(VALID_PHONE_NUMBER);
|
445 |
|
446 | if (startsAt < 0) {
|
447 | return;
|
448 | }
|
449 |
|
450 |
|
451 | text = text.slice(startsAt);
|
452 |
|
453 | var hasPlus;
|
454 |
|
455 | if (text[0] === '+') {
|
456 | hasPlus = true;
|
457 | text = text.slice('+'.length);
|
458 | }
|
459 |
|
460 |
|
461 | text = text.replace(AFTER_PHONE_NUMBER_DIGITS_END_PATTERN, '');
|
462 |
|
463 | if (hasPlus) {
|
464 | text = '+' + text;
|
465 | }
|
466 |
|
467 | return text;
|
468 | }
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function _extractFormattedDigitsAndPlus(text) {
|
477 |
|
478 | var extractedNumber = extractFormattedPhoneNumber(text) || '';
|
479 |
|
480 | if (extractedNumber[0] === '+') {
|
481 | return [extractedNumber.slice('+'.length), true];
|
482 | }
|
483 |
|
484 | return [extractedNumber];
|
485 | }
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | function extractFormattedDigitsAndPlus(text) {
|
494 | var _extractFormattedDigi3 = _extractFormattedDigitsAndPlus(text),
|
495 | _extractFormattedDigi4 = _slicedToArray(_extractFormattedDigi3, 2),
|
496 | formattedDigits = _extractFormattedDigi4[0],
|
497 | hasPlus = _extractFormattedDigi4[1];
|
498 |
|
499 |
|
500 |
|
501 |
|
502 | if (!VALID_FORMATTED_PHONE_NUMBER_PART_PATTERN.test(formattedDigits)) {
|
503 | formattedDigits = '';
|
504 | }
|
505 |
|
506 | return [formattedDigits, hasPlus];
|
507 | }
|
508 |
|
\ | No newline at end of file |