Introduce InputLine and the ability to acquire inputs from a TaxReturn.
[ustaxlib.git] / src / Line.ts
1 import TaxReturn from './TaxReturn';
2
3 export abstract class Line<T> {
4 private _id: string;
5 private _description?: string;
6
7 constructor(id: string, description?: string) {
8 this._id = id;
9 this._description = description;
10 }
11
12 get id(): string {
13 return this._id;
14 }
15
16 get description(): string {
17 return this._description;
18 }
19
20 abstract value(tr: TaxReturn): T;
21 };
22
23 type ComputeFunc<T> = (tr: TaxReturn, l: ComputedLine<T>) => T;
24
25 export class ComputedLine<T> extends Line<T> {
26 private _compute: ComputeFunc<T>;
27
28 constructor(id: string, compute: ComputeFunc<T>, description?: string) {
29 super(id, description);
30 this._compute = compute;
31 }
32
33 value(tr: TaxReturn): T {
34 return this._compute(tr, this);
35 }
36 };
37
38 export class ReferenceLine<T> extends Line<T> {
39 private _form: string;
40 private _line: string;
41
42 constructor(id: string, form: string, line: string) {
43 super(id, `Reference F${form}.L${line}`);
44 this._form = form;
45 this._line = line;
46 }
47
48 value(tr: TaxReturn): T {
49 return tr.getForm(this._form).getLine(this._line).value(tr);
50 }
51 };
52
53 export class InputLine<T> extends Line<T> {
54 private _input: string;
55
56 constructor(id: string, input: string, description?: string) {
57 super(id, description);
58 this._input = input;
59 }
60
61 value(tr: TaxReturn): T {
62 return tr.getInput<T>(this._input);
63 }
64 };