Skip to content

Commit

Permalink
fix(shallow): fallback map-like iterator comparison (#2795)
Browse files Browse the repository at this point in the history
* add failing test

* add a fallback
  • Loading branch information
dai-shi authored Oct 27, 2024
1 parent f9b5538 commit 259c19f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/vanilla/shallow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ export function shallow<T>(objA: T, objB: T): boolean {
nextA.value.length === 2 &&
nextB.value.length === 2
) {
return compareMapLike(
objA as Iterable<[unknown, unknown]>,
objB as Iterable<[unknown, unknown]>,
)
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)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/vanilla/shallow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 259c19f

Please sign in to comment.