(a: any, b: any, depth = 0)
| 286 | */ |
| 287 | export function replaceEqualDeep<T>(a: unknown, b: T, depth?: number): T |
| 288 | export 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. |
no test coverage detected