@@ -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}
0 commit comments