mirror of https://github.com/lukechilds/ow.git
Sam Verschueren
7 years ago
committed by
Sindre Sorhus
3 changed files with 58 additions and 0 deletions
@ -0,0 +1,31 @@ |
|||
import {Predicate, Context} from './predicate'; |
|||
|
|||
export class DatePredicate extends Predicate<Date> { |
|||
constructor(context?: Context) { |
|||
super('date', context); |
|||
} |
|||
|
|||
/** |
|||
* Test a date to be before another date. |
|||
* |
|||
* @param date Maximum value. |
|||
*/ |
|||
before(date: Date) { |
|||
return this.addValidator({ |
|||
message: value => `Expected ${value.toISOString()} to be before ${date.toISOString()}`, |
|||
validator: value => value.getTime() < date.getTime() |
|||
}); |
|||
} |
|||
|
|||
/** |
|||
* Test a date to be before another date. |
|||
* |
|||
* @param date Minimum value. |
|||
*/ |
|||
after(date: Date) { |
|||
return this.addValidator({ |
|||
message: value => `Expected ${value.toISOString()} to be after ${date.toISOString()}`, |
|||
validator: value => value.getTime() > date.getTime() |
|||
}); |
|||
} |
|||
} |
@ -0,0 +1,19 @@ |
|||
import test from 'ava'; |
|||
import m from '..'; |
|||
|
|||
test('date', t => { |
|||
t.notThrows(() => m(new Date(), m.date)); |
|||
t.throws(() => m('12', m.date), 'Expected argument to be of type `date` but received type `string`'); |
|||
}); |
|||
|
|||
test('date.before', t => { |
|||
t.notThrows(() => m(new Date('2017-11-25'), m.date.before(new Date('2017-11-26')))); |
|||
t.notThrows(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:01Z')))); |
|||
t.throws(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:00Z'))), 'Expected 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z'); |
|||
}); |
|||
|
|||
test('date.after', t => { |
|||
t.notThrows(() => m(new Date('2017-11-26'), m.date.after(new Date('2017-11-25')))); |
|||
t.notThrows(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T11:59:59Z')))); |
|||
t.throws(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T12:00:00Z'))), 'Expected 2017-11-26T12:00:00.000Z to be after 2017-11-26T12:00:00.000Z'); |
|||
}); |
Loading…
Reference in new issue