UNPKG

4.26 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = formatNumber;
7
8var _format_ = _interopRequireDefault(require("./format_"));
9
10var _parse_ = _interopRequireDefault(require("./parse_"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
14function _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); }
15
16function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
17
18function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
19
20function _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; }
21
22function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
23
24function formatNumber() {
25 var _normalizeArguments = normalizeArguments(arguments),
26 input = _normalizeArguments.input,
27 format = _normalizeArguments.format,
28 options = _normalizeArguments.options,
29 metadata = _normalizeArguments.metadata;
30
31 return (0, _format_["default"])(input, format, options, metadata);
32} // Sort out arguments
33
34
35function normalizeArguments(args) {
36 var _Array$prototype$slic = Array.prototype.slice.call(args),
37 _Array$prototype$slic2 = _slicedToArray(_Array$prototype$slic, 5),
38 arg_1 = _Array$prototype$slic2[0],
39 arg_2 = _Array$prototype$slic2[1],
40 arg_3 = _Array$prototype$slic2[2],
41 arg_4 = _Array$prototype$slic2[3],
42 arg_5 = _Array$prototype$slic2[4];
43
44 var input;
45 var format;
46 var options;
47 var metadata; // Sort out arguments.
48 // If the phone number is passed as a string.
49 // `format('8005553535', ...)`.
50
51 if (typeof arg_1 === 'string') {
52 // If country code is supplied.
53 // `format('8005553535', 'RU', 'NATIONAL', [options], metadata)`.
54 if (typeof arg_3 === 'string') {
55 format = arg_3;
56
57 if (arg_5) {
58 options = arg_4;
59 metadata = arg_5;
60 } else {
61 metadata = arg_4;
62 }
63
64 input = (0, _parse_["default"])(arg_1, {
65 defaultCountry: arg_2,
66 extended: true
67 }, metadata);
68 } // Just an international phone number is supplied
69 // `format('+78005553535', 'NATIONAL', [options], metadata)`.
70 else {
71 if (typeof arg_2 !== 'string') {
72 throw new Error('`format` argument not passed to `formatNumber(number, format)`');
73 }
74
75 format = arg_2;
76
77 if (arg_4) {
78 options = arg_3;
79 metadata = arg_4;
80 } else {
81 metadata = arg_3;
82 }
83
84 input = (0, _parse_["default"])(arg_1, {
85 extended: true
86 }, metadata);
87 }
88 } // If the phone number is passed as a parsed number object.
89 // `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
90 else if (is_object(arg_1)) {
91 input = arg_1;
92 format = arg_2;
93
94 if (arg_4) {
95 options = arg_3;
96 metadata = arg_4;
97 } else {
98 metadata = arg_3;
99 }
100 } else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.'); // Legacy lowercase formats.
101
102
103 if (format === 'International') {
104 format = 'INTERNATIONAL';
105 } else if (format === 'National') {
106 format = 'NATIONAL';
107 }
108
109 return {
110 input: input,
111 format: format,
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=format.js.map
\No newline at end of file