UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = getNumberType;
7exports.normalizeArguments = normalizeArguments;
8
9var _isViablePhoneNumber = _interopRequireDefault(require("./helpers/isViablePhoneNumber"));
10
11var _getNumberType2 = _interopRequireDefault(require("./helpers/getNumberType"));
12
13var _parse_ = _interopRequireDefault(require("./parse_"));
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
16
17function _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); }
18
19function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
20
21function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
22
23function _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; }
24
25function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
26
27// Finds out national phone number type (fixed line, mobile, etc)
28function getNumberType() {
29 var _normalizeArguments = normalizeArguments(arguments),
30 input = _normalizeArguments.input,
31 options = _normalizeArguments.options,
32 metadata = _normalizeArguments.metadata;
33
34 return (0, _getNumberType2["default"])(input, options, metadata);
35} // Sort out arguments
36
37
38function normalizeArguments(args) {
39 var _Array$prototype$slic = Array.prototype.slice.call(args),
40 _Array$prototype$slic2 = _slicedToArray(_Array$prototype$slic, 4),
41 arg_1 = _Array$prototype$slic2[0],
42 arg_2 = _Array$prototype$slic2[1],
43 arg_3 = _Array$prototype$slic2[2],
44 arg_4 = _Array$prototype$slic2[3];
45
46 var input;
47 var options = {};
48 var metadata; // If the phone number is passed as a string.
49 // `getNumberType('88005553535', ...)`.
50
51 if (typeof arg_1 === 'string') {
52 // If "default country" argument is being passed
53 // then convert it to an `options` object.
54 // `getNumberType('88005553535', 'RU', metadata)`.
55 if (_typeof(arg_2) !== 'object') {
56 if (arg_4) {
57 options = arg_3;
58 metadata = arg_4;
59 } else {
60 metadata = arg_3;
61 } // `parse` extracts phone numbers from raw text,
62 // therefore it will cut off all "garbage" characters,
63 // while this `validate` function needs to verify
64 // that the phone number contains no "garbage"
65 // therefore the explicit `isViablePhoneNumber` check.
66
67
68 if ((0, _isViablePhoneNumber["default"])(arg_1)) {
69 input = (0, _parse_["default"])(arg_1, {
70 defaultCountry: arg_2
71 }, metadata);
72 } else {
73 input = {};
74 }
75 } // No "resrict country" argument is being passed.
76 // International phone number is passed.
77 // `getNumberType('+78005553535', metadata)`.
78 else {
79 if (arg_3) {
80 options = arg_2;
81 metadata = arg_3;
82 } else {
83 metadata = arg_2;
84 } // `parse` extracts phone numbers from raw text,
85 // therefore it will cut off all "garbage" characters,
86 // while this `validate` function needs to verify
87 // that the phone number contains no "garbage"
88 // therefore the explicit `isViablePhoneNumber` check.
89
90
91 if ((0, _isViablePhoneNumber["default"])(arg_1)) {
92 input = (0, _parse_["default"])(arg_1, undefined, metadata);
93 } else {
94 input = {};
95 }
96 }
97 } // If the phone number is passed as a parsed phone number.
98 // `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`.
99 else if (is_object(arg_1)) {
100 input = arg_1;
101
102 if (arg_3) {
103 options = arg_2;
104 metadata = arg_3;
105 } else {
106 metadata = arg_2;
107 }
108 } else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.');
109
110 return {
111 input: input,
112 options: options,
113 metadata: metadata
114 };
115} // Babel transforms `typeof` into some "branches"
116// so istanbul will show this as "branch not covered".
117
118/* istanbul ignore next */
119
120
121var is_object = function is_object(_) {
122 return _typeof(_) === 'object';
123};
124//# sourceMappingURL=getNumberType.js.map
\No newline at end of file