1 | import parsePhoneNumber_ from './parsePhoneNumber_'
|
2 |
|
3 | export default function parsePhoneNumber() {
|
4 | const { text, options, metadata } = normalizeArguments(arguments)
|
5 | return parsePhoneNumber_(text, options, metadata)
|
6 | }
|
7 |
|
8 | export 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 |
|
17 |
|
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 |
|
24 |
|
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 |
|
40 |
|
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 |
|
60 |
|
61 | const isObject = _ => typeof _ === 'object' |
\ | No newline at end of file |