Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(react): date compare in shallow equal function #164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/react-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export function shallow<T>(objA: T, objB: T) {
return true
}

if (objA instanceof Date && objB instanceof Date) {
if (objA.getTime() !== objB.getTime()) return false
return true
}

const keysA = Object.keys(objA)
if (keysA.length !== Object.keys(objB).length) {
return false
Expand Down
12 changes: 12 additions & 0 deletions packages/react-store/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,16 @@ describe('shallow', () => {
const objB = new Set([2])
expect(shallow(objA, objB)).toBe(false)
})

test('should return false for dates with different values', () => {
const objA = new Date('2025-04-10T14:48:00')
const objB = new Date('2025-04-10T14:58:00')
expect(shallow(objA, objB)).toBe(false)
})

test('should return true for equal dates', () => {
const objA = new Date('2025-02-10')
const objB = new Date('2025-02-10')
expect(shallow(objA, objB)).toBe(true)
})
})