32 lines
512 B
TypeScript
32 lines
512 B
TypeScript
|
|
||
|
export interface Stream<T> {
|
||
|
get(): T;
|
||
|
peek(count?: number): T;
|
||
|
}
|
||
|
|
||
|
export class StreamWrapper<T> {
|
||
|
|
||
|
offset = 0
|
||
|
|
||
|
constructor(protected data: T[], protected createSentry: () => T) {
|
||
|
|
||
|
}
|
||
|
|
||
|
peek(count = 1) {
|
||
|
const offset = this.offset + (count - 1);
|
||
|
if (offset >= this.data.length) {
|
||
|
return this.createSentry();
|
||
|
}
|
||
|
return this.data[offset];
|
||
|
}
|
||
|
|
||
|
get() {
|
||
|
if (this.offset >= this.data.length) {
|
||
|
return this.createSentry();
|
||
|
}
|
||
|
return this.data[this.offset++];
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|