Skip to content

Commit 7a68ade

Browse files
fix(date): Compare dates only using year, month, date
Closes #2484
1 parent 5510b09 commit 7a68ade

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/params/paramTypes.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ export class ParamTypes {
5757
return match ? new Date(match[1], match[2] - 1, match[3]) : undefined;
5858
},
5959
is: (val) => val instanceof Date && !isNaN(val.valueOf()),
60-
equals(a, b) { return this.is(a) && this.is(b) && a.toISOString() === b.toISOString(); },
60+
equals(l, r) {
61+
return ['getFullYear', 'getMonth', 'getDate']
62+
.reduce((acc, fn) => acc && l[fn]() === r[fn](), true)
63+
},
6164
pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
6265
capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/
6366
},

test/paramSpec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {ParamTypes} from "../src/core";
2+
3+
describe('parameters', () => {
4+
let types;
5+
6+
beforeEach(() => types = new ParamTypes());
7+
8+
describe('date type', () => {
9+
let dateType;
10+
beforeEach(() => dateType = types.type("date"));
11+
12+
it('should compare dates', () => {
13+
let date1 = new Date('2010-01-01');
14+
let date2 = new Date('2010-01-01');
15+
16+
let date3 = new Date('2010-02-01');
17+
18+
expect(dateType.equals(date1, date2)).toBeTruthy();
19+
expect(dateType.equals(date1, date3)).toBeFalsy();
20+
});
21+
22+
it('should compare year/month/day only', () => {
23+
let date1 = new Date('2010-01-01');
24+
date1.setHours(1);
25+
let date2 = new Date('2010-01-01');
26+
date2.setHours(2);
27+
let date3 = new Date('2010-02-01');
28+
date3.setHours(3);
29+
30+
// Failing test case for #2484
31+
expect(dateType.equals(date1, date2)).toBeTruthy();
32+
expect(dateType.equals(date1, date3)).toBeFalsy();
33+
});
34+
});
35+
});

0 commit comments

Comments
 (0)