All files / src/model value-cloner.ts

96.96% Statements 32/33
96.42% Branches 27/28
100% Functions 4/4
96.87% Lines 31/32

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75    132x   132x                 132x                   461x 461x       4407x 2237x   2170x 3x   2167x 2167x 1179x   988x 171x   817x 817x           171x 171x 171x 213x 211x 211x     211x     171x       816x 816x 3735x 3735x 38x   3697x   778x      
import { ValuesWriter } from './values-writer.ts';
 
const MAX_DEPTH = 32;
/** Marks a value the fast copy does not handle. */
const UNSUPPORTED = Symbol('postgrelize.unsupported');
 
/**
 * Deep copy of attribute values for `toJSON`. Rows hold primitives, dates, arrays and plain JSON
 * objects, which are copied directly; `structuredClone` is the slow general path, so it is used
 * for the whole value as soon as something else shows up (functions, symbols, class instances,
 * typed arrays, `Map`, cycles or an object reachable twice), which keeps its exact semantics.
 */
export class ValueCloner {
  readonly #writer = new ValuesWriter();
 
  /**
   * Copies a value like `structuredClone` does.
   *
   * @param value - Value to copy.
   * @returns The copy.
   * @throws DataCloneError for values `structuredClone` rejects (functions, symbols).
   */
  clone<T>(value: T): T {
    const copy = this.#copy(value, new Set(), 0);
    return copy === UNSUPPORTED ? structuredClone(value) : (copy as T);
  }
 
  #copy(value: unknown, seen: Set<object>, depth: number): unknown {
    if (value === null || typeof value !== 'object') {
      return typeof value === 'function' || typeof value === 'symbol' ? UNSUPPORTED : value;
    }
    if (depth > MAX_DEPTH || seen.has(value)) {
      return UNSUPPORTED;
    }
    seen.add(value);
    if (value instanceof Date) {
      return new Date(value.getTime());
    }
    if (Array.isArray(value)) {
      return this.#array(value, seen, depth);
    }
    const prototype = Object.getPrototypeOf(value) as unknown;
    return prototype === Object.prototype || prototype === null
      ? this.#object(value, seen, depth)
      : UNSUPPORTED;
  }
 
  #array(value: readonly unknown[], seen: Set<object>, depth: number): unknown {
    const copy: unknown[] = [];
    copy.length = value.length;
    for (let index = 0; index < value.length; index += 1) {
      if (index in value) {
        const item = this.#copy(value[index], seen, depth + 1);
        Iif (item === UNSUPPORTED) {
          return UNSUPPORTED;
        }
        copy[index] = item;
      }
    }
    return copy;
  }
 
  #object(value: object, seen: Set<object>, depth: number): unknown {
    const copy: Record<string, unknown> = {};
    for (const [key, item] of Object.entries(value)) {
      const itemCopy = this.#copy(item, seen, depth + 1);
      if (itemCopy === UNSUPPORTED) {
        return UNSUPPORTED;
      }
      this.#writer.set(copy, key, itemCopy);
    }
    return copy;
  }
}