Skip to content

Commit

Permalink
feat(constraints): add set constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
lparolari committed Oct 15, 2020
1 parent 5d5bfa4 commit fc84460
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/__tests__/constraint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
onThursday,
onTuesday,
onWednesday,
between,
validate,
} from "../constraint";
import { addInterval } from "../interval";
Expand Down Expand Up @@ -81,6 +82,14 @@ describe("comparison constraints", () => {
});
});

describe("set costraints", () => {
test("between", () => {
const d = moment();
// we may check call to other functions with right parameters.
expect(between(d, d)(d)).toBe(false);
});
});

describe("weekday constraints", () => {
const daysOfTheWeek = [1, 2, 3, 4, 5, 6, 7];

Expand Down
17 changes: 16 additions & 1 deletion src/constraint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Moment } from "moment";
import { Moment, unitOfTime } from "moment";
import * as R from "ramda";

export type Constraint = (current: Moment) => boolean;
Expand All @@ -7,6 +7,7 @@ export const validate = (constraints: Constraint[]) => (
current: Moment,
): boolean => R.all(R.identity, R.map(R.applyTo(current), constraints));

// comparison constraints
export const gt = (threshold: Moment) => (current: Moment): boolean =>
current.isAfter(threshold); // c > t

Expand All @@ -25,6 +26,20 @@ export const le = (threshold: Moment) => (current: Moment): boolean =>
export const lt = (threshold: Moment) => (current: Moment): boolean =>
current.isBefore(threshold); // c < t

// set constraints
export const betweenWithInclusivityAndGranularity = (
granularity: unitOfTime.StartOf,
) => (inclusivity: "()" | "[)" | "(]" | "[]") => (d1: Moment, d2: Moment) => (
current: Moment,
): boolean => current.isBetween(d1, d2, granularity, inclusivity);

export const betweenWithInclusivity = betweenWithInclusivityAndGranularity(
"minute",
);

export const between = betweenWithInclusivity("[)");

// weekday constraints
export const onMonday = (m: Moment) => m.isoWeekday() === 1;
export const onTuesday = (m: Moment) => m.isoWeekday() === 2;
export const onWednesday = (m: Moment) => m.isoWeekday() === 3;
Expand Down

0 comments on commit fc84460

Please sign in to comment.