Skip to content

Commit

Permalink
fix(core): make sure stringifyValueWithType does not print unlimited …
Browse files Browse the repository at this point in the history
…depth
  • Loading branch information
marcj committed Oct 25, 2023
1 parent 8c79e4b commit 56fbef9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ export function isClassInstance(target: any): boolean {
}

/**
* Returns a human readable string representation from the given value.
* Returns a human-readable string representation from the given value.
*/
export function stringifyValueWithType(value: any): string {
export function stringifyValueWithType(value: any, depth: number = 0): string {
if ('string' === typeof value) return `string(${value})`;
if ('number' === typeof value) return `number(${value})`;
if ('boolean' === typeof value) return `boolean(${value})`;
if ('bigint' === typeof value) return `bigint(${value})`;
if (isPlainObject(value)) return `object ${prettyPrintObject(value)}`;
if (isPlainObject(value)) return `object ${depth < 2 ? prettyPrintObject(value, depth) : ''}`;
if (isArray(value)) return `Array`;
if (isClass(value)) return `${getClassName(value)}`;
if (isObject(value)) return `${getClassName(getClassTypeFromInstance(value))} ${prettyPrintObject(value)}`;
if (isObject(value)) return `${getClassName(getClassTypeFromInstance(value))} ${depth < 2 ? prettyPrintObject(value, depth): ''}`;
if ('function' === typeof value) return `function ${value.name}`;
if (null === value) return `null`;
return 'undefined';
Expand Down Expand Up @@ -173,10 +173,10 @@ export function changeClass<T>(value: object, newClass: ClassType<T>): T {
return Object.assign(Object.create(newClass.prototype), value);
}

export function prettyPrintObject(object: object): string {
let res: string[] = [];
export function prettyPrintObject(object: object, depth: number = 0): string {
const res: string[] = [];
for (const i in object) {
res.push(i + ': ' + stringifyValueWithType((object as any)[i]));
res.push(i + ': ' + stringifyValueWithType((object as any)[i], depth + 1));
}
return '{' + res.join(',') + '}';
}
Expand Down

0 comments on commit 56fbef9

Please sign in to comment.