diff --git a/src/__tests__/constraint.test.ts b/src/__tests__/constraint.test.ts index bce9176..833ece4 100644 --- a/src/__tests__/constraint.test.ts +++ b/src/__tests__/constraint.test.ts @@ -1,6 +1,22 @@ import moment, { Moment } from "moment"; +import { map } from "ramda"; -import { eq, ge, gt, le, lt, ne, validate } from "../constraint"; +import { + eq, + ge, + gt, + le, + lt, + ne, + onFriday, + onMonday, + onSathurday, + onSunday, + onThursday, + onTuesday, + onWednesday, + validate, +} from "../constraint"; import { addInterval } from "../interval"; describe("validate", () => { @@ -64,3 +80,43 @@ describe("comparison constraints", () => { expect(injectCurrent(gt(less))).toBe(true); }); }); + +describe("weekday constraints", () => { + const daysOfTheWeek = [1, 2, 3, 4, 5, 6, 7]; + + const ds = map( + (x: number) => + moment("2020-10-12 17:23:57") + .clone() + .add(x - 1, "days"), + daysOfTheWeek, + ); + + test("onMonday", () => { + ds.forEach((d) => expect(onMonday(d)).toBe(d.isoWeekday() === 1)); + }); + + test("onTuesday", () => { + ds.forEach((d) => expect(onTuesday(d)).toBe(d.isoWeekday() === 2)); + }); + + test("onWednesday", () => { + ds.forEach((d) => expect(onWednesday(d)).toBe(d.isoWeekday() === 3)); + }); + + test("onThursday", () => { + ds.forEach((d) => expect(onThursday(d)).toBe(d.isoWeekday() === 4)); + }); + + test("onFriday", () => { + ds.forEach((d) => expect(onFriday(d)).toBe(d.isoWeekday() === 5)); + }); + + test("onSathurday", () => { + ds.forEach((d) => expect(onSathurday(d)).toBe(d.isoWeekday() === 6)); + }); + + test("onSunday", () => { + ds.forEach((d) => expect(onSunday(d)).toBe(d.isoWeekday() === 7)); + }); +}); diff --git a/src/constraint.ts b/src/constraint.ts index db54a92..9fa8d58 100644 --- a/src/constraint.ts +++ b/src/constraint.ts @@ -24,3 +24,11 @@ export const le = (threshold: Moment) => (current: Moment): boolean => export const lt = (threshold: Moment) => (current: Moment): boolean => current.isBefore(threshold); // c < t + +export const onMonday = (m: Moment) => m.isoWeekday() === 1; +export const onTuesday = (m: Moment) => m.isoWeekday() === 2; +export const onWednesday = (m: Moment) => m.isoWeekday() === 3; +export const onThursday = (m: Moment) => m.isoWeekday() === 4; +export const onFriday = (m: Moment) => m.isoWeekday() === 5; +export const onSathurday = (m: Moment) => m.isoWeekday() === 6; +export const onSunday = (m: Moment) => m.isoWeekday() === 7;