Restructure adding Lines to Forms.
[ustaxlib.git] / src / Form.ts
1 import { Line } from './Line';
2 import { InconsistencyError, NotFoundError } from './Errors';
3
4 export default abstract class Form {
5 private _lines: Line<any>[] = [];
6
7 abstract get name(): string;
8
9 constructor() {
10 this.getLines().map(this.addLine.bind(this));
11 }
12
13 protected abstract getLines(): Line<any>[];
14
15 get allowMultipleCopies(): boolean {
16 return false;
17 }
18
19 private addLine(line: Line<any>) {
20 try {
21 this.getLine(line.id);
22 } catch {
23 this._lines.push(line);
24 return;
25 }
26 throw new InconsistencyError('Cannot add a line with a duplicate identifier');
27 }
28
29 getLine(id: string): Line<any> {
30 const lines = this._lines.filter(l => l.id === id);
31 if (lines.length == 0) {
32 throw new NotFoundError(id);
33 }
34 return lines[0];
35 }
36 };