Skip to content

Commit

Permalink
feat(time-slots): add generator utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
lparolari committed Oct 13, 2020
1 parent 392a8c7 commit f14572f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/__tests__/generator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import moment from "moment";

import { Interval, slots } from "../slots";
import { take, flatten } from "../generator";

describe("generator", () => {
const interval: Interval = { amount: 30, unit: "minutes" };
const d1 = moment();
const start = d1;

let gen: Generator<string[], never, unknown>;

beforeEach(() => {
gen = slots(interval)([])(start);
});

describe("take", () => {
it("get n element from generator", () => {
expect(take(0)(gen)).toHaveLength(0);
expect(take(5)(gen)).toHaveLength(5);
});
});

describe("flatten", () => {
it("flatten elements from generator", () => {
expect(flatten(take(0)(gen))).toHaveLength(0);
expect(flatten(take(5)(gen))).toHaveLength(5);
});
});
});
10 changes: 10 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Slot } from "./slots";

import * as R from "ramda";

const repeat = (n: number) => R.repeat(0, n);

export const take = (n: number) => (iterable: IterableIterator<Slot[]>): string[][] =>
R.map(() => iterable.next().value as string[], repeat(n));

export const flatten = R.flatten;

0 comments on commit f14572f

Please sign in to comment.