From d6737b53f29c9e0ff995c7d6b14eb6d122be9b52 Mon Sep 17 00:00:00 2001 From: mertsincan Date: Tue, 12 Dec 2023 14:22:19 +0000 Subject: [PATCH] Added `stringify` function to ObjectUtils --- components/lib/utils/ObjectUtils.js | 24 ++++++++++++++++++++++++ components/lib/utils/Utils.d.ts | 1 + 2 files changed, 25 insertions(+) diff --git a/components/lib/utils/ObjectUtils.js b/components/lib/utils/ObjectUtils.js index d8fe31173..e8e23f106 100755 --- a/components/lib/utils/ObjectUtils.js +++ b/components/lib/utils/ObjectUtils.js @@ -341,5 +341,29 @@ export default { return o; }, []); + }, + + stringify(value, indent = 2, currentIndent = 0) { + const currentIndentStr = ' '.repeat(currentIndent); + const nextIndentStr = ' '.repeat(currentIndent + indent); + + if (this.isArray(value)) { + return '[' + value.map((v) => this.stringify(v, indent, currentIndent + indent)).join(', ') + ']'; + } else if (this.isDate(value)) { + return value.toISOString(); + } else if (this.isFunction(value)) { + return value.toString(); + } else if (this.isObject(value)) { + return ( + '{\n' + + Object.entries(value) + .map(([k, v]) => `${nextIndentStr}${k}: ${this.stringify(v, indent, currentIndent + indent)}`) + .join(',\n') + + `\n${currentIndentStr}` + + '}' + ); + } else { + return JSON.stringify(value); + } } }; diff --git a/components/lib/utils/Utils.d.ts b/components/lib/utils/Utils.d.ts index 2e8d9e613..72af49664 100644 --- a/components/lib/utils/Utils.d.ts +++ b/components/lib/utils/Utils.d.ts @@ -99,6 +99,7 @@ export declare class ObjectUtils { static sort(value1: any, value2: any, order: number, comparator: (a: any, b: any) => any, nullSortOrder: number): number; static compare(value1: any, value2: any, comparator: (a: any, b: any) => any, order: number): number; static nestedKeys(obj: object, parentKey?: string): string[]; + static stringify(value: any, indent?: number, currentIndent?: number): string; } export declare namespace ZIndexUtils {