From 97cfda5acbd98693b12e58c09eda38eb45b7eb8b Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 21 Mar 2021 15:05:01 -0400 Subject: [PATCH] Introduce the LineOptions interface and optional ctor param for Line. Currently this is used to specify a new FormatType, which can be used by ustaxviewer to format the value appropriately. --- src/core/Line.ts | 39 ++++++++++++++++++++++++++++----------- src/core/index.ts | 2 +- src/fed2019/Form1116.ts | 4 ++-- src/fed2019/Form8606.ts | 4 ++-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/core/Line.ts b/src/core/Line.ts index bdd5571..3b709c9 100644 --- a/src/core/Line.ts +++ b/src/core/Line.ts @@ -7,14 +7,27 @@ import TaxReturn from './TaxReturn'; import * as Trace from './Trace'; import Form, { FormClass } from './Form'; +export enum FormatType { + Dollar = '$', + Decimal = '.', + Percent = '%', + String = 's', +} + +interface LineOptions { + formatType?: FormatType; +} + export abstract class Line { private _description?: string; + private _options?: LineOptions; _id: string; // _id is set by Form.init(). form: Form; // Set by Form.init(); - constructor(description?: string) { + constructor(description?: string, options?: LineOptions) { this._description = description; + this._options = options; } get id(): string { @@ -25,6 +38,10 @@ export abstract class Line { return this._description; } + get options(): LineOptions { + return this._options || {}; + } + abstract value(tr: TaxReturn): T; }; @@ -33,8 +50,8 @@ type ComputeFunc = (tr: TaxReturn) => T; export class ComputedLine extends Line { private _compute: ComputeFunc; - constructor(compute: ComputeFunc, description?: string) { - super(description); + constructor(compute: ComputeFunc, description?: string, options?: LineOptions) { + super(description, options); this._compute = compute; } @@ -57,8 +74,8 @@ export class ReferenceLine, line: L, description?: string, fallback?: T) { - super(description || `Reference ${form.name}@${line}`); + constructor(form: FormClass, line: L, description?: string, fallback?: T, options?: LineOptions) { + super(description || `Reference ${form.name}@${line}`, options); this._form = form; this._line = line; this._fallback = fallback; @@ -85,8 +102,8 @@ export class SymbolicLine; private _key: K; - constructor(form: FormClass, key: K, description?: string) { - super(description || `Reference ${form.name}/${key}`); + constructor(form: FormClass, key: K, description?: string, options?: LineOptions) { + super(description || `Reference ${form.name}/${key}`, options); this._form = form; this._key = key; } @@ -106,8 +123,8 @@ export class InputLine extends Line form: Form; - constructor(input: T, description?: string, fallback?: U[T]) { - super(description || `Input from ${input}`); + constructor(input: T, description?: string, fallback?: U[T], options?: LineOptions) { + super(description || `Input from ${input}`, options); this._input = input; this._fallback = fallback; } @@ -130,8 +147,8 @@ export class AccumulatorLine; private _line: L; - constructor(form: FormClass, line: L, description?: string) { - super(description || `Accumulator ${form.name}@${line}`); + constructor(form: FormClass, line: L, description?: string, options?: LineOptions) { + super(description || `Accumulator ${form.name}@${line}`, options); this._form = form; this._line = line; } diff --git a/src/core/index.ts b/src/core/index.ts index c0d8979..20e758b 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -3,7 +3,7 @@ // version 3.0. The full text of the license can be found in LICENSE.txt. // SPDX-License-Identifier: GPL-3.0-only -export { Line } from './Line'; +export { Line, FormatType } from './Line'; export { default as Form } from './Form'; export { default as Person } from './Person'; export { default as TaxReturn } from './TaxReturn'; diff --git a/src/fed2019/Form1116.ts b/src/fed2019/Form1116.ts index 1514b51..4baf98d 100644 --- a/src/fed2019/Form1116.ts +++ b/src/fed2019/Form1116.ts @@ -4,7 +4,7 @@ // SPDX-License-Identifier: GPL-3.0-only import { Form, Person, TaxReturn } from '../core'; -import { Line, ComputedLine, InputLine, ReferenceLine, SymbolicLine, UnsupportedLine, sumFormLines } from '../core/Line'; +import { Line, ComputedLine, InputLine, ReferenceLine, SymbolicLine, UnsupportedLine, sumFormLines, FormatType } from '../core/Line'; import { UnsupportedFeatureError } from '../core/Errors'; import { reduceBySum } from '../core/Math'; @@ -71,7 +71,7 @@ export default class Form1116 extends Form { }), '3f': new ComputedLine((tr): number => { return Number.parseFloat((this.getValue(tr, '3d') / this.getValue(tr, '3e')).toFixed(4)); - }), + }, undefined, { formatType: FormatType.Decimal }), '3g': new ComputedLine((tr): number => { return this.getValue(tr, '3c') * this.getValue(tr, '3f'); }), diff --git a/src/fed2019/Form8606.ts b/src/fed2019/Form8606.ts index 4938de3..2ab8132 100644 --- a/src/fed2019/Form8606.ts +++ b/src/fed2019/Form8606.ts @@ -4,7 +4,7 @@ // SPDX-License-Identifier: GPL-3.0-only import { Form, Person, TaxReturn } from '../core'; -import { Line, AccumulatorLine, ComputedLine, InputLine, ReferenceLine, UnsupportedLine } from '../core/Line'; +import { Line, AccumulatorLine, ComputedLine, InputLine, ReferenceLine, UnsupportedLine, FormatType } from '../core/Line'; import { clampToZero, undefinedToZero } from '../core/Math'; export interface Form8606Input { @@ -44,7 +44,7 @@ export default class Form8606 extends Form { undefinedToZero(this.getValue(tr, '7')) + undefinedToZero(this.getValue(tr, '8')); }), - '10': new ComputedLine((tr): number => this.getValue(tr, '5') / this.getValue(tr, '9')), + '10': new ComputedLine((tr): number => this.getValue(tr, '5') / this.getValue(tr, '9'), undefined, { formatType: FormatType.Decimal }), '11': new ComputedLine((tr): number => this.getValue(tr, '8') * this.getValue(tr, '10'), 'Nontaxable portion converted to Roth'), '12': new ComputedLine((tr): number => this.getValue(tr, '7') * this.getValue(tr, '10'), 'Nontaxable portion of distributions not converted to Roth'), '13': new ComputedLine((tr): number => this.getValue(tr, '11') + this.getValue(tr, '12'), 'Nontaxable portion of all distributions'), -- 2.22.5