UNPKG

1.48 kBJavaScriptView Raw
1import parsePhoneNumber_ from './parsePhoneNumber_'
2
3export default function parsePhoneNumber() {
4 const { text, options, metadata } = normalizeArguments(arguments)
5 return parsePhoneNumber_(text, options, metadata)
6}
7
8export function normalizeArguments(args)
9{
10 const [arg_1, arg_2, arg_3, arg_4] = Array.prototype.slice.call(args)
11
12 let text
13 let options
14 let metadata
15
16 // If the phone number is passed as a string.
17 // `parsePhoneNumber('88005553535', ...)`.
18 if (typeof arg_1 === 'string') {
19 text = arg_1
20 }
21 else throw new TypeError('A text for parsing must be a string.')
22
23 // If "default country" argument is being passed then move it to `options`.
24 // `parsePhoneNumber('88005553535', 'RU', [options], metadata)`.
25 if (!arg_2 || typeof arg_2 === 'string')
26 {
27 if (arg_4) {
28 options = arg_3
29 metadata = arg_4
30 } else {
31 options = undefined
32 metadata = arg_3
33 }
34
35 if (arg_2) {
36 options = { defaultCountry: arg_2, ...options }
37 }
38 }
39 // `defaultCountry` is not passed.
40 // Example: `parsePhoneNumber('+78005553535', [options], metadata)`.
41 else if (isObject(arg_2))
42 {
43 if (arg_3) {
44 options = arg_2
45 metadata = arg_3
46 } else {
47 metadata = arg_2
48 }
49 }
50 else throw new Error(`Invalid second argument: ${arg_2}`)
51
52 return {
53 text,
54 options,
55 metadata
56 }
57}
58
59// Otherwise istanbul would show this as "branch not covered".
60/* istanbul ignore next */
61const isObject = _ => typeof _ === 'object'
\No newline at end of file