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