UNPKG

libphonenumber-js

Version:

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

124 lines (99 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = getNumberType; exports.normalizeArguments = normalizeArguments; var _isViablePhoneNumber = _interopRequireDefault(require("./helpers/isViablePhoneNumber")); var _getNumberType2 = _interopRequireDefault(require("./helpers/getNumberType")); var _parse_ = _interopRequireDefault(require("./parse_")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } 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; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } // Finds out national phone number type (fixed line, mobile, etc) function getNumberType() { var _normalizeArguments = normalizeArguments(arguments), input = _normalizeArguments.input, options = _normalizeArguments.options, metadata = _normalizeArguments.metadata; return (0, _getNumberType2["default"])(input, options, metadata); } // Sort out arguments function normalizeArguments(args) { var _Array$prototype$slic = Array.prototype.slice.call(args), _Array$prototype$slic2 = _slicedToArray(_Array$prototype$slic, 4), arg_1 = _Array$prototype$slic2[0], arg_2 = _Array$prototype$slic2[1], arg_3 = _Array$prototype$slic2[2], arg_4 = _Array$prototype$slic2[3]; var input; var options = {}; var metadata; // If the phone number is passed as a string. // `getNumberType('88005553535', ...)`. if (typeof arg_1 === 'string') { // If "default country" argument is being passed // then convert it to an `options` object. // `getNumberType('88005553535', 'RU', metadata)`. if (_typeof(arg_2) !== 'object') { if (arg_4) { options = arg_3; metadata = arg_4; } else { metadata = arg_3; } // `parse` extracts phone numbers from raw text, // therefore it will cut off all "garbage" characters, // while this `validate` function needs to verify // that the phone number contains no "garbage" // therefore the explicit `isViablePhoneNumber` check. if ((0, _isViablePhoneNumber["default"])(arg_1)) { input = (0, _parse_["default"])(arg_1, { defaultCountry: arg_2 }, metadata); } else { input = {}; } } // No "resrict country" argument is being passed. // International phone number is passed. // `getNumberType('+78005553535', metadata)`. else { if (arg_3) { options = arg_2; metadata = arg_3; } else { metadata = arg_2; } // `parse` extracts phone numbers from raw text, // therefore it will cut off all "garbage" characters, // while this `validate` function needs to verify // that the phone number contains no "garbage" // therefore the explicit `isViablePhoneNumber` check. if ((0, _isViablePhoneNumber["default"])(arg_1)) { input = (0, _parse_["default"])(arg_1, undefined, metadata); } else { input = {}; } } } // If the phone number is passed as a parsed phone number. // `getNumberType({ phone: '88005553535', country: 'RU' }, ...)`. else if (is_object(arg_1)) { input = arg_1; if (arg_3) { options = arg_2; metadata = arg_3; } else { metadata = arg_2; } } else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.'); return { input: input, options: options, metadata: metadata }; } // Babel transforms `typeof` into some "branches" // so istanbul will show this as "branch not covered". /* istanbul ignore next */ var is_object = function is_object(_) { return _typeof(_) === 'object'; }; //# sourceMappingURL=getNumberType.js.map