fix(shallow): fallback map-like iterator comparison (#2795)

* add failing test

* add a fallback
This commit is contained in:
Daishi Kato 2024-10-27 16:38:59 +09:00 committed by GitHub
parent f9b5538402
commit 259c19ff2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 4 deletions

View File

@ -40,10 +40,14 @@ export function shallow<T>(objA: T, objB: T): boolean {
nextA.value.length === 2 &&
nextB.value.length === 2
) {
try {
return compareMapLike(
objA as Iterable<[unknown, unknown]>,
objB as Iterable<[unknown, unknown]>,
)
} catch {
// fallback
}
}
while (!nextA.done && !nextB.done) {
if (!Object.is(nextA.value, nextB.value)) {

View File

@ -97,6 +97,11 @@ describe('shallow', () => {
const b = new URLSearchParams({ zustand: 'shallow' })
expect(shallow(a, b)).toBe(false)
})
it('should work with nested arrays (#2794)', () => {
const arr = [1, 2]
expect(shallow([arr, 1], [arr, 1])).toBe(true)
})
})
describe('unsupported cases', () => {