Allow InputLine to specify a fallback value.
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Mar 2020 06:02:52 +0000 (01:02 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 8 Mar 2020 06:02:52 +0000 (01:02 -0500)
src/Line.test.ts
src/Line.ts

index 52271d5ba1294cd12ab52274ed5074f5d93c1061..ababbb777f63f7420dcae90c8009144c8027cfb1 100644 (file)
@@ -91,7 +91,8 @@ test('input line', () => {
     readonly name = 'F1';
     protected readonly _lines = {
       '1': new InputLine<Input>('key'),
-      '2': new InputLine<Input>('key2')
+      '2': new InputLine<Input>('key2'),
+      '3': new InputLine<Input>('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', () => {
index ed182e3bcae0ccc679db4062bcb130d18996e6bf..f85c359063845f24edac73872e3bdfb553fffbca 100644 (file)
@@ -66,15 +66,19 @@ export class ReferenceLine<F extends Form<any>,
 
 export class InputLine<U = unknown, T extends keyof U = any> extends Line<U[T]> {
   private _input: T;
+  private _fallback: U[T];
 
   form: Form<any, U>;
 
-  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<T>(this._input);
   }
 };