Skip to content

Commit

Permalink
Add date predicate (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Nov 25, 2017
1 parent b26de41 commit f2be585
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {StringPredicate} from './lib/predicates/string';
import {NumberPredicate} from './lib/predicates/number';
import {BooleanPredicate} from './lib/predicates/boolean';
import {ArrayPredicate} from './lib/predicates/array';
import {DatePredicate} from './lib/predicates/date';

export interface Ow {
(value: any, predicate: Predicate): void;
Expand All @@ -23,6 +24,10 @@ export interface Ow {
* Test the value to be an array.
*/
array: ArrayPredicate;
/**
* Test the value to be a Date.
*/
date: DatePredicate;
}

const main = (value: any, predicate: Predicate) => {
Expand All @@ -46,6 +51,9 @@ Object.defineProperties(main, {
},
array: {
get: () => new ArrayPredicate()
},
date: {
get: () => new DatePredicate()
}
});

Expand Down
31 changes: 31 additions & 0 deletions source/lib/predicates/date.ts
Original file line number Diff line number Diff line change
@@ -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()
});
}
}
19 changes: 19 additions & 0 deletions source/test/date.ts
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit f2be585

Please sign in to comment.