Add AccumulatorLine class.
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 06:18:51 +0000 (01:18 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 06:18:51 +0000 (01:18 -0500)
src/Line.test.ts
src/Line.ts

index 135bd922ce02b061522793ba703f386ffd6ad1b1..0d658c275452b43660cfd677de313771b5f37314 100644 (file)
@@ -1,5 +1,5 @@
-import { Line, InputLine, ReferenceLine, ComputedLine } from './Line';
-import Form from './Form';
+import { Line, AccumulatorLine, InputLine, ReferenceLine, ComputedLine } from './Line';
+import Form, { SupportsMultipleCopies } from './Form';
 import TaxReturn from './TaxReturn';
 import { NotFoundError } from './Errors';
 
@@ -104,3 +104,23 @@ test('line stack', () => {
   const l = new ReferenceLine<number>('32', 'Z-2', '2c');
   expect(l.value(tr)).toBe(20);
 });
+
+test('accumulator line', () => {
+  class TestForm extends Form implements SupportsMultipleCopies {
+    get name() { return 'Form B'; }
+
+    aggregate() { return null; }
+
+    protected getLines() {
+      return [ new ConstantLine<number>('g', 100.25) ]
+    }
+  };
+
+  const tr = new TaxReturn(2019);
+  tr.addForm(new TestForm());
+  tr.addForm(new TestForm());
+  tr.addForm(new TestForm());
+
+  const l = new AccumulatorLine('line', 'Form B', 'g');
+  expect(l.value(tr)).toBe(300.75);
+});
index f4beb7d07ff26d4d9d8cef7d6b06641d3ccb12a2..212fd6172bc87fcad7d41093fe531ef528d54441 100644 (file)
@@ -67,3 +67,20 @@ export class InputLine<T, U = unknown> extends Line<T> {
     return this.form.getInput(this._input) as any;
   }
 };
+
+export class AccumulatorLine extends Line<number> {
+  private _form: string;
+  private _line: string;
+
+  constructor(id: string, form: string, line: string, description?: string) {
+    super(id, description || `Accumulator F${form}.L${line}`);
+    this._form = form;
+    this._line = line;
+  }
+
+  value(tr: TaxReturn): number {
+    const forms = tr.getForms(this._form);
+    const reducer = (acc: number, curr: Form) => acc + curr.getValue<number>(tr, this._line);
+    return forms.reduce(reducer, 0);
+  }
+};