Skip to content

Commit

Permalink
fix(date): Compare dates only using year, month, date
Browse files Browse the repository at this point in the history
Closes #2484
  • Loading branch information
christopherthielen committed Mar 27, 2016
1 parent 5510b09 commit 7a68ade
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/params/paramTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ export class ParamTypes {
return match ? new Date(match[1], match[2] - 1, match[3]) : undefined;
},
is: (val) => val instanceof Date && !isNaN(val.valueOf()),
equals(a, b) { return this.is(a) && this.is(b) && a.toISOString() === b.toISOString(); },
equals(l, r) {
return ['getFullYear', 'getMonth', 'getDate']
.reduce((acc, fn) => acc && l[fn]() === r[fn](), true)
},
pattern: /[0-9]{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2][0-9]|3[0-1])/,
capture: /([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/
},
Expand Down
35 changes: 35 additions & 0 deletions test/paramSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {ParamTypes} from "../src/core";

describe('parameters', () => {
let types;

beforeEach(() => types = new ParamTypes());

describe('date type', () => {
let dateType;
beforeEach(() => dateType = types.type("date"));

it('should compare dates', () => {
let date1 = new Date('2010-01-01');
let date2 = new Date('2010-01-01');

let date3 = new Date('2010-02-01');

expect(dateType.equals(date1, date2)).toBeTruthy();
expect(dateType.equals(date1, date3)).toBeFalsy();
});

it('should compare year/month/day only', () => {
let date1 = new Date('2010-01-01');
date1.setHours(1);
let date2 = new Date('2010-01-01');
date2.setHours(2);
let date3 = new Date('2010-02-01');
date3.setHours(3);

// Failing test case for #2484
expect(dateType.equals(date1, date2)).toBeTruthy();
expect(dateType.equals(date1, date3)).toBeFalsy();
});
});
});

0 comments on commit 7a68ade

Please sign in to comment.