MCPcopy Index your code
hub / github.com/subquery/subql / truncateObject

Function truncateObject

packages/node-core/src/utils/string.ts:4–27  ·  view source on GitHub ↗
(value: any, currentDepth: number, maxDepth: number, seen: Set<any> = new Set())

Source from the content-addressed store, hash-verified

2// SPDX-License-Identifier: GPL-3.0
3
4function truncateObject(value: any, currentDepth: number, maxDepth: number, seen: Set<any> = new Set()): any {
5 if (currentDepth >= maxDepth) {
6 if (Array.isArray(value)) return `[Array(${value.length})]`;
7 if (value && typeof value === 'object') return `[Object(${Object.keys(value).length})]`;
8 }
9
10 if (value && typeof value === 'object') {
11 if (seen.has(value)) {
12 throw new Error('Converting circular structure to JSON');
13 }
14 seen.add(value);
15
16 const truncated: any = Array.isArray(value) ? [] : {};
17 for (const key in value) {
18 if (Object.prototype.hasOwnProperty.call(value, key)) {
19 truncated[key] = truncateObject(value[key], currentDepth + 1, maxDepth, seen);
20 }
21 }
22 seen.delete(value);
23 return truncated;
24 }
25
26 return value;
27}
28
29export function handledStringify(obj: any, depth = 0): string {
30 try {

Callers 1

handledStringifyFunction · 0.85

Calls 1

addMethod · 0.80

Tested by

no test coverage detected