Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .changeset/deep-equal-date-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix(utils): compare Date values by time in deepEqualObj
13 changes: 13 additions & 0 deletions packages/core/utils/src/common/__tests__/deep-equal-obj.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ describe("deepEqualObj", function () {

expect(deepEqualObj(object1, object2)).toBe(false)
})

it("should compare Date values by time, not by their (empty) keys", function () {
expect(deepEqualObj(new Date(0), new Date(0))).toBe(true)
expect(deepEqualObj(new Date(0), new Date(999999))).toBe(false)
expect(
deepEqualObj({ created_at: new Date(0) }, { created_at: new Date(0) })
).toBe(true)
expect(
deepEqualObj({ created_at: new Date(0) }, { created_at: new Date(999999) })
).toBe(false)
// a Date is not equal to a plain object
expect(deepEqualObj(new Date(0), {})).toBe(false)
})
})
10 changes: 10 additions & 0 deletions packages/core/utils/src/common/deep-equal-obj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ export function deepEqualObj(obj1: unknown, obj2: unknown): boolean {
return obj2 === obj1
}

// Dates expose no own enumerable keys, so the key comparison below would treat
// any two Date instances as equal; compare their time value instead.
if (obj1 instanceof Date || obj2 instanceof Date) {
return (
obj1 instanceof Date &&
obj2 instanceof Date &&
obj1.getTime() === obj2.getTime()
)
}

const obj1Keys = Object.keys(obj1)
const obj2Keys = Object.keys(obj2)

Expand Down
Loading