Skip to content

Commit

Permalink
feat(constraints): add weekday constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
lparolari committed Oct 15, 2020
1 parent dc12ea9 commit 5d5bfa4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/__tests__/constraint.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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));
});
});
8 changes: 8 additions & 0 deletions src/constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 5d5bfa4

Please sign in to comment.