All files / core/src/utilities deepClone.ts

30.76% Statements 4/13
41.66% Branches 5/12
100% Functions 1/1
30.76% Lines 4/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33              2902x       2902x       2902x   2902x                              
/**
 * Deeply clones an object using structuredClone if available, otherwise falls back to a custom implementation.
 *
 * @param obj - The object to be cloned.
 * @returns A deep clone of the input object.
 */
export function deepClone(obj: unknown): unknown {
  Iif (obj === null || typeof obj !== 'object') {
    return obj;
  }
 
  Iif (typeof obj === 'function') {
    return obj; // Return function reference as-is
  }
 
  Eif (typeof structuredClone === 'function') {
    // just return the object
    return obj;
  }
 
  if (Array.isArray(obj)) {
    return obj.map(deepClone);
  } else {
    const clonedObj = {};
    for (const key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        clonedObj[key] = deepClone(obj[key]);
      }
    }
    return clonedObj;
  }
}