You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/util/collection/ArrayIterable.ts

34 lines
769 B

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])
}
}