lib/src/util/collection/ArrayIterable.ts
garrettmills 1d5056b753
All checks were successful
continuous-integration/drone/push Build is passing
Setup eslint and enforce rules
2021-06-02 22:36:25 -05:00

34 lines
769 B
TypeScript

import { Iterable } from './Iterable'
import {collect, Collection} from './Collection'
/**
* A basic Iterable implementation that uses an array as a backend.
* @extends Iterable
*/
export class ArrayIterable<T> extends Iterable<T> {
constructor(
/**
* Items to use for this iterable.
*/
protected items: T[],
) {
super()
}
async at(i: number): Promise<T | undefined> {
return this.items[i]
}
async range(start: number, end: number): Promise<Collection<T>> {
return collect(this.items.slice(start, end + 1))
}
async count(): Promise<number> {
return this.items.length
}
clone(): ArrayIterable<T> {
return new ArrayIterable([...this.items])
}
}