From ce61187b9b748c30d6d43d8314c9599e7676eb2c Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 20 Feb 2020 01:18:51 -0500 Subject: [PATCH] Add AccumulatorLine class. --- src/Line.test.ts | 24 ++++++++++++++++++++++-- src/Line.ts | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Line.test.ts b/src/Line.test.ts index 135bd92..0d658c2 100644 --- a/src/Line.test.ts +++ b/src/Line.test.ts @@ -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('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('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); +}); diff --git a/src/Line.ts b/src/Line.ts index f4beb7d..212fd61 100644 --- a/src/Line.ts +++ b/src/Line.ts @@ -67,3 +67,20 @@ export class InputLine extends Line { return this.form.getInput(this._input) as any; } }; + +export class AccumulatorLine extends Line { + 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(tr, this._line); + return forms.reduce(reducer, 0); + } +}; -- 2.22.5