Skip to content

feat(shallow): add Date object comparison support #201

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 18 additions & 5 deletions packages/react-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export function shallow<T>(objA: T, objB: T) {
if (objA instanceof Map && objB instanceof Map) {
if (objA.size !== objB.size) return false
for (const [k, v] of objA) {
if (!objB.has(k) || !Object.is(v, objB.get(k))) return false
const v2 = objB.get(k)
if (v instanceof Date && v2 instanceof Date) {
if (v.getTime() !== v2.getTime()) return false
} else if (!objB.has(k) || !Object.is(v, v2)) {
return false
}
}
return true
}
Expand All @@ -67,12 +72,20 @@ export function shallow<T>(objA: T, objB: T) {
}

for (let i = 0; i < keysA.length; i++) {
if (
!Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
!Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
) {
const key = keysA[i] as keyof T
const valA = objA[key]
const valB = objB[key]

if (!Object.prototype.hasOwnProperty.call(objB, key as string)) {
return false
}

if (valA instanceof Date && valB instanceof Date) {
if (valA.getTime() !== valB.getTime()) return false
} else if (!Object.is(valA, valB)) {
return false
}
}

return true
}