-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time-slots): add basic generation strategy
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import moment, { Moment } from "moment"; | ||
|
||
import { addInterval, Interval, slots } from "../slots"; | ||
|
||
describe("slots", () => { | ||
const interval: Interval = { amount: 30, unit: "minutes" }; | ||
const d1 = moment(); | ||
const d2 = addInterval(interval)(d1); // d1 + 30m | ||
const d3 = addInterval(interval)(d2); // d2 + 30m = d1 + 60m | ||
const start = d1; | ||
|
||
const geq = (d: Moment) => (c: Moment): boolean => c >= d; | ||
|
||
it("generate a single time slot", () => { | ||
const gen = slots(interval)([])(start); | ||
|
||
expect(gen.next().value).toEqual([start.format("HH:mm")]); | ||
}); | ||
|
||
it("generate subsequent time slots", () => { | ||
const gen = slots(interval)([])(start); | ||
|
||
expect(gen.next().value).toEqual([start.format("HH:mm")]); | ||
expect(gen.next().value).toEqual([addInterval(interval)(start).format("HH:mm")]); | ||
}); | ||
|
||
it("do not generate time slots if constraints always fail", () => { | ||
const gen = slots(interval)([() => false])(start); | ||
|
||
expect(gen.next().value).toEqual([]); | ||
}); | ||
|
||
it("do not generate time slots if a constraint is failing", () => { | ||
const gen = slots(interval)([geq(d2)])(start); | ||
|
||
expect(gen.next().value).toEqual([]); | ||
expect(gen.next().value).toEqual([d2.format("HH:mm")]); | ||
expect(gen.next().value).not.toEqual([]); | ||
}); | ||
|
||
it("do not generate time slots if costraints are failing", () => { | ||
const gen = slots(interval)([geq(d2), geq(d3)])(start); | ||
|
||
expect(gen.next().value).toEqual([]); | ||
expect(gen.next().value).toEqual([]); | ||
expect(gen.next().value).toEqual([d3.format("HH:mm")]); | ||
expect(gen.next().value).not.toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Moment } from "moment"; | ||
import * as R from "ramda"; | ||
|
||
export type Constraint = (current: Moment) => boolean; | ||
|
||
export const validate = (constraints: Constraint[]) => (current: Moment) => | ||
R.all(R.identity, R.map(R.applyTo(current), constraints)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import moment, { Moment } from "moment"; | ||
import { Constraint, validate } from "./constraint"; | ||
|
||
export type Interval = { | ||
amount: moment.DurationInputArg1; | ||
unit: moment.DurationInputArg2; | ||
}; | ||
|
||
export type Slot = string; | ||
|
||
export const addInterval = (interval: Interval) => (m: Moment): Moment => m.clone().add(interval.amount, interval.unit); | ||
|
||
export const slots = (interval: Interval) => (constraints: Constraint[]) => (start: Moment) => { | ||
return (function* () { | ||
let newStart = start; | ||
|
||
while (true) { | ||
yield one(constraints)(newStart); | ||
newStart = addInterval(interval)(newStart); | ||
} | ||
})(); | ||
}; | ||
|
||
export const one = (constraints: Constraint[]) => (start: Moment): string[] => { | ||
const current = moment(start, "HH:mm"); | ||
|
||
if (validate(constraints)(current)) { | ||
return [current.format("HH:mm")]; | ||
} | ||
|
||
return []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters