MCPcopy Index your code
hub / github.com/rrweb-io/rrweb / stringify

Function stringify

packages/plugins/rrweb-plugin-console-record/src/stringify.ts:81–194  ·  view source on GitHub ↗
(
  obj: unknown,
  stringifyOptions?: StringifyOptions,
)

Source from the content-addressed store, hash-verified

79 * @param obj - the object to stringify
80 */
81export 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 }

Callers 4

stringify.test.tsFile · 0.90
errorHandlerFunction · 0.90
replaceFunction · 0.90

Calls 4

shouldIgnoreFunction · 0.85
pathToSelectorFunction · 0.85
toStringFunction · 0.70
toStringMethod · 0.65

Tested by

no test coverage detected