Skip to content

Commit b3901d2

Browse files
fubhytim-smart
andauthored
Add reproduction for Equal issue (#6874)
Co-authored-by: Tim Smart <[email protected]>
1 parent 4a0984a commit b3901d2

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎.changeset/calm-dates-view.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix `Equal.equals` and `Hash.hash` to handle invalid dates and `DataView` values without throwing.

‎packages/effect/src/Equal.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ function compareObjects(self: object, that: object): boolean {
233233
return false
234234
} else if (self instanceof Date) {
235235
if (!(that instanceof Date)) return false
236-
return self.toISOString() === that.toISOString()
236+
const selfTime = self.getTime()
237+
const thatTime = that.getTime()
238+
return selfTime === thatTime || (Number.isNaN(selfTime) && Number.isNaN(thatTime))
237239
} else if (self instanceof RegExp) {
238240
if (!(that instanceof RegExp)) return false
239241
return self.toString() === that.toString()
@@ -254,9 +256,21 @@ function compareObjects(self: object, that: object): boolean {
254256
}
255257
return compareArrays(self, that)
256258
} else if (ArrayBuffer.isView(self)) {
257-
if (!ArrayBuffer.isView(that) || self.byteLength !== that.byteLength) {
259+
const selfIsDataView = self instanceof DataView
260+
if (
261+
!ArrayBuffer.isView(that) ||
262+
self.byteLength !== that.byteLength ||
263+
selfIsDataView !== (that instanceof DataView)
264+
) {
258265
return false
259266
}
267+
if (selfIsDataView) {
268+
const thatDataView = that as DataView
269+
return compareTypedArrays(
270+
new Uint8Array(self.buffer, self.byteOffset, self.byteLength),
271+
new Uint8Array(thatDataView.buffer, thatDataView.byteOffset, thatDataView.byteLength)
272+
)
273+
}
260274
return compareTypedArrays(self as Uint8Array, that as Uint8Array)
261275
} else if (self instanceof Map) {
262276
if (!(that instanceof Map) || self.size !== that.size) {

‎packages/effect/src/Hash.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ export const hash: <A>(self: A) => number = <A>(self: A) => {
119119
if (self === null) {
120120
return string("null")
121121
} else if (self instanceof Date) {
122+
if (Number.isNaN(self.getTime())) {
123+
return string("Invalid Date")
124+
}
122125
return string(self.toISOString())
123126
} else if (self instanceof RegExp) {
124127
return string(self.toString())
@@ -134,6 +137,8 @@ export const hash: <A>(self: A) => number = <A>(self: A) => {
134137
return self[symbol]()
135138
} else if (typeof self === "function") {
136139
return random(self)
140+
} else if (self instanceof DataView) {
141+
return array(new Uint8Array(self.buffer, self.byteOffset, self.byteLength))
137142
} else if (Array.isArray(self) || ArrayBuffer.isView(self)) {
138143
return array(self as any)
139144
} else if (self instanceof Map) {

‎packages/effect/test/Equal.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,18 @@ describe("Equal.equals", () => {
289289
const date2 = new Date("2023-01-02T00:00:00.000Z")
290290
expect(Equal.equals(date1, date2)).toBe(false)
291291
})
292+
293+
it("should compare invalid dates without throwing", () => {
294+
expect(Equal.equals(new Date(NaN), new Date(NaN))).toBe(true)
295+
})
296+
})
297+
298+
describe("DataView objects", () => {
299+
it("should compare viewed bytes", () => {
300+
const self = new DataView(Uint8Array.of(1).buffer)
301+
const that = new DataView(Uint8Array.of(2).buffer)
302+
expect(Equal.equals(self, that)).toBe(false)
303+
})
292304
})
293305

294306
describe("Effect data structures", () => {

0 commit comments

Comments
 (0)