diff --git a/.eslintrc.js b/.eslintrc.js index a2b48396..e803a458 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,5 @@ // This is a workaround for https://github.com/eslint/eslint/issues/3458 -require('@rushstack/eslint-config/patch/modern-module-resolution'); +require("@rushstack/eslint-config/patch/modern-module-resolution"); module.exports = { plugins: ["prettier"], @@ -7,7 +7,10 @@ module.exports = { "prettier/prettier": "error", eqeqeq: [2, "smart"], }, - extends: [ "@rushstack/eslint-config/profile/node", "plugin:prettier/recommended"], + extends: [ + "@rushstack/eslint-config/profile/node", + "plugin:prettier/recommended", + ], ignorePatterns: ["node_modules/", "dist/", "tests/", "*.test.ts", "example/"], env: { node: true, diff --git a/src/LoggerHelper.ts b/src/LoggerHelper.ts index 5e5fb4cd..d7b7c39e 100644 --- a/src/LoggerHelper.ts +++ b/src/LoggerHelper.ts @@ -276,18 +276,21 @@ export class LoggerHelper { public static traverseObjectRecursively( obj: T, - fn: (key: number | string, value: unknown) => [number | string, unknown], + maskValuesFn: (key: number | string, obj: T) => T, done: unknown[] = [] ): T { 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 [key, value] = fn(currentKey, obj[currentKey]); - obj[key] = LoggerHelper.traverseObjectRecursively(value, fn, done); + const maskedObj = maskValuesFn(currentKey, obj); + obj[currentKey] = LoggerHelper.traverseObjectRecursively( + maskedObj[currentKey], + maskValuesFn, + done + ); } else { - const [key, value] = fn(currentKey, obj[currentKey]); - obj[key] = value; + obj = maskValuesFn(currentKey, obj); } } }); @@ -304,23 +307,23 @@ export class LoggerHelper { return obj; } - const fn = ( - key: number | string, - value: unknown - ): [number | string, unknown] => { + const maskValuesFn = (key: number | string, obj: T): T => { const keysLowerCase: ( | string | number )[] = keys.map((key: string | number) => typeof key === "string" ? key.toLowerCase() : key ); - return keysLowerCase.includes( - typeof key === "string" ? key.toLowerCase() : key - ) - ? [key, maskPlaceholder] - : [key, value]; + if ( + keysLowerCase.includes( + typeof key === "string" ? key.toLowerCase() : key + ) + ) { + obj[key] = maskPlaceholder; + } + return obj; }; - return LoggerHelper.traverseObjectRecursively(obj, fn); + return LoggerHelper.traverseObjectRecursively(obj, maskValuesFn); } } diff --git a/tests/settings.test.ts b/tests/settings.test.ts index 3291caae..fa23ea62 100644 --- a/tests/settings.test.ts +++ b/tests/settings.test.ts @@ -817,7 +817,9 @@ describe("Logger: settings", () => { stdErr: std, }); - const { argumentsArray: undefinedMaskValuesArgArray } = undefinedMaskValuesLogger.info(verySecretiveCircularObject); + const { + argumentsArray: undefinedMaskValuesArgArray, + } = undefinedMaskValuesLogger.info(verySecretiveCircularObject); expect(undefinedMaskValuesArgArray[0]).toBe(verySecretiveCircularObject); });