bolt/src/util.ts

36 lines
570 B
TypeScript
Raw Normal View History

2020-02-25 12:26:21 +01:00
export interface FastStringMap<T> {
[key: string]: T
}
2020-02-25 12:26:21 +01:00
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++];
}
}