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

Function replaceEqualDeep

packages/query-core/src/utils.ts:288–334  ·  view source on GitHub ↗
(a: any, b: any, depth = 0)

Source from the content-addressed store, hash-verified

286 */
287export function replaceEqualDeep<T>(a: unknown, b: T, depth?: number): T
288export function replaceEqualDeep(a: any, b: any, depth = 0): any {
289 if (a === b) {
290 return a
291 }
292
293 if (depth > 500) return b
294
295 const array = isPlainArray(a) && isPlainArray(b)
296
297 if (!array && !(isPlainObject(a) && isPlainObject(b))) return b
298
299 const aItems = array ? a : Object.keys(a)
300 const aSize = aItems.length
301 const bItems = array ? b : Object.keys(b)
302 const bSize = bItems.length
303 const copy: any = array ? new Array(bSize) : {}
304
305 let equalItems = 0
306
307 for (let i = 0; i < bSize; i++) {
308 const key: any = array ? i : bItems[i]
309 const aItem = a[key]
310 const bItem = b[key]
311
312 if (aItem === bItem) {
313 copy[key] = aItem
314 if (array ? i < aSize : hasOwn.call(a, key)) equalItems++
315 continue
316 }
317
318 if (
319 aItem === null ||
320 bItem === null ||
321 typeof aItem !== 'object' ||
322 typeof bItem !== 'object'
323 ) {
324 copy[key] = bItem
325 continue
326 }
327
328 const v = replaceEqualDeep(aItem, bItem, depth + 1)
329 copy[key] = v
330 if (v === aItem) equalItems++
331 }
332
333 return aSize === bSize && equalItems === aSize ? a : copy
334}
335
336/**
337 * Shallow compare objects.

Callers 10

createResultMethod · 0.90
setMutationStateMethod · 0.90
useMutationStateFunction · 0.90
injectMutationStateFunction · 0.90
useMutationStateFunction · 0.90
useMutationStateFunction · 0.90
useMutationStateFunction · 0.90
#combineResultMethod · 0.90
utils.test.tsxFile · 0.90
replaceDataFunction · 0.85

Calls 2

isPlainArrayFunction · 0.85
isPlainObjectFunction · 0.70

Tested by

no test coverage detected