diff --git a/src/LoggerHelper.ts b/src/LoggerHelper.ts index 52665e19..d493def5 100644 --- a/src/LoggerHelper.ts +++ b/src/LoggerHelper.ts @@ -155,7 +155,7 @@ export class LoggerHelper { resultStr ); }, str) - : str; + : `${str}`; } private static _stylizeWithColor( @@ -282,29 +282,42 @@ export class LoggerHelper { clonedObject: T = Object.create(Object.getPrototypeOf(obj)) as T ): T { done.push(obj); - Object.getOwnPropertyNames(obj).forEach((currentKey: string | number) => { - if (!done.includes(obj[currentKey])) { - if (obj[currentKey] == null) { - clonedObject[currentKey] = obj[currentKey]; - } else if (typeof obj[currentKey] !== "object") { - clonedObject[currentKey] = - maskValuesFn != null - ? maskValuesFn(currentKey, obj[currentKey]) - : obj[currentKey]; + + // clone array. could potentially be a separate function + if (obj instanceof Date) { + return (new Date(obj) as unknown) as T; + } else if (Array.isArray(obj)) { + return (Object.entries(obj).map(([key, value]) => { + if (value == null || typeof value !== "object") { + return value; } else { - clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively( - obj[currentKey], - maskValuesFn, - done, - clonedObject[currentKey] - ); + return LoggerHelper.cloneObjectRecursively(value, maskValuesFn, done); } - } else { - // cicrular detected: point to itself to make inspect printout [circular] - clonedObject[currentKey] = clonedObject; - } - }); - + }) as unknown) as T; + } else { + Object.getOwnPropertyNames(obj).forEach((currentKey: string | number) => { + if (!done.includes(obj[currentKey])) { + if (obj[currentKey] == null) { + clonedObject[currentKey] = obj[currentKey]; + } else if (typeof obj[currentKey] !== "object") { + clonedObject[currentKey] = + maskValuesFn != null + ? maskValuesFn(currentKey, obj[currentKey]) + : obj[currentKey]; + } else { + clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively( + obj[currentKey], + maskValuesFn, + done, + clonedObject[currentKey] + ); + } + } else { + // cicrular detected: point to itself to make inspect printout [circular] + clonedObject[currentKey] = clonedObject; + } + }); + } return clonedObject as T; } diff --git a/src/LoggerWithoutCallSite.ts b/src/LoggerWithoutCallSite.ts index 356013ef..073cfa0c 100644 --- a/src/LoggerWithoutCallSite.ts +++ b/src/LoggerWithoutCallSite.ts @@ -155,6 +155,13 @@ export class LoggerWithoutCallSite { ...settings, }; + if ( + this.settings.prettyInspectOptions?.colors != null || + this.settings.prettyInspectOptions?.colors === true + ) { + this.settings.prettyInspectOptions.colors = this.settings.colorizePrettyLogs; + } + this._mySettings.name = this._mySettings.name ?? (this._mySettings.setCallerAsLoggerName diff --git a/tests/parameter.test.ts b/tests/parameter.test.ts index a7bd68f4..ffa573ba 100644 --- a/tests/parameter.test.ts +++ b/tests/parameter.test.ts @@ -17,6 +17,7 @@ const logger: Logger = new Logger({ stdErr.push(print); }, }, + colorizePrettyLogs: false, }); describe("Logger: Parameter", () => { @@ -54,4 +55,76 @@ describe("Logger: Parameter", () => { expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); expect(doesLogContain(stdOut, "string")).toBeTruthy(); }); + + test("date", (): void => { + const date = new Date(); + logger.silly(date); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, date.toISOString())).toBeTruthy(); + }); + + test("array", (): void => { + logger.silly([1, 2, 3]); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "1,")).toBeTruthy(); + expect(doesLogContain(stdOut, "2,")).toBeTruthy(); + expect(doesLogContain(stdOut, "3")).toBeTruthy(); + }); + + test("array with objects", (): void => { + logger.silly([{ 1: true }, { 2: true }, { 3: true }]); + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "'1': true")).toBeTruthy(); + }); + + test("object", (): void => { + const obj = { + null: null, + undefined: undefined, + boolean: true, + number: 0, + string: "string", + array: [1, 2, 3], + date: new Date(), + error: new Error(), + object: { + null: null, + undefined: undefined, + boolean: true, + number: 0, + string: "string", + array: [1, 2, 3], + date: new Date(), + error: new Error(), + object: { + null: null, + undefined: undefined, + boolean: true, + number: 0, + string: "string", + array: [1, 2, 3], + date: new Date(), + error: new Error(), + recursive: {}, + }, + }, + }; + obj.object.object.recursive = obj.object; + + logger.silly(obj); + + expect(doesLogContain(stdOut, "SILLY")).toBeTruthy(); + expect(doesLogContain(stdOut, "{")).toBeTruthy(); + expect(doesLogContain(stdOut, "null: null,")).toBeTruthy(); + expect(doesLogContain(stdOut, "undefined: undefined,")).toBeTruthy(); + expect(doesLogContain(stdOut, "boolean: true,")).toBeTruthy(); + expect(doesLogContain(stdOut, "number: 0,")).toBeTruthy(); + expect(doesLogContain(stdOut, "tring: 'string',")).toBeTruthy(); + expect(doesLogContain(stdOut, "array: [")).toBeTruthy(); + expect(doesLogContain(stdOut, "1,")).toBeTruthy(); + expect(doesLogContain(stdOut, "date: ")).toBeTruthy(); + expect(doesLogContain(stdOut, "error: Error {")).toBeTruthy(); + expect(doesLogContain(stdOut, "object: ")).toBeTruthy(); + expect(doesLogContain(stdOut, "recursive: [Circular")).toBeTruthy(); + }); }); diff --git a/tests/pretty.test.ts b/tests/pretty.test.ts index 6016728e..911da67e 100644 --- a/tests/pretty.test.ts +++ b/tests/pretty.test.ts @@ -1,7 +1,6 @@ import "ts-jest"; import { IStd, Logger } from "../src"; import { doesLogContain } from "./helper"; -import exp = require("constants"); const stdOut: string[] = []; const stdErr: string[] = [];