54 lines
1.1 KiB
TypeScript
54 lines
1.1 KiB
TypeScript
import { IKeyRow, IKeyRows } from './Row';
|
|
|
|
export interface ILayer extends IterableIterator<IKeyRow> {
|
|
rows: IKeyRows;
|
|
index: number;
|
|
length: number;
|
|
forEach(fn: (k: IKeyRow, i: number) => any): void;
|
|
map(fn: (k: IKeyRow, i: number) => IKeyRow): IKeyRows;
|
|
}
|
|
|
|
export type ILayers = ILayer[];
|
|
|
|
class Layer implements ILayer {
|
|
rows: IKeyRows = [];
|
|
index: number = 0;
|
|
|
|
constructor(rows: IKeyRows) {
|
|
this.rows = rows;
|
|
this.index = 0;
|
|
}
|
|
|
|
forEach(fn: (k: IKeyRow, i: number) => any): void {
|
|
return this.rows.forEach(fn);
|
|
}
|
|
|
|
map(fn: (k: IKeyRow, i: number) => IKeyRow): IKeyRows {
|
|
return this.rows.map(fn);
|
|
}
|
|
|
|
get length(): number {
|
|
return this.rows.length;
|
|
}
|
|
|
|
public next(): IteratorResult<IKeyRow> {
|
|
if (this.index < this.rows.length) {
|
|
return {
|
|
done: false,
|
|
value: this.rows[this.index++],
|
|
};
|
|
}
|
|
return {
|
|
done: true,
|
|
value: null,
|
|
};
|
|
}
|
|
|
|
[Symbol.iterator](): IterableIterator<IKeyRow> {
|
|
return this;
|
|
}
|
|
}
|
|
|
|
export const layer = (...rows: IKeyRows): ILayer => new Layer(rows);
|
|
export default layer;
|