Skip to content

Commit c0cc9a6

Browse files
author
Joao Madeiras
committed
Add support for numeric timestamps on
1 parent 8eabd63 commit c0cc9a6

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/asserts/date-diff-less-than-assert.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,32 @@ export default function dateDiffLessThanAssert(threshold, options) {
5353
*/
5454

5555
this.validate = value => {
56-
if (typeof value !== 'string' && Object.prototype.toString.call(value) !== '[object Date]') {
57-
throw new Violation(this, value, { value: 'must_be_a_date_or_a_string' });
56+
let date = value;
57+
58+
if (typeof date === 'number') {
59+
date = new Date(date);
60+
61+
if (date.toString() === 'Invalid Date') {
62+
throw new Violation(this, date);
63+
}
64+
}
65+
66+
if (typeof date !== 'string' && Object.prototype.toString.call(date) !== '[object Date]') {
67+
throw new Violation(this, date, { value: 'must_be_a_date_or_a_string_or_a_number' });
5868
}
5969

60-
if (isNaN(Date.parse(value)) === true) {
61-
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
70+
if (isNaN(Date.parse(date)) === true) {
71+
throw new Violation(this, date, { absolute: this.absolute, asFloat: this.asFloat, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
6272
}
6373

64-
let diff = moment(this.fromDate || Date.now()).diff(value, this.unit, this.asFloat);
74+
let diff = moment(this.fromDate || Date.now()).diff(date, this.unit, this.asFloat);
6575

6676
if (this.absolute) {
6777
diff = Math.abs(diff);
6878
}
6979

7080
if (diff >= this.threshold) {
71-
throw new Violation(this, value, { absolute: this.absolute, asFloat: this.asFloat, diff, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
81+
throw new Violation(this, date, { absolute: this.absolute, asFloat: this.asFloat, diff, fromDate: this.fromDate, threshold: this.threshold, unit: this.unit });
7282
}
7383

7484
return true;

test/asserts/date-diff-less-than-assert_test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('DateDiffLessThanAssert', () => {
6565
should.fail();
6666
} catch (e) {
6767
e.should.be.instanceOf(Violation);
68-
e.violation.value.should.equal('must_be_a_date_or_a_string');
68+
e.violation.value.should.equal('must_be_a_date_or_a_string_or_a_number');
6969
}
7070
});
7171
});
@@ -81,6 +81,17 @@ describe('DateDiffLessThanAssert', () => {
8181
}
8282
});
8383

84+
it('should throw an error if the input value is an invalid timestamp', () => {
85+
try {
86+
new Assert().DateDiffLessThan(10).validate(-Number.MAX_VALUE);
87+
88+
should.fail();
89+
} catch (e) {
90+
e.should.be.instanceOf(Violation);
91+
e.show().assert.should.equal('DateDiffLessThan');
92+
}
93+
});
94+
8495
it('should throw an error if the diff between `now` and input date is equal to `threshold`', () => {
8596
const clock = sinon.useFakeTimers(0, 'Date');
8697

0 commit comments

Comments
 (0)