Skip to content

Commit 9fee6e3

Browse files
authored
Merge pull request #84 from fullstack-build/development
Bugfixes
2 parents 8d41494 + 4560d7c commit 9fee6e3

File tree

4 files changed

+115
-23
lines changed

4 files changed

+115
-23
lines changed

src/LoggerHelper.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class LoggerHelper {
155155
resultStr
156156
);
157157
}, str)
158-
: str;
158+
: `${str}`;
159159
}
160160

161161
private static _stylizeWithColor<T>(
@@ -282,29 +282,42 @@ export class LoggerHelper {
282282
clonedObject: T = Object.create(Object.getPrototypeOf(obj)) as T
283283
): T {
284284
done.push(obj);
285-
Object.getOwnPropertyNames(obj).forEach((currentKey: string | number) => {
286-
if (!done.includes(obj[currentKey])) {
287-
if (obj[currentKey] == null) {
288-
clonedObject[currentKey] = obj[currentKey];
289-
} else if (typeof obj[currentKey] !== "object") {
290-
clonedObject[currentKey] =
291-
maskValuesFn != null
292-
? maskValuesFn(currentKey, obj[currentKey])
293-
: obj[currentKey];
285+
286+
// clone array. could potentially be a separate function
287+
if (obj instanceof Date) {
288+
return (new Date(obj) as unknown) as T;
289+
} else if (Array.isArray(obj)) {
290+
return (Object.entries(obj).map(([key, value]) => {
291+
if (value == null || typeof value !== "object") {
292+
return value;
294293
} else {
295-
clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively(
296-
obj[currentKey],
297-
maskValuesFn,
298-
done,
299-
clonedObject[currentKey]
300-
);
294+
return LoggerHelper.cloneObjectRecursively(value, maskValuesFn, done);
301295
}
302-
} else {
303-
// cicrular detected: point to itself to make inspect printout [circular]
304-
clonedObject[currentKey] = clonedObject;
305-
}
306-
});
307-
296+
}) as unknown) as T;
297+
} else {
298+
Object.getOwnPropertyNames(obj).forEach((currentKey: string | number) => {
299+
if (!done.includes(obj[currentKey])) {
300+
if (obj[currentKey] == null) {
301+
clonedObject[currentKey] = obj[currentKey];
302+
} else if (typeof obj[currentKey] !== "object") {
303+
clonedObject[currentKey] =
304+
maskValuesFn != null
305+
? maskValuesFn(currentKey, obj[currentKey])
306+
: obj[currentKey];
307+
} else {
308+
clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively(
309+
obj[currentKey],
310+
maskValuesFn,
311+
done,
312+
clonedObject[currentKey]
313+
);
314+
}
315+
} else {
316+
// cicrular detected: point to itself to make inspect printout [circular]
317+
clonedObject[currentKey] = clonedObject;
318+
}
319+
});
320+
}
308321
return clonedObject as T;
309322
}
310323

src/LoggerWithoutCallSite.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ export class LoggerWithoutCallSite {
155155
...settings,
156156
};
157157

158+
if (
159+
this.settings.prettyInspectOptions?.colors != null ||
160+
this.settings.prettyInspectOptions?.colors === true
161+
) {
162+
this.settings.prettyInspectOptions.colors = this.settings.colorizePrettyLogs;
163+
}
164+
158165
this._mySettings.name =
159166
this._mySettings.name ??
160167
(this._mySettings.setCallerAsLoggerName

tests/parameter.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const logger: Logger = new Logger({
1717
stdErr.push(print);
1818
},
1919
},
20+
colorizePrettyLogs: false,
2021
});
2122

2223
describe("Logger: Parameter", () => {
@@ -54,4 +55,76 @@ describe("Logger: Parameter", () => {
5455
expect(doesLogContain(stdOut, "SILLY")).toBeTruthy();
5556
expect(doesLogContain(stdOut, "string")).toBeTruthy();
5657
});
58+
59+
test("date", (): void => {
60+
const date = new Date();
61+
logger.silly(date);
62+
expect(doesLogContain(stdOut, "SILLY")).toBeTruthy();
63+
expect(doesLogContain(stdOut, date.toISOString())).toBeTruthy();
64+
});
65+
66+
test("array", (): void => {
67+
logger.silly([1, 2, 3]);
68+
expect(doesLogContain(stdOut, "SILLY")).toBeTruthy();
69+
expect(doesLogContain(stdOut, "1,")).toBeTruthy();
70+
expect(doesLogContain(stdOut, "2,")).toBeTruthy();
71+
expect(doesLogContain(stdOut, "3")).toBeTruthy();
72+
});
73+
74+
test("array with objects", (): void => {
75+
logger.silly([{ 1: true }, { 2: true }, { 3: true }]);
76+
expect(doesLogContain(stdOut, "SILLY")).toBeTruthy();
77+
expect(doesLogContain(stdOut, "'1': true")).toBeTruthy();
78+
});
79+
80+
test("object", (): void => {
81+
const obj = {
82+
null: null,
83+
undefined: undefined,
84+
boolean: true,
85+
number: 0,
86+
string: "string",
87+
array: [1, 2, 3],
88+
date: new Date(),
89+
error: new Error(),
90+
object: {
91+
null: null,
92+
undefined: undefined,
93+
boolean: true,
94+
number: 0,
95+
string: "string",
96+
array: [1, 2, 3],
97+
date: new Date(),
98+
error: new Error(),
99+
object: {
100+
null: null,
101+
undefined: undefined,
102+
boolean: true,
103+
number: 0,
104+
string: "string",
105+
array: [1, 2, 3],
106+
date: new Date(),
107+
error: new Error(),
108+
recursive: {},
109+
},
110+
},
111+
};
112+
obj.object.object.recursive = obj.object;
113+
114+
logger.silly(obj);
115+
116+
expect(doesLogContain(stdOut, "SILLY")).toBeTruthy();
117+
expect(doesLogContain(stdOut, "{")).toBeTruthy();
118+
expect(doesLogContain(stdOut, "null: null,")).toBeTruthy();
119+
expect(doesLogContain(stdOut, "undefined: undefined,")).toBeTruthy();
120+
expect(doesLogContain(stdOut, "boolean: true,")).toBeTruthy();
121+
expect(doesLogContain(stdOut, "number: 0,")).toBeTruthy();
122+
expect(doesLogContain(stdOut, "tring: 'string',")).toBeTruthy();
123+
expect(doesLogContain(stdOut, "array: [")).toBeTruthy();
124+
expect(doesLogContain(stdOut, "1,")).toBeTruthy();
125+
expect(doesLogContain(stdOut, "date: ")).toBeTruthy();
126+
expect(doesLogContain(stdOut, "error: Error {")).toBeTruthy();
127+
expect(doesLogContain(stdOut, "object: ")).toBeTruthy();
128+
expect(doesLogContain(stdOut, "recursive: [Circular")).toBeTruthy();
129+
});
57130
});

tests/pretty.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import "ts-jest";
22
import { IStd, Logger } from "../src";
33
import { doesLogContain } from "./helper";
4-
import exp = require("constants");
54

65
const stdOut: string[] = [];
76
const stdErr: string[] = [];

0 commit comments

Comments
 (0)