1 | 'use strict';
|
---|
2 |
|
---|
3 | function isIterable(value) {
|
---|
4 | return (
|
---|
5 | typeof value === 'object' &&
|
---|
6 | value !== null &&
|
---|
7 | (
|
---|
8 | typeof value[Symbol.iterator] === 'function' ||
|
---|
9 | typeof value[Symbol.asyncIterator] === 'function'
|
---|
10 | )
|
---|
11 | );
|
---|
12 | }
|
---|
13 |
|
---|
14 | function replaceValue(holder, key, value, replacer) {
|
---|
15 | if (value && typeof value.toJSON === 'function') {
|
---|
16 | value = value.toJSON();
|
---|
17 | }
|
---|
18 |
|
---|
19 | if (replacer !== null) {
|
---|
20 | value = replacer.call(holder, String(key), value);
|
---|
21 | }
|
---|
22 |
|
---|
23 | switch (typeof value) {
|
---|
24 | case 'function':
|
---|
25 | case 'symbol':
|
---|
26 | value = undefined;
|
---|
27 | break;
|
---|
28 |
|
---|
29 | case 'object':
|
---|
30 | if (value !== null) {
|
---|
31 | const cls = value.constructor;
|
---|
32 | if (cls === String || cls === Number || cls === Boolean) {
|
---|
33 | value = value.valueOf();
|
---|
34 | }
|
---|
35 | }
|
---|
36 | break;
|
---|
37 | }
|
---|
38 |
|
---|
39 | return value;
|
---|
40 | }
|
---|
41 |
|
---|
42 | function normalizeReplacer(replacer) {
|
---|
43 | if (typeof replacer === 'function') {
|
---|
44 | return replacer;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (Array.isArray(replacer)) {
|
---|
48 | const allowlist = new Set(replacer
|
---|
49 | .map(item => {
|
---|
50 | const cls = item && item.constructor;
|
---|
51 | return cls === String || cls === Number ? String(item) : null;
|
---|
52 | })
|
---|
53 | .filter(item => typeof item === 'string')
|
---|
54 | );
|
---|
55 |
|
---|
56 | return [...allowlist];
|
---|
57 | }
|
---|
58 |
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | function normalizeSpace(space) {
|
---|
63 | if (typeof space === 'number') {
|
---|
64 | if (!Number.isFinite(space) || space < 1) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return ' '.repeat(Math.min(space, 10));
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (typeof space === 'string') {
|
---|
72 | return space.slice(0, 10) || false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 |
|
---|
78 | function normalizeStringifyOptions(optionsOrReplacer, space) {
|
---|
79 | if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== 'object') {
|
---|
80 | optionsOrReplacer = {
|
---|
81 | replacer: optionsOrReplacer,
|
---|
82 | space
|
---|
83 | };
|
---|
84 | }
|
---|
85 |
|
---|
86 | let replacer = normalizeReplacer(optionsOrReplacer.replacer);
|
---|
87 | let getKeys = Object.keys;
|
---|
88 |
|
---|
89 | if (Array.isArray(replacer)) {
|
---|
90 | const allowlist = replacer;
|
---|
91 |
|
---|
92 | getKeys = () => allowlist;
|
---|
93 | replacer = null;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return {
|
---|
97 | ...optionsOrReplacer,
|
---|
98 | replacer,
|
---|
99 | getKeys,
|
---|
100 | space: normalizeSpace(optionsOrReplacer.space)
|
---|
101 | };
|
---|
102 | }
|
---|
103 |
|
---|
104 | exports.isIterable = isIterable;
|
---|
105 | exports.normalizeReplacer = normalizeReplacer;
|
---|
106 | exports.normalizeSpace = normalizeSpace;
|
---|
107 | exports.normalizeStringifyOptions = normalizeStringifyOptions;
|
---|
108 | exports.replaceValue = replaceValue;
|
---|