add range class

This commit is contained in:
JOLIMAITRE Matthieu 2024-06-15 01:28:46 +02:00
parent 2e7cfb9deb
commit a84450fc23
3 changed files with 29 additions and 5 deletions

27
src/lib/Range.ts Normal file
View file

@ -0,0 +1,27 @@
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]);
});