MCPcopy Create free account
hub / github.com/TanStack/query / _cloneDeep

Function _cloneDeep

packages/vue-query/src/utils.ts:46–80  ·  view source on GitHub ↗

* Recursive implementation of cloneDeep, which also tracks the key and nesting level of * the current node for `customize`. * @param value - The value to clone. * @param customize - Called for every node with its key and nesting level. If it returns a value * other than `undefined`, that

(
  value: MaybeRefDeep<T>,
  customize?: (
    val: MaybeRefDeep<T>,
    key: string,
    level: number,
  ) => T | undefined,
  currentKey: string = '',
  currentLevel: number = 0,
)

Source from the content-addressed store, hash-verified

44 * @returns The cloned value.
45 */
46function _cloneDeep<T>(
47 value: MaybeRefDeep<T>,
48 customize?: (
49 val: MaybeRefDeep<T>,
50 key: string,
51 level: number,
52 ) => T | undefined,
53 currentKey: string = '',
54 currentLevel: number = 0,
55): T {
56 if (customize) {
57 const result = customize(value, currentKey, currentLevel)
58 if (result === undefined && isRef(value)) {
59 return result as T
60 }
61 if (result !== undefined) {
62 return result
63 }
64 }
65
66 if (Array.isArray(value)) {
67 return value.map((val, index) =>
68 _cloneDeep(val, customize, String(index), currentLevel + 1),
69 ) as unknown as T
70 }
71
72 if (typeof value === 'object' && isPlainObject(value)) {
73 const entries = Object.entries(value as Record<string, any>).map(
74 ([key, val]) => [key, _cloneDeep(val, customize, key, currentLevel + 1)],
75 )
76 return Object.fromEntries(entries)
77 }
78
79 return value as T
80}
81
82/**
83 * Deep-clones `value`, recursing into arrays and plain objects. `customize`, if provided, can

Callers 1

cloneDeepFunction · 0.85

Calls 1

isPlainObjectFunction · 0.70

Tested by

no test coverage detected