15815213711
2024-08-26 67b8b6731811983447e053d4396b3708c14dfe3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import isObject from './helpers/isObject.js'
 
// Extracts the following properties from function arguments:
// * input `text`
// * `options` object
// * `metadata` JSON
export default function normalizeArguments(args) {
    const [arg_1, arg_2, arg_3, arg_4] = Array.prototype.slice.call(args)
 
    let text
    let options
    let metadata
 
    // If the phone number is passed as a string.
    // `parsePhoneNumber('88005553535', ...)`.
    if (typeof arg_1 === 'string') {
        text = arg_1
    }
    else throw new TypeError('A text for parsing must be a string.')
 
    // If "default country" argument is being passed then move it to `options`.
    // `parsePhoneNumber('88005553535', 'RU', [options], metadata)`.
    if (!arg_2 || typeof arg_2 === 'string')
    {
        if (arg_4) {
            options = arg_3
            metadata = arg_4
        } else {
            options = undefined
            metadata = arg_3
        }
 
        if (arg_2) {
            options = { defaultCountry: arg_2, ...options }
        }
    }
    // `defaultCountry` is not passed.
    // Example: `parsePhoneNumber('+78005553535', [options], metadata)`.
    else if (isObject(arg_2))
    {
        if (arg_3) {
            options  = arg_2
            metadata = arg_3
        } else {
            metadata = arg_2
        }
    }
    else throw new Error(`Invalid second argument: ${arg_2}`)
 
    return {
        text,
        options,
        metadata
    }
}