WXL
3 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
 
function isIterable(value) {
    return (
        typeof value === 'object' &&
        value !== null &&
        (
            typeof value[Symbol.iterator] === 'function' ||
            typeof value[Symbol.asyncIterator] === 'function'
        )
    );
}
 
function replaceValue(holder, key, value, replacer) {
    if (value && typeof value.toJSON === 'function') {
        value = value.toJSON();
    }
 
    if (replacer !== null) {
        value = replacer.call(holder, String(key), value);
    }
 
    switch (typeof value) {
        case 'function':
        case 'symbol':
            value = undefined;
            break;
 
        case 'object':
            if (value !== null) {
                const cls = value.constructor;
                if (cls === String || cls === Number || cls === Boolean) {
                    value = value.valueOf();
                }
            }
            break;
    }
 
    return value;
}
 
function normalizeReplacer(replacer) {
    if (typeof replacer === 'function') {
        return replacer;
    }
 
    if (Array.isArray(replacer)) {
        const allowlist = new Set(replacer
            .map(item => {
                const cls = item && item.constructor;
                return cls === String || cls === Number ? String(item) : null;
            })
            .filter(item => typeof item === 'string')
        );
 
        return [...allowlist];
    }
 
    return null;
}
 
function normalizeSpace(space) {
    if (typeof space === 'number') {
        if (!Number.isFinite(space) || space < 1) {
            return false;
        }
 
        return ' '.repeat(Math.min(space, 10));
    }
 
    if (typeof space === 'string') {
        return space.slice(0, 10) || false;
    }
 
    return false;
}
 
function normalizeStringifyOptions(optionsOrReplacer, space) {
    if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== 'object') {
        optionsOrReplacer = {
            replacer: optionsOrReplacer,
            space
        };
    }
 
    let replacer = normalizeReplacer(optionsOrReplacer.replacer);
    let getKeys = Object.keys;
 
    if (Array.isArray(replacer)) {
        const allowlist = replacer;
 
        getKeys = () => allowlist;
        replacer = null;
    }
 
    return {
        ...optionsOrReplacer,
        replacer,
        getKeys,
        space: normalizeSpace(optionsOrReplacer.space)
    };
}
 
exports.isIterable = isIterable;
exports.normalizeReplacer = normalizeReplacer;
exports.normalizeSpace = normalizeSpace;
exports.normalizeStringifyOptions = normalizeStringifyOptions;
exports.replaceValue = replaceValue;