1 import { Line } from './Line';
2 import { InconsistencyError, NotFoundError } from './Errors';
4 export default abstract class Form {
5 private _lines: Line<any>[] = [];
6 private _input?: object;
8 abstract get name(): string;
10 constructor(input?: object) {
12 this.getLines().map(this.addLine.bind(this));
15 protected abstract getLines(): Line<any>[];
17 get allowMultipleCopies(): boolean {
21 private addLine(line: Line<any>) {
22 if (line.form !== undefined) {
23 throw new InconsistencyError('Line is already in a Form');
26 this.getLine(line.id);
29 this._lines.push(line);
32 throw new InconsistencyError('Cannot add a line with a duplicate identifier');
35 getLine(id: string): Line<any> {
36 const lines = this._lines.filter(l => l.id === id);
37 if (lines.length == 0) {
38 throw new NotFoundError(id);
43 getInput<T>(name: string): T {
44 if (!(name in this._input)) {
45 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
47 return this._input[name] as T;