mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
fix(shallow): fallback map-like iterator comparison (#2795)
* add failing test * add a fallback
This commit is contained in:
parent
f9b5538402
commit
259c19ff2d
@ -40,10 +40,14 @@ export function shallow<T>(objA: T, objB: T): boolean {
|
|||||||
nextA.value.length === 2 &&
|
nextA.value.length === 2 &&
|
||||||
nextB.value.length === 2
|
nextB.value.length === 2
|
||||||
) {
|
) {
|
||||||
return compareMapLike(
|
try {
|
||||||
objA as Iterable<[unknown, unknown]>,
|
return compareMapLike(
|
||||||
objB as Iterable<[unknown, unknown]>,
|
objA as Iterable<[unknown, unknown]>,
|
||||||
)
|
objB as Iterable<[unknown, unknown]>,
|
||||||
|
)
|
||||||
|
} catch {
|
||||||
|
// fallback
|
||||||
|
}
|
||||||
}
|
}
|
||||||
while (!nextA.done && !nextB.done) {
|
while (!nextA.done && !nextB.done) {
|
||||||
if (!Object.is(nextA.value, nextB.value)) {
|
if (!Object.is(nextA.value, nextB.value)) {
|
||||||
|
|||||||
@ -97,6 +97,11 @@ describe('shallow', () => {
|
|||||||
const b = new URLSearchParams({ zustand: 'shallow' })
|
const b = new URLSearchParams({ zustand: 'shallow' })
|
||||||
expect(shallow(a, b)).toBe(false)
|
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', () => {
|
describe('unsupported cases', () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user