MCPcopy
hub / github.com/TanStack/query / replaceEqualDeep

Function replaceEqualDeep

packages/query-core/src/utils.ts:257–301  ·  view source on GitHub ↗
(a: any, b: any)

Source from the content-addressed store, hash-verified

255 */
256export function replaceEqualDeep<T>(a: unknown, b: T): T
257export function replaceEqualDeep(a: any, b: any): any {
258 if (a === b) {
259 return a
260 }
261
262 const array = isPlainArray(a) && isPlainArray(b)
263
264 if (!array && !(isPlainObject(a) && isPlainObject(b))) return b
265
266 const aItems = array ? a : Object.keys(a)
267 const aSize = aItems.length
268 const bItems = array ? b : Object.keys(b)
269 const bSize = bItems.length
270 const copy: any = array ? new Array(bSize) : {}
271
272 let equalItems = 0
273
274 for (let i = 0; i < bSize; i++) {
275 const key: any = array ? i : bItems[i]
276 const aItem = a[key]
277 const bItem = b[key]
278
279 if (aItem === bItem) {
280 copy[key] = aItem
281 if (array ? i < aSize : hasOwn.call(a, key)) equalItems++
282 continue
283 }
284
285 if (
286 aItem === null ||
287 bItem === null ||
288 typeof aItem !== 'object' ||
289 typeof bItem !== 'object'
290 ) {
291 copy[key] = bItem
292 continue
293 }
294
295 const v = replaceEqualDeep(aItem, bItem)
296 copy[key] = v
297 if (v === aItem) equalItems++
298 }
299
300 return aSize === bSize && equalItems === aSize ? a : copy
301}
302
303/**
304 * Shallow compare objects.

Callers 7

useMutationStateFunction · 0.90
injectMutationStateFunction · 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

Used in the wild real call sites across dependent graphs

searching dependent graphs…