Skip to content

Commit 9a58d6c

Browse files
committed
Clone while masking object #66
1 parent 61bbbbb commit 9a58d6c

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/LoggerHelper.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,28 +274,34 @@ export class LoggerHelper {
274274
: `${lineNumber}`;
275275
}
276276

277-
public static traverseObjectRecursively<T>(
277+
public static cloneObjectRecursively<T>(
278278
obj: T,
279-
maskValuesFn: (key: number | string, obj: T) => T,
280-
done: unknown[] = []
279+
maskValuesFn: (key: number | string, value: unknown) => unknown,
280+
done: unknown[] = [],
281+
clonedObject: T = Object.create(Object.getPrototypeOf(obj)) as T
281282
): T {
283+
done.push(obj);
282284
Object.keys(obj).forEach((currentKey: string | number) => {
283285
if (!done.includes(obj[currentKey])) {
284-
done.push(obj[currentKey]);
285-
if (obj[currentKey] != null && typeof obj[currentKey] === "object") {
286-
const maskedObj = maskValuesFn(currentKey, obj);
287-
obj[currentKey] = LoggerHelper.traverseObjectRecursively(
288-
maskedObj[currentKey],
286+
if (obj[currentKey] == null) {
287+
clonedObject[currentKey] = obj[currentKey];
288+
} else if (typeof obj[currentKey] !== "object") {
289+
clonedObject[currentKey] = maskValuesFn(currentKey, obj[currentKey]);
290+
} else {
291+
clonedObject[currentKey] = LoggerHelper.cloneObjectRecursively(
292+
obj[currentKey],
289293
maskValuesFn,
290-
done
294+
done,
295+
clonedObject[currentKey]
291296
);
292-
} else {
293-
obj = maskValuesFn(currentKey, obj);
294297
}
298+
} else {
299+
// cicrular detected: point to itself to make inspect printout [circular]
300+
clonedObject[currentKey] = clonedObject;
295301
}
296302
});
297303

298-
return obj;
304+
return clonedObject as T;
299305
}
300306

301307
public static logObjectMaskValuesOfKeys<T>(
@@ -307,7 +313,7 @@ export class LoggerHelper {
307313
return obj;
308314
}
309315

310-
const maskValuesFn = <T>(key: number | string, obj: T): T => {
316+
const maskValuesFn = (key: number | string, value: unknown): unknown => {
311317
const keysLowerCase: (
312318
| string
313319
| number
@@ -319,11 +325,11 @@ export class LoggerHelper {
319325
typeof key === "string" ? key.toLowerCase() : key
320326
)
321327
) {
322-
obj[key] = maskPlaceholder;
328+
return maskPlaceholder;
323329
}
324-
return obj;
330+
return value;
325331
};
326332

327-
return LoggerHelper.traverseObjectRecursively(obj, maskValuesFn);
333+
return LoggerHelper.cloneObjectRecursively(obj, maskValuesFn);
328334
}
329335
}

src/LoggerWithoutCallSite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,15 +855,15 @@ export class LoggerWithoutCallSite {
855855
return this._maskAny(format(formatParam, ...param));
856856
}
857857

858-
private _maskValuesOfKeys(object: object | null) {
858+
private _maskValuesOfKeys<T>(object: T): T {
859859
return LoggerHelper.logObjectMaskValuesOfKeys(
860860
object,
861861
this.settings.maskValuesOfKeys,
862862
this.settings.maskPlaceholder
863863
);
864864
}
865865

866-
private _maskAny(str: string) {
866+
private _maskAny(str: string): string {
867867
const formattedStr = str;
868868

869869
return this._maskAnyRegExp != null

0 commit comments

Comments
 (0)