barnulf_ts/src/lib/Range.ts

27 lines
575 B
TypeScript

export class Range {
from;
to;
constructor(from: number, to: number) {
this.from = from;
this.to = to;
}
*[Symbol.iterator](): Generator<number, void, void> {
let index = this.from;
while (index < this.to) yield index++;
}
includes(n: number) {
return (n > this.from) && (n < this.to);
}
}
export function range(from: number, to: number) {
return new Range(from, to);
}
Deno.test("range", async () => {
const { assertEquals } = await import("https://deno.land/std@0.224.0/assert/assert_equals.ts");
assertEquals([...range(0, 4)], [0, 1, 2, 3]);
});