From 851b2600bbab2a11a2072d451a656a2056b0a06d Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 8 Mar 2020 01:02:52 -0500 Subject: [PATCH] Allow InputLine to specify a fallback value. --- src/Line.test.ts | 5 ++++- src/Line.ts | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Line.test.ts b/src/Line.test.ts index 52271d5..ababbb7 100644 --- a/src/Line.test.ts +++ b/src/Line.test.ts @@ -91,7 +91,8 @@ test('input line', () => { readonly name = 'F1'; protected readonly _lines = { '1': new InputLine('key'), - '2': new InputLine('key2') + '2': new InputLine('key2'), + '3': new InputLine('key2', undefined, 'FALLBACK') }; }; const tr = new TaxReturn(2019); @@ -103,6 +104,8 @@ test('input line', () => { const l2 = f.getLine('2'); expect(() => l2.value(tr)).toThrow(NotFoundError); + + expect(f.getLine('3').value(tr)).toBe('FALLBACK'); }); test('line stack', () => { diff --git a/src/Line.ts b/src/Line.ts index ed182e3..f85c359 100644 --- a/src/Line.ts +++ b/src/Line.ts @@ -66,15 +66,19 @@ export class ReferenceLine, export class InputLine extends Line { private _input: T; + private _fallback: U[T]; form: Form; - constructor(input: T, description?: string) { + constructor(input: T, description?: string, fallback?: U[T]) { super(description || `Input from ${input}`); this._input = input; + this._fallback = fallback; } value(tr: TaxReturn): U[T] { + if (!this.form.hasInput(this._input) && this._fallback !== undefined) + return this._fallback; return this.form.getInput(this._input); } }; -- 2.22.5