1- import { UnitsHelper } from "@iota/iota.js" ;
1+ import { INodeInfoBaseToken , UnitsHelper } from "@iota/iota.js" ;
22import humanize from "humanize-duration" ;
33import moment from "moment" ;
44import { ServiceFactory } from "../factories/serviceFactory" ;
@@ -8,6 +8,35 @@ import { NodeConfigService } from "../services/nodeConfigService";
88 * Class to help formatting values.
99 */
1010export 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