1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.validateMetadata = validateMetadata;
|
7 | exports.getExtPrefix = getExtPrefix;
|
8 | exports.getCountryCallingCode = getCountryCallingCode;
|
9 | exports.isSupportedCountry = isSupportedCountry;
|
10 | exports["default"] = void 0;
|
11 |
|
12 | var _semverCompare = _interopRequireDefault(require("./tools/semver-compare"));
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
17 |
|
18 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
19 |
|
20 | 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); } }
|
21 |
|
22 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
23 |
|
24 |
|
25 |
|
26 | var V2 = '1.0.18';
|
27 |
|
28 | var V3 = '1.2.0';
|
29 |
|
30 | var V4 = '1.7.35';
|
31 | var DEFAULT_EXT_PREFIX = ' ext. ';
|
32 | var CALLING_CODE_REG_EXP = /^\d+$/;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var Metadata =
|
38 |
|
39 | function () {
|
40 | function Metadata(metadata) {
|
41 | _classCallCheck(this, Metadata);
|
42 |
|
43 | validateMetadata(metadata);
|
44 | this.metadata = metadata;
|
45 | setVersion.call(this, metadata);
|
46 | }
|
47 |
|
48 | _createClass(Metadata, [{
|
49 | key: "getCountries",
|
50 | value: function getCountries() {
|
51 | return Object.keys(this.metadata.countries).filter(function (_) {
|
52 | return _ !== '001';
|
53 | });
|
54 | }
|
55 | }, {
|
56 | key: "getCountryMetadata",
|
57 | value: function getCountryMetadata(countryCode) {
|
58 | return this.metadata.countries[countryCode];
|
59 | }
|
60 | }, {
|
61 | key: "nonGeographic",
|
62 | value: function nonGeographic() {
|
63 | if (this.v1 || this.v2 || this.v3) return;
|
64 |
|
65 |
|
66 | return this.metadata.nonGeographic || this.metadata.nonGeographical;
|
67 | }
|
68 | }, {
|
69 | key: "hasCountry",
|
70 | value: function hasCountry(country) {
|
71 | return this.getCountryMetadata(country) !== undefined;
|
72 | }
|
73 | }, {
|
74 | key: "hasCallingCode",
|
75 | value: function hasCallingCode(callingCode) {
|
76 | if (this.getCountryCodesForCallingCode(callingCode)) {
|
77 | return true;
|
78 | }
|
79 |
|
80 | if (this.nonGeographic()) {
|
81 | if (this.nonGeographic()[callingCode]) {
|
82 | return true;
|
83 | }
|
84 | } else {
|
85 |
|
86 | var countryCodes = this.countryCallingCodes()[callingCode];
|
87 |
|
88 | if (countryCodes && countryCodes.length === 1 && countryCodes[0] === '001') {
|
89 | return true;
|
90 | }
|
91 | }
|
92 | }
|
93 | }, {
|
94 | key: "isNonGeographicCallingCode",
|
95 | value: function isNonGeographicCallingCode(callingCode) {
|
96 | if (this.nonGeographic()) {
|
97 | return this.nonGeographic()[callingCode] ? true : false;
|
98 | } else {
|
99 | return this.getCountryCodesForCallingCode(callingCode) ? false : true;
|
100 | }
|
101 | }
|
102 |
|
103 | }, {
|
104 | key: "country",
|
105 | value: function country(countryCode) {
|
106 | return this.selectNumberingPlan(countryCode);
|
107 | }
|
108 | }, {
|
109 | key: "selectNumberingPlan",
|
110 | value: function selectNumberingPlan(countryCode, callingCode) {
|
111 |
|
112 | if (countryCode && CALLING_CODE_REG_EXP.test(countryCode)) {
|
113 | callingCode = countryCode;
|
114 | countryCode = null;
|
115 | }
|
116 |
|
117 | if (countryCode && countryCode !== '001') {
|
118 | if (!this.hasCountry(countryCode)) {
|
119 | throw new Error("Unknown country: ".concat(countryCode));
|
120 | }
|
121 |
|
122 | this.numberingPlan = new NumberingPlan(this.getCountryMetadata(countryCode), this);
|
123 | } else if (callingCode) {
|
124 | if (!this.hasCallingCode(callingCode)) {
|
125 | throw new Error("Unknown calling code: ".concat(callingCode));
|
126 | }
|
127 |
|
128 | this.numberingPlan = new NumberingPlan(this.getNumberingPlanMetadata(callingCode), this);
|
129 | } else {
|
130 | this.numberingPlan = undefined;
|
131 | }
|
132 |
|
133 | return this;
|
134 | }
|
135 | }, {
|
136 | key: "getCountryCodesForCallingCode",
|
137 | value: function getCountryCodesForCallingCode(callingCode) {
|
138 | var countryCodes = this.countryCallingCodes()[callingCode];
|
139 |
|
140 | if (countryCodes) {
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | if (countryCodes.length === 1 && countryCodes[0].length === 3) {
|
151 | return;
|
152 | }
|
153 |
|
154 | return countryCodes;
|
155 | }
|
156 | }
|
157 | }, {
|
158 | key: "getCountryCodeForCallingCode",
|
159 | value: function getCountryCodeForCallingCode(callingCode) {
|
160 | var countryCodes = this.getCountryCodesForCallingCode(callingCode);
|
161 |
|
162 | if (countryCodes) {
|
163 | return countryCodes[0];
|
164 | }
|
165 | }
|
166 | }, {
|
167 | key: "getNumberingPlanMetadata",
|
168 | value: function getNumberingPlanMetadata(callingCode) {
|
169 | var countryCode = this.getCountryCodeForCallingCode(callingCode);
|
170 |
|
171 | if (countryCode) {
|
172 | return this.getCountryMetadata(countryCode);
|
173 | }
|
174 |
|
175 | if (this.nonGeographic()) {
|
176 | var metadata = this.nonGeographic()[callingCode];
|
177 |
|
178 | if (metadata) {
|
179 | return metadata;
|
180 | }
|
181 | } else {
|
182 |
|
183 | var countryCodes = this.countryCallingCodes()[callingCode];
|
184 |
|
185 | if (countryCodes && countryCodes.length === 1 && countryCodes[0] === '001') {
|
186 | return this.metadata.countries['001'];
|
187 | }
|
188 | }
|
189 | }
|
190 |
|
191 | }, {
|
192 | key: "countryCallingCode",
|
193 | value: function countryCallingCode() {
|
194 | return this.numberingPlan.callingCode();
|
195 | }
|
196 |
|
197 | }, {
|
198 | key: "IDDPrefix",
|
199 | value: function IDDPrefix() {
|
200 | return this.numberingPlan.IDDPrefix();
|
201 | }
|
202 |
|
203 | }, {
|
204 | key: "defaultIDDPrefix",
|
205 | value: function defaultIDDPrefix() {
|
206 | return this.numberingPlan.defaultIDDPrefix();
|
207 | }
|
208 |
|
209 | }, {
|
210 | key: "nationalNumberPattern",
|
211 | value: function nationalNumberPattern() {
|
212 | return this.numberingPlan.nationalNumberPattern();
|
213 | }
|
214 |
|
215 | }, {
|
216 | key: "possibleLengths",
|
217 | value: function possibleLengths() {
|
218 | return this.numberingPlan.possibleLengths();
|
219 | }
|
220 |
|
221 | }, {
|
222 | key: "formats",
|
223 | value: function formats() {
|
224 | return this.numberingPlan.formats();
|
225 | }
|
226 |
|
227 | }, {
|
228 | key: "nationalPrefixForParsing",
|
229 | value: function nationalPrefixForParsing() {
|
230 | return this.numberingPlan.nationalPrefixForParsing();
|
231 | }
|
232 |
|
233 | }, {
|
234 | key: "nationalPrefixTransformRule",
|
235 | value: function nationalPrefixTransformRule() {
|
236 | return this.numberingPlan.nationalPrefixTransformRule();
|
237 | }
|
238 |
|
239 | }, {
|
240 | key: "leadingDigits",
|
241 | value: function leadingDigits() {
|
242 | return this.numberingPlan.leadingDigits();
|
243 | }
|
244 |
|
245 | }, {
|
246 | key: "hasTypes",
|
247 | value: function hasTypes() {
|
248 | return this.numberingPlan.hasTypes();
|
249 | }
|
250 |
|
251 | }, {
|
252 | key: "type",
|
253 | value: function type(_type) {
|
254 | return this.numberingPlan.type(_type);
|
255 | }
|
256 |
|
257 | }, {
|
258 | key: "ext",
|
259 | value: function ext() {
|
260 | return this.numberingPlan.ext();
|
261 | }
|
262 | }, {
|
263 | key: "countryCallingCodes",
|
264 | value: function countryCallingCodes() {
|
265 | if (this.v1) return this.metadata.country_phone_code_to_countries;
|
266 | return this.metadata.country_calling_codes;
|
267 | }
|
268 |
|
269 | }, {
|
270 | key: "chooseCountryByCountryCallingCode",
|
271 | value: function chooseCountryByCountryCallingCode(callingCode) {
|
272 | return this.selectNumberingPlan(callingCode);
|
273 | }
|
274 | }, {
|
275 | key: "hasSelectedNumberingPlan",
|
276 | value: function hasSelectedNumberingPlan() {
|
277 | return this.numberingPlan !== undefined;
|
278 | }
|
279 | }]);
|
280 |
|
281 | return Metadata;
|
282 | }();
|
283 |
|
284 | exports["default"] = Metadata;
|
285 |
|
286 | var NumberingPlan =
|
287 |
|
288 | function () {
|
289 | function NumberingPlan(metadata, globalMetadataObject) {
|
290 | _classCallCheck(this, NumberingPlan);
|
291 |
|
292 | this.globalMetadataObject = globalMetadataObject;
|
293 | this.metadata = metadata;
|
294 | setVersion.call(this, globalMetadataObject.metadata);
|
295 | }
|
296 |
|
297 | _createClass(NumberingPlan, [{
|
298 | key: "callingCode",
|
299 | value: function callingCode() {
|
300 | return this.metadata[0];
|
301 | }
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 | }, {
|
309 | key: "getDefaultCountryMetadataForRegion",
|
310 | value: function getDefaultCountryMetadataForRegion() {
|
311 | return this.globalMetadataObject.getNumberingPlanMetadata(this.callingCode());
|
312 | }
|
313 | }, {
|
314 | key: "IDDPrefix",
|
315 | value: function IDDPrefix() {
|
316 | if (this.v1 || this.v2) return;
|
317 | return this.metadata[1];
|
318 | }
|
319 | }, {
|
320 | key: "defaultIDDPrefix",
|
321 | value: function defaultIDDPrefix() {
|
322 | if (this.v1 || this.v2) return;
|
323 | return this.metadata[12];
|
324 | }
|
325 | }, {
|
326 | key: "nationalNumberPattern",
|
327 | value: function nationalNumberPattern() {
|
328 | if (this.v1 || this.v2) return this.metadata[1];
|
329 | return this.metadata[2];
|
330 | }
|
331 | }, {
|
332 | key: "possibleLengths",
|
333 | value: function possibleLengths() {
|
334 | if (this.v1) return;
|
335 | return this.metadata[this.v2 ? 2 : 3];
|
336 | }
|
337 | }, {
|
338 | key: "_getFormats",
|
339 | value: function _getFormats(metadata) {
|
340 | return metadata[this.v1 ? 2 : this.v2 ? 3 : 4];
|
341 | }
|
342 |
|
343 |
|
344 |
|
345 | }, {
|
346 | key: "formats",
|
347 | value: function formats() {
|
348 | var _this = this;
|
349 |
|
350 | var formats = this._getFormats(this.metadata) || this._getFormats(this.getDefaultCountryMetadataForRegion()) || [];
|
351 | return formats.map(function (_) {
|
352 | return new Format(_, _this);
|
353 | });
|
354 | }
|
355 | }, {
|
356 | key: "nationalPrefix",
|
357 | value: function nationalPrefix() {
|
358 | return this.metadata[this.v1 ? 3 : this.v2 ? 4 : 5];
|
359 | }
|
360 | }, {
|
361 | key: "_getNationalPrefixFormattingRule",
|
362 | value: function _getNationalPrefixFormattingRule(metadata) {
|
363 | return metadata[this.v1 ? 4 : this.v2 ? 5 : 6];
|
364 | }
|
365 |
|
366 |
|
367 |
|
368 | }, {
|
369 | key: "nationalPrefixFormattingRule",
|
370 | value: function nationalPrefixFormattingRule() {
|
371 | return this._getNationalPrefixFormattingRule(this.metadata) || this._getNationalPrefixFormattingRule(this.getDefaultCountryMetadataForRegion());
|
372 | }
|
373 | }, {
|
374 | key: "_nationalPrefixForParsing",
|
375 | value: function _nationalPrefixForParsing() {
|
376 | return this.metadata[this.v1 ? 5 : this.v2 ? 6 : 7];
|
377 | }
|
378 | }, {
|
379 | key: "nationalPrefixForParsing",
|
380 | value: function nationalPrefixForParsing() {
|
381 |
|
382 |
|
383 | return this._nationalPrefixForParsing() || this.nationalPrefix();
|
384 | }
|
385 | }, {
|
386 | key: "nationalPrefixTransformRule",
|
387 | value: function nationalPrefixTransformRule() {
|
388 | return this.metadata[this.v1 ? 6 : this.v2 ? 7 : 8];
|
389 | }
|
390 | }, {
|
391 | key: "_getNationalPrefixIsOptionalWhenFormatting",
|
392 | value: function _getNationalPrefixIsOptionalWhenFormatting() {
|
393 | return !!this.metadata[this.v1 ? 7 : this.v2 ? 8 : 9];
|
394 | }
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | }, {
|
400 | key: "nationalPrefixIsOptionalWhenFormattingInNationalFormat",
|
401 | value: function nationalPrefixIsOptionalWhenFormattingInNationalFormat() {
|
402 | return this._getNationalPrefixIsOptionalWhenFormatting(this.metadata) || this._getNationalPrefixIsOptionalWhenFormatting(this.getDefaultCountryMetadataForRegion());
|
403 | }
|
404 | }, {
|
405 | key: "leadingDigits",
|
406 | value: function leadingDigits() {
|
407 | return this.metadata[this.v1 ? 8 : this.v2 ? 9 : 10];
|
408 | }
|
409 | }, {
|
410 | key: "types",
|
411 | value: function types() {
|
412 | return this.metadata[this.v1 ? 9 : this.v2 ? 10 : 11];
|
413 | }
|
414 | }, {
|
415 | key: "hasTypes",
|
416 | value: function hasTypes() {
|
417 |
|
418 |
|
419 |
|
420 | if (this.types() && this.types().length === 0) {
|
421 | return false;
|
422 | }
|
423 |
|
424 |
|
425 |
|
426 | return !!this.types();
|
427 | }
|
428 | }, {
|
429 | key: "type",
|
430 | value: function type(_type2) {
|
431 | if (this.hasTypes() && getType(this.types(), _type2)) {
|
432 | return new Type(getType(this.types(), _type2), this);
|
433 | }
|
434 | }
|
435 | }, {
|
436 | key: "ext",
|
437 | value: function ext() {
|
438 | if (this.v1 || this.v2) return DEFAULT_EXT_PREFIX;
|
439 | return this.metadata[13] || DEFAULT_EXT_PREFIX;
|
440 | }
|
441 | }]);
|
442 |
|
443 | return NumberingPlan;
|
444 | }();
|
445 |
|
446 | var Format =
|
447 |
|
448 | function () {
|
449 | function Format(format, metadata) {
|
450 | _classCallCheck(this, Format);
|
451 |
|
452 | this._format = format;
|
453 | this.metadata = metadata;
|
454 | }
|
455 |
|
456 | _createClass(Format, [{
|
457 | key: "pattern",
|
458 | value: function pattern() {
|
459 | return this._format[0];
|
460 | }
|
461 | }, {
|
462 | key: "format",
|
463 | value: function format() {
|
464 | return this._format[1];
|
465 | }
|
466 | }, {
|
467 | key: "leadingDigitsPatterns",
|
468 | value: function leadingDigitsPatterns() {
|
469 | return this._format[2] || [];
|
470 | }
|
471 | }, {
|
472 | key: "nationalPrefixFormattingRule",
|
473 | value: function nationalPrefixFormattingRule() {
|
474 | return this._format[3] || this.metadata.nationalPrefixFormattingRule();
|
475 | }
|
476 | }, {
|
477 | key: "nationalPrefixIsOptionalWhenFormattingInNationalFormat",
|
478 | value: function nationalPrefixIsOptionalWhenFormattingInNationalFormat() {
|
479 | return !!this._format[4] || this.metadata.nationalPrefixIsOptionalWhenFormattingInNationalFormat();
|
480 | }
|
481 | }, {
|
482 | key: "nationalPrefixIsMandatoryWhenFormattingInNationalFormat",
|
483 | value: function nationalPrefixIsMandatoryWhenFormattingInNationalFormat() {
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | return this.usesNationalPrefix() && !this.nationalPrefixIsOptionalWhenFormattingInNationalFormat();
|
490 | }
|
491 |
|
492 | }, {
|
493 | key: "usesNationalPrefix",
|
494 | value: function usesNationalPrefix() {
|
495 | return this.nationalPrefixFormattingRule() &&
|
496 | !FIRST_GROUP_ONLY_PREFIX_PATTERN.test(this.nationalPrefixFormattingRule())
|
497 |
|
498 |
|
499 |
|
500 | ? true : false;
|
501 | }
|
502 | }, {
|
503 | key: "internationalFormat",
|
504 | value: function internationalFormat() {
|
505 | return this._format[5] || this.format();
|
506 | }
|
507 | }]);
|
508 |
|
509 | return Format;
|
510 | }();
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 | var FIRST_GROUP_ONLY_PREFIX_PATTERN = /^\(?\$1\)?$/;
|
519 |
|
520 | var Type =
|
521 |
|
522 | function () {
|
523 | function Type(type, metadata) {
|
524 | _classCallCheck(this, Type);
|
525 |
|
526 | this.type = type;
|
527 | this.metadata = metadata;
|
528 | }
|
529 |
|
530 | _createClass(Type, [{
|
531 | key: "pattern",
|
532 | value: function pattern() {
|
533 | if (this.metadata.v1) return this.type;
|
534 | return this.type[0];
|
535 | }
|
536 | }, {
|
537 | key: "possibleLengths",
|
538 | value: function possibleLengths() {
|
539 | if (this.metadata.v1) return;
|
540 | return this.type[1] || this.metadata.possibleLengths();
|
541 | }
|
542 | }]);
|
543 |
|
544 | return Type;
|
545 | }();
|
546 |
|
547 | function getType(types, type) {
|
548 | switch (type) {
|
549 | case 'FIXED_LINE':
|
550 | return types[0];
|
551 |
|
552 | case 'MOBILE':
|
553 | return types[1];
|
554 |
|
555 | case 'TOLL_FREE':
|
556 | return types[2];
|
557 |
|
558 | case 'PREMIUM_RATE':
|
559 | return types[3];
|
560 |
|
561 | case 'PERSONAL_NUMBER':
|
562 | return types[4];
|
563 |
|
564 | case 'VOICEMAIL':
|
565 | return types[5];
|
566 |
|
567 | case 'UAN':
|
568 | return types[6];
|
569 |
|
570 | case 'PAGER':
|
571 | return types[7];
|
572 |
|
573 | case 'VOIP':
|
574 | return types[8];
|
575 |
|
576 | case 'SHARED_COST':
|
577 | return types[9];
|
578 | }
|
579 | }
|
580 |
|
581 | function validateMetadata(metadata) {
|
582 | if (!metadata) {
|
583 | throw new Error('[libphonenumber-js] `metadata` argument not passed. Check your arguments.');
|
584 | }
|
585 |
|
586 |
|
587 |
|
588 | if (!is_object(metadata) || !is_object(metadata.countries)) {
|
589 | throw new Error("[libphonenumber-js] `metadata` argument was passed but it's not a valid metadata. Must be an object having `.countries` child object property. Got ".concat(is_object(metadata) ? 'an object of shape: { ' + Object.keys(metadata).join(', ') + ' }' : 'a ' + type_of(metadata) + ': ' + metadata, "."));
|
590 | }
|
591 | }
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 | var is_object = function is_object(_) {
|
598 | return _typeof(_) === 'object';
|
599 | };
|
600 |
|
601 |
|
602 |
|
603 |
|
604 |
|
605 | var type_of = function type_of(_) {
|
606 | return _typeof(_);
|
607 | };
|
608 |
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 |
|
619 | function getExtPrefix(country, metadata) {
|
620 | metadata = new Metadata(metadata);
|
621 |
|
622 | if (metadata.hasCountry(country)) {
|
623 | return metadata.country(country).ext();
|
624 | }
|
625 |
|
626 | return DEFAULT_EXT_PREFIX;
|
627 | }
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 | function getCountryCallingCode(country, metadata) {
|
641 | metadata = new Metadata(metadata);
|
642 |
|
643 | if (metadata.hasCountry(country)) {
|
644 | return metadata.country(country).countryCallingCode();
|
645 | }
|
646 |
|
647 | throw new Error("Unknown country: ".concat(country));
|
648 | }
|
649 |
|
650 | function isSupportedCountry(country, metadata) {
|
651 |
|
652 |
|
653 | return metadata.countries[country] !== undefined;
|
654 | }
|
655 |
|
656 | function setVersion(metadata) {
|
657 | var version = metadata.version;
|
658 |
|
659 | if (typeof version === 'number') {
|
660 | this.v1 = version === 1;
|
661 | this.v2 = version === 2;
|
662 | this.v3 = version === 3;
|
663 | this.v4 = version === 4;
|
664 | } else {
|
665 | if (!version) {
|
666 | this.v1 = true;
|
667 | } else if ((0, _semverCompare["default"])(version, V3) === -1) {
|
668 | this.v2 = true;
|
669 | } else if ((0, _semverCompare["default"])(version, V4) === -1) {
|
670 | this.v3 = true;
|
671 | } else {
|
672 | this.v4 = true;
|
673 | }
|
674 | }
|
675 | }
|
676 |
|
677 |
|
678 |
|
679 |
|
\ | No newline at end of file |