Skip to content

Commit

Permalink
Merge pull request #84 from fullstack-build/development
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
terehov authored Jan 20, 2021
2 parents 8d41494 + 4560d7c commit 9fee6e3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 23 deletions.
57 changes: 35 additions & 22 deletions src/LoggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class LoggerHelper {
resultStr
);
}, str)
: str;
: `${str}`;
}

private static _stylizeWithColor<T>(
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions src/LoggerWithoutCallSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions tests/parameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const logger: Logger = new Logger({
stdErr.push(print);
},
},
colorizePrettyLogs: false,
});

describe("Logger: Parameter", () => {
Expand Down Expand Up @@ -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();
});
});
1 change: 0 additions & 1 deletion tests/pretty.test.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];
Expand Down

0 comments on commit 9fee6e3

Please sign in to comment.