Skip to content

Commit

Permalink
Clone while masking object #66
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Nov 24, 2020
1 parent 61bbbbb commit 9a58d6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
38 changes: 22 additions & 16 deletions src/LoggerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,34 @@ export class LoggerHelper {
: `${lineNumber}`;
}

public static traverseObjectRecursively<T>(
public static cloneObjectRecursively<T>(
obj: T,
maskValuesFn: (key: number | string, obj: T) => T,
done: unknown[] = []
maskValuesFn: (key: number | string, value: unknown) => unknown,
done: unknown[] = [],
clonedObject: T = Object.create(Object.getPrototypeOf(obj)) as T
): T {
done.push(obj);
Object.keys(obj).forEach((currentKey: string | number) => {
if (!done.includes(obj[currentKey])) {
done.push(obj[currentKey]);
if (obj[currentKey] != null && typeof obj[currentKey] === "object") {
const maskedObj = maskValuesFn(currentKey, obj);
obj[currentKey] = LoggerHelper.traverseObjectRecursively(
maskedObj[currentKey],
if (obj[currentKey] == null) {
clonedObject[currentKey] = obj[currentKey];
} else if (typeof obj[currentKey] !== "object") {
clonedObject[currentKey] = maskValuesFn(currentKey, obj[currentKey]);
} else {
clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively(
obj[currentKey],
maskValuesFn,
done
done,
clonedObject[currentKey]
);
} else {
obj = maskValuesFn(currentKey, obj);
}
} else {
// cicrular detected: point to itself to make inspect printout [circular]
clonedObject[currentKey] = clonedObject;
}
});

return obj;
return clonedObject as T;
}

public static logObjectMaskValuesOfKeys<T>(
Expand All @@ -307,7 +313,7 @@ export class LoggerHelper {
return obj;
}

const maskValuesFn = <T>(key: number | string, obj: T): T => {
const maskValuesFn = (key: number | string, value: unknown): unknown => {
const keysLowerCase: (
| string
| number
Expand All @@ -319,11 +325,11 @@ export class LoggerHelper {
typeof key === "string" ? key.toLowerCase() : key
)
) {
obj[key] = maskPlaceholder;
return maskPlaceholder;
}
return obj;
return value;
};

return LoggerHelper.traverseObjectRecursively(obj, maskValuesFn);
return LoggerHelper.cloneObjectRecursively(obj, maskValuesFn);
}
}
4 changes: 2 additions & 2 deletions src/LoggerWithoutCallSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,15 +855,15 @@ export class LoggerWithoutCallSite {
return this._maskAny(format(formatParam, ...param));
}

private _maskValuesOfKeys(object: object | null) {
private _maskValuesOfKeys<T>(object: T): T {
return LoggerHelper.logObjectMaskValuesOfKeys(
object,
this.settings.maskValuesOfKeys,
this.settings.maskPlaceholder
);
}

private _maskAny(str: string) {
private _maskAny(str: string): string {
const formattedStr = str;

return this._maskAnyRegExp != null
Expand Down

0 comments on commit 9a58d6c

Please sign in to comment.