Skip to content

Commit 3eea31d

Browse files
committed
fix: Update FormatHelper as Singleton
1 parent 4bdec60 commit 3eea31d

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

src/app/components/tangle/MigratedFund.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ class MigratedFund extends Component<MigratedFundProps, MigratedFundState> {
9191
}
9292
)}
9393
>
94-
{FormatHelper.amount(Number(this.props.fund.deposit), this.state.formatFull)}
94+
{FormatHelper.getInstance().amount(
95+
Number(this.props.fund.deposit),
96+
this.state.formatFull
97+
)}
9598
</button>
9699
</div>
97100
</div>

src/app/components/tangle/Output.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ class Output extends Component<OutputProps, OutputState> {
6666
}
6767
)}
6868
>
69-
{FormatHelper.amount(Number(this.state.output.amount), this.state.formatFull)}
69+
{FormatHelper.getInstance().amount(
70+
Number(this.state.output.amount),
71+
this.state.formatFull
72+
)}
7073
</button>
7174
</div>
7275
</div>

src/app/components/tangle/ReceiptMilestoneOption.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ReceiptMilestoneOption extends Component<ReceiptMilestoneOptionProps, Rece
8383
}
8484
)}
8585
>
86-
{FormatHelper.amount(
86+
{FormatHelper.getInstance().amount(
8787
Number(this.props.option.transaction.output.amount),
8888
this.state.formatFull
8989
)}

src/app/routes/explorer/Address.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ class Address extends AsyncComponent<RouteComponentProps<AddressRouteProps>, Add
138138
}
139139
)}
140140
>
141-
{FormatHelper.amount(Number(this.state.balance), this.state.formatFull)}
141+
{FormatHelper.getInstance().amount(
142+
Number(this.state.balance),
143+
this.state.formatFull
144+
)}
142145
</button>
143146
</div>
144147
{this.state.address?.nativeTokens && (

src/utils/formatHelper.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UnitsHelper } from "@iota/iota.js";
1+
import { INodeInfoBaseToken, UnitsHelper } from "@iota/iota.js";
22
import humanize from "humanize-duration";
33
import moment from "moment";
44
import { ServiceFactory } from "../factories/serviceFactory";
@@ -8,6 +8,35 @@ import { NodeConfigService } from "../services/nodeConfigService";
88
* Class to help formatting values.
99
*/
1010
export class FormatHelper {
11+
/**
12+
* The singleton instance.
13+
*/
14+
private static instance: FormatHelper;
15+
16+
/**
17+
* The base token of the node.
18+
*/
19+
private readonly _baseToken: INodeInfoBaseToken;
20+
21+
/**
22+
* Create a new instance FormatHelper.
23+
*/
24+
private constructor() {
25+
const nodeConfigService = ServiceFactory.get<NodeConfigService>("node-config");
26+
this._baseToken = nodeConfigService.getBaseToken();
27+
}
28+
29+
/**
30+
* Get the FormatHelper singleton instance.
31+
*/
32+
public static getInstance(): FormatHelper {
33+
if (!FormatHelper.instance) {
34+
FormatHelper.instance = new FormatHelper();
35+
}
36+
37+
return FormatHelper.instance;
38+
}
39+
1140
/**
1241
* Format the duration as human readable.
1342
* @param milliseconds The milliseconds total for the duration.
@@ -129,17 +158,15 @@ export class FormatHelper {
129158
* @param decimalPlaces The number of decimal places.
130159
* @returns The formatted amount.
131160
*/
132-
public static amount(value: number, formatFull: boolean, decimalPlaces: number = 2): string {
133-
const nodeConfigService = ServiceFactory.get<NodeConfigService>("node-config");
134-
const baseToken = nodeConfigService.getBaseToken();
135-
161+
public amount(value: number, formatFull: boolean, decimalPlaces: number = 2): string {
136162
if (formatFull) {
137-
return `${value} ${baseToken.subunit ? baseToken.subunit : baseToken.unit}`;
163+
return `${value} ${this._baseToken.subunit ? this._baseToken.subunit : this._baseToken.unit}`;
138164
}
165+
const baseTokeValue = value / Math.pow(10, this._baseToken.decimals);
166+
const amount = this._baseToken.useMetricPrefix
167+
? UnitsHelper.formatBest(baseTokeValue)
168+
: `${Number.parseFloat(baseTokeValue.toFixed(decimalPlaces))} `;
139169

140-
const amount = baseToken.useMetricPrefix
141-
? UnitsHelper.formatBest(value / Math.pow(10, baseToken.decimals))
142-
: `${Number.parseFloat((value / Math.pow(10, baseToken.decimals)).toFixed(decimalPlaces))} `;
143-
return `${amount}${baseToken.unit}`;
170+
return `${amount}${this._baseToken.unit}`;
144171
}
145172
}

0 commit comments

Comments
 (0)