UNPKG

21.1 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validateMetadata = validateMetadata;
7exports.getExtPrefix = getExtPrefix;
8exports.getCountryCallingCode = getCountryCallingCode;
9exports.isSupportedCountry = isSupportedCountry;
10exports["default"] = void 0;
11
12var _semverCompare = _interopRequireDefault(require("./tools/semver-compare"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16function _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
18function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
19
20function _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
22function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
23
24// Added "possibleLengths" and renamed
25// "country_phone_code_to_countries" to "country_calling_codes".
26var V2 = '1.0.18'; // Added "idd_prefix" and "default_idd_prefix".
27
28var V3 = '1.2.0'; // Moved `001` country code to "nonGeographic" section of metadata.
29
30var V4 = '1.7.35';
31var DEFAULT_EXT_PREFIX = ' ext. ';
32var CALLING_CODE_REG_EXP = /^\d+$/;
33/**
34 * See: https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md
35 */
36
37var Metadata =
38/*#__PURE__*/
39function () {
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; // `nonGeographical` was a typo.
64 // It's present in metadata generated from `1.7.35` to `1.7.37`.
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 // A hacky workaround for old custom metadata (generated before V4).
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 } // Deprecated.
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 // Supports just passing `callingCode` as the first argument.
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 // Metadata before V4 included "non-geographic entity" calling codes
142 // inside `country_calling_codes` (for example, `"881":["001"]`).
143 // Now the semantics of `country_calling_codes` has changed:
144 // it's specifically for "countries" now.
145 // Older versions of custom metadata will simply skip parsing
146 // "non-geographic entity" phone numbers with new versions
147 // of this library: it's not considered a bug,
148 // because such numbers are extremely rare,
149 // and developers extremely rarely use custom metadata.
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 // A hacky workaround for old custom metadata (generated before V4).
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 } // Deprecated.
190
191 }, {
192 key: "countryCallingCode",
193 value: function countryCallingCode() {
194 return this.numberingPlan.callingCode();
195 } // Deprecated.
196
197 }, {
198 key: "IDDPrefix",
199 value: function IDDPrefix() {
200 return this.numberingPlan.IDDPrefix();
201 } // Deprecated.
202
203 }, {
204 key: "defaultIDDPrefix",
205 value: function defaultIDDPrefix() {
206 return this.numberingPlan.defaultIDDPrefix();
207 } // Deprecated.
208
209 }, {
210 key: "nationalNumberPattern",
211 value: function nationalNumberPattern() {
212 return this.numberingPlan.nationalNumberPattern();
213 } // Deprecated.
214
215 }, {
216 key: "possibleLengths",
217 value: function possibleLengths() {
218 return this.numberingPlan.possibleLengths();
219 } // Deprecated.
220
221 }, {
222 key: "formats",
223 value: function formats() {
224 return this.numberingPlan.formats();
225 } // Deprecated.
226
227 }, {
228 key: "nationalPrefixForParsing",
229 value: function nationalPrefixForParsing() {
230 return this.numberingPlan.nationalPrefixForParsing();
231 } // Deprecated.
232
233 }, {
234 key: "nationalPrefixTransformRule",
235 value: function nationalPrefixTransformRule() {
236 return this.numberingPlan.nationalPrefixTransformRule();
237 } // Deprecated.
238
239 }, {
240 key: "leadingDigits",
241 value: function leadingDigits() {
242 return this.numberingPlan.leadingDigits();
243 } // Deprecated.
244
245 }, {
246 key: "hasTypes",
247 value: function hasTypes() {
248 return this.numberingPlan.hasTypes();
249 } // Deprecated.
250
251 }, {
252 key: "type",
253 value: function type(_type) {
254 return this.numberingPlan.type(_type);
255 } // Deprecated.
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 } // Deprecated.
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
284exports["default"] = Metadata;
285
286var NumberingPlan =
287/*#__PURE__*/
288function () {
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 } // Formatting information for regions which share
302 // a country calling code is contained by only one region
303 // for performance reasons. For example, for NANPA region
304 // ("North American Numbering Plan Administration",
305 // which includes USA, Canada, Cayman Islands, Bahamas, etc)
306 // it will be contained in the metadata for `US`.
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 } // For countries of the same region (e.g. NANPA)
342 // formats are all stored in the "main" country for that region.
343 // E.g. "RU" and "KZ", "US" and "CA".
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 } // For countries of the same region (e.g. NANPA)
365 // national prefix formatting rule is stored in the "main" country for that region.
366 // E.g. "RU" and "KZ", "US" and "CA".
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 // If `national_prefix_for_parsing` is not set explicitly,
382 // then infer it from `national_prefix` (if any)
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 } // For countries of the same region (e.g. NANPA)
395 // "national prefix is optional when formatting" flag is
396 // stored in the "main" country for that region.
397 // E.g. "RU" and "KZ", "US" and "CA".
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 // Versions 1.2.0 - 1.2.4: can be `[]`.
418
419 /* istanbul ignore next */
420 if (this.types() && this.types().length === 0) {
421 return false;
422 } // Versions <= 1.2.4: can be `undefined`.
423 // Version >= 1.2.5: can be `0`.
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
446var Format =
447/*#__PURE__*/
448function () {
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 // National prefix is omitted if there's no national prefix formatting rule
485 // set for this country, or when the national prefix formatting rule
486 // contains no national prefix itself, or when this rule is set but
487 // national prefix is optional for this phone number format
488 // (and it is not enforced explicitly)
489 return this.usesNationalPrefix() && !this.nationalPrefixIsOptionalWhenFormattingInNationalFormat();
490 } // Checks whether national prefix formatting rule contains national prefix.
491
492 }, {
493 key: "usesNationalPrefix",
494 value: function usesNationalPrefix() {
495 return this.nationalPrefixFormattingRule() && // Check that national prefix formatting rule is not a "dummy" one.
496 !FIRST_GROUP_ONLY_PREFIX_PATTERN.test(this.nationalPrefixFormattingRule()) // In compressed metadata, `this.nationalPrefixFormattingRule()` is `0`
497 // when `national_prefix_formatting_rule` is not present.
498 // So, `true` or `false` are returned explicitly here, so that
499 // `0` number isn't returned.
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 * A pattern that is used to determine if the national prefix formatting rule
513 * has the first group only, i.e., does not start with the national prefix.
514 * Note that the pattern explicitly allows for unbalanced parentheses.
515 */
516
517
518var FIRST_GROUP_ONLY_PREFIX_PATTERN = /^\(?\$1\)?$/;
519
520var Type =
521/*#__PURE__*/
522function () {
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
547function 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
581function validateMetadata(metadata) {
582 if (!metadata) {
583 throw new Error('[libphonenumber-js] `metadata` argument not passed. Check your arguments.');
584 } // `country_phone_code_to_countries` was renamed to
585 // `country_calling_codes` in `1.0.18`.
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} // Babel transforms `typeof` into some "branches"
592// so istanbul will show this as "branch not covered".
593
594/* istanbul ignore next */
595
596
597var is_object = function is_object(_) {
598 return _typeof(_) === 'object';
599}; // Babel transforms `typeof` into some "branches"
600// so istanbul will show this as "branch not covered".
601
602/* istanbul ignore next */
603
604
605var type_of = function type_of(_) {
606 return _typeof(_);
607};
608/**
609 * Returns extension prefix for a country.
610 * @param {string} country
611 * @param {object} metadata
612 * @return {string?}
613 * @example
614 * // Returns " ext. "
615 * getExtPrefix("US")
616 */
617
618
619function 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 * Returns "country calling code" for a country.
630 * Throws an error if the country doesn't exist or isn't supported by this library.
631 * @param {string} country
632 * @param {object} metadata
633 * @return {string}
634 * @example
635 * // Returns "44"
636 * getCountryCallingCode("GB")
637 */
638
639
640function 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
650function isSupportedCountry(country, metadata) {
651 // metadata = new Metadata(metadata)
652 // return metadata.hasCountry(country)
653 return metadata.countries[country] !== undefined;
654}
655
656function 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} // const ISO_COUNTRY_CODE = /^[A-Z]{2}$/
676// function isCountryCode(countryCode) {
677// return ISO_COUNTRY_CODE.test(countryCodeOrCountryCallingCode)
678// }
679//# sourceMappingURL=metadata.js.map
\No newline at end of file