All files / utils cloneDeepFast.ts

96.55% Statements 28/29
96.15% Branches 25/26
100% Functions 2/2
100% Lines 24/24

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                                          10930x 10928x 10926x 5464x 5464x 11368x 11368x 11368x 5951x 5417x 6x   5411x     5464x       5462x 5462x 5462x 5501x 5501x 5501x 79x 5422x 6x   5416x     5462x    
// @ts-nocheck
// Copyright 2019 "David Mark Clements <david.mark.clements@gmail.com>"
// MIT Licensed
// https://github.com/davidmarkclements/rfdc
// Modified by Jay Fong
/**
 * 深克隆快速版。
 *
 * @param value 要克隆的值
 * @param ignore 忽略的值
 * @returns 返回克隆后的值
 * @example
 * ```typescript
 * cloneDeepFast({ x: [1] })
 * // => { x: [1] }
 * ```
 */
export function cloneDeepFast<T>(
  value: T,
  ignore?: (value: unknown) => boolean | undefined,
): T {
  if (typeof value !== 'object' || value === null) return value
  if (value instanceof Date) return new Date(value)
  if (Array.isArray(value)) return cloneArray(value, ignore)
  const o2 = {}
  for (const k in value) {
    Iif (Object.hasOwnProperty.call(value, k) === false) continue
    const cur = value[k]
    if (typeof cur !== 'object' || cur === null || (ignore && ignore(cur))) {
      o2[k] = cur
    } else if (cur instanceof Date) {
      o2[k] = new Date(cur)
    } else {
      o2[k] = cloneDeepFast(cur, ignore)
    }
  }
  return o2
}
 
function cloneArray(a, ignore) {
  const keys = Object.keys(a)
  const a2 = new Array(keys.length)
  for (let i = 0; i < keys.length; i++) {
    const k = keys[i]
    const cur = a[k]
    if (typeof cur !== 'object' || cur === null || (ignore && ignore(cur))) {
      a2[k] = cur
    } else if (cur instanceof Date) {
      a2[k] = new Date(cur)
    } else {
      a2[k] = cloneDeepFast(cur, ignore)
    }
  }
  return a2
}