( obj: unknown, stringifyOptions?: StringifyOptions, )
| 79 | * @param obj - the object to stringify |
| 80 | */ |
| 81 | export function stringify( |
| 82 | obj: unknown, |
| 83 | stringifyOptions?: StringifyOptions, |
| 84 | ): string { |
| 85 | const options: StringifyOptions = { |
| 86 | numOfKeysLimit: 50, |
| 87 | depthOfLimit: 4, |
| 88 | }; |
| 89 | Object.assign(options, stringifyOptions); |
| 90 | const stack: unknown[] = []; |
| 91 | const keys: unknown[] = []; |
| 92 | return JSON.stringify( |
| 93 | obj, |
| 94 | function (key, value: string | bigint | object | null | undefined) { |
| 95 | /** |
| 96 | * forked from https://github.com/moll/json-stringify-safe/blob/master/stringify.js |
| 97 | * to deCycle the object |
| 98 | */ |
| 99 | if (stack.length > 0) { |
| 100 | const thisPos = stack.indexOf(this); |
| 101 | ~thisPos ? stack.splice(thisPos + 1) : stack.push(this); |
| 102 | ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key); |
| 103 | if (~stack.indexOf(value)) { |
| 104 | if (stack[0] === value) { |
| 105 | value = '[Circular ~]'; |
| 106 | } else { |
| 107 | value = |
| 108 | '[Circular ~.' + |
| 109 | keys.slice(0, stack.indexOf(value)).join('.') + |
| 110 | ']'; |
| 111 | } |
| 112 | } |
| 113 | } else { |
| 114 | stack.push(value); |
| 115 | } |
| 116 | /* END of the FORK */ |
| 117 | |
| 118 | if (value === null) return value; |
| 119 | if (value === undefined) return 'undefined'; |
| 120 | if (shouldIgnore(value as object)) { |
| 121 | return toString(value as object); |
| 122 | } |
| 123 | if (typeof value === 'bigint') { |
| 124 | return value.toString() + 'n'; |
| 125 | } |
| 126 | if (value instanceof Event) { |
| 127 | const eventResult: Record<string, unknown> = {}; |
| 128 | for (const eventKey in value) { |
| 129 | const eventValue = (value as unknown as Record<string, unknown>)[ |
| 130 | eventKey |
| 131 | ]; |
| 132 | if (Array.isArray(eventValue)) { |
| 133 | eventResult[eventKey] = pathToSelector( |
| 134 | (eventValue.length ? eventValue[0] : null) as HTMLElement, |
| 135 | ); |
| 136 | } else { |
| 137 | eventResult[eventKey] = eventValue; |
| 138 | } |
no test coverage detected