Skip to content

Commit da2f327

Browse files
YoanChapronjohnleider
authored andcommitted
Add Date comparison in deepEqual function (#4638)
1 parent 43a18b6 commit da2f327

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/util/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ export function getNestedValue (obj: any, path: (string | number)[], fallback?:
130130
export function deepEqual (a: any, b: any): boolean {
131131
if (a === b) return true
132132

133+
if (a instanceof Date && b instanceof Date) {
134+
// If the values are Date, they were convert to timestamp with getTime and compare it
135+
if (a.getTime() !== b.getTime()) return false
136+
}
137+
133138
if (a !== Object(a) || b !== Object(b)) {
134139
// If the values aren't objects, they were already checked for equality
135140
return false

test/unit/util/helpers.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ test('helpers.js', () => {
8484
expect(deepEqual({x: 1}, {})).toEqual(false)
8585
expect(deepEqual({x: {a: 1, b: 2}}, {x: {a: 1, b: 2}})).toEqual(true)
8686

87+
// Date
88+
const currentDate = new Date
89+
const futureDate = new Date(1000)
90+
91+
expect(deepEqual(currentDate, currentDate)).toEqual(true)
92+
expect(deepEqual({date: currentDate}, {date: currentDate})).toEqual(true)
93+
expect(deepEqual(currentDate, futureDate)).toEqual(false)
94+
expect(deepEqual({date: currentDate}, {date: futureDate})).toEqual(false)
95+
8796
const circular = {}
8897
circular.me = circular
8998

0 commit comments

Comments
 (0)