add range class
This commit is contained in:
parent
2e7cfb9deb
commit
a84450fc23
3 changed files with 29 additions and 5 deletions
27
src/lib/Range.ts
Normal file
27
src/lib/Range.ts
Normal 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]);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue