From edcaf96c120d2efae784da1cb1282044ee072883 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 26 Mar 2021 12:19:27 -0400 Subject: [PATCH] Format values in FormView, using the new FormatType LineOption. --- src/FormView.tsx | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/FormView.tsx b/src/FormView.tsx index aa2c1c5..d90c4fe 100644 --- a/src/FormView.tsx +++ b/src/FormView.tsx @@ -5,7 +5,7 @@ import { createEffect, createMemo, createState } from 'solid-js'; import { For, Show } from 'solid-js/dom'; -import { TaxReturn, Form, Line } from 'ustaxlib/core'; +import { TaxReturn, Form, Line, FormatType } from 'ustaxlib/core'; import * as Trace from 'ustaxlib/core/Trace'; import { Edge } from 'ustaxlib/core/Trace'; import { graphviz } from 'd3-graphviz'; @@ -42,6 +42,56 @@ interface LineProps { line: Line; } +class Formatter { + private static _instance: Formatter; + + private _dollar = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', 'currencySign': 'accounting' } as Intl.NumberFormatOptions); + private _percent = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 3 }); + private _decimal = new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 5 }); + + private constructor() {} + + static getInstance() { + if (!Formatter._instance) { + Formatter._instance = new Formatter(); + } + return Formatter._instance; + } + + dollar(value: number): string { + return this._dollar.format(value); + } + + percent(value: number): string { + return this._percent.format(value); + } + + decimal(value: number): string { + return this._decimal.format(value); + } + + string(value: any): string { + return JSON.stringify(value, null, 1); + } +} + +function formatLine(value: any, line: Line): string { + const formatter = Formatter.getInstance(); + const formatType = line.options.formatType; + + if (typeof(value) === 'number') { + if (formatType == FormatType.Decimal) { + return formatter.decimal(value); + } else if (formatType == FormatType.Percent) { + return formatter.percent(value); + } else if (formatType == FormatType.Dollar || formatType == undefined) { + return formatter.dollar(value); + } + } + + return formatter.string(value); +} + function LineView(props: LineProps) { const { tr, line } = props; @@ -72,7 +122,7 @@ function LineView(props: LineProps) { if (state.error) { return {state.error.message}; } - return JSON.stringify(state.value, null, 1); + return formatLine(state.value, line); }); const toggleTrace = () => setState('showTrace', !state.showTrace); -- 2.43.5