libphonenumber-js
Version:
A simpler (and smaller) rewrite of Google Android's popular libphonenumber library
97 lines (87 loc) • 3.23 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
import isViablePhoneNumber from './isViablePhoneNumber';
import _getNumberType from './getNumberType_';
import parse from './parse_';
// Finds out national phone number type (fixed line, mobile, etc)
export default function getNumberType(arg_1, arg_2, arg_3, arg_4) {
var _sort_out_arguments = sort_out_arguments(arg_1, arg_2, arg_3, arg_4),
input = _sort_out_arguments.input,
options = _sort_out_arguments.options,
metadata = _sort_out_arguments.metadata;
return _getNumberType(input, options, metadata);
}
// Sort out arguments
export function sort_out_arguments(arg_1, arg_2, arg_3, arg_4) {
var input = void 0;
var options = {};
var metadata = void 0;
// 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 === 'undefined' ? 'undefined' : _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 (isViablePhoneNumber(arg_1)) {
input = parse(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 (isViablePhoneNumber(arg_1)) {
input = parse(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 _ === 'undefined' ? 'undefined' : _typeof(_)) === 'object';
};
//# sourceMappingURL=getNumberType.js.map