1
- import BN from "bn.js" ;
2
- import numberToBN from "number-to-bn" ;
1
+ import BigNumber from 'bignumber.js' ;
3
2
4
- const negative1 = new BN ( - 1 ) ;
5
- const zero = new BN ( 0 ) ;
3
+ var Units = { }
6
4
7
- const unitMap = {
8
- FFGZero : "0" ,
9
- FFGOne : "1" ,
10
- KFFG : "1000" ,
11
- MFFG : "1000000" ,
12
- GFFG : "1000000000" ,
13
- MicroFFG : "1000000000000" ,
14
- MiliFFG : "1000000000000000" ,
15
- FFG : "1000000000000000000" ,
16
- ZFFG : "1000000000000000000000" ,
17
- } ;
5
+ var rawUnits = {
6
+ ffgzero : "0" ,
7
+ ffgone : "1" ,
8
+ kffg : "1000" ,
9
+ mffg : "1000000" ,
10
+ gffg : "1000000000" ,
11
+ microffg : "1000000000000" ,
12
+ miliffg : "1000000000000000" ,
13
+ ffg : "1000000000000000000" ,
14
+ zffg : "1000000000000000000000" ,
15
+ } ;
18
16
19
- function getValueOfUnit ( unitInput ) {
20
- const unit = unitInput ? unitInput : "FFG" ;
21
- var unitValue = unitMap [ unit ] ; // eslint-disable-line
17
+ var units = { }
22
18
23
- if ( typeof unitValue !== "string" ) {
24
- throw new Error (
25
- `the unit provided ${ unitInput } doesn't exists, please use the one of the following units ${ JSON . stringify (
26
- unitMap ,
27
- null ,
28
- 2
29
- ) } `
30
- ) ;
31
- }
32
-
33
- return new BN ( unitValue , 10 ) ;
34
- }
35
-
36
- function numberToString ( arg ) {
37
- if ( typeof arg === "string" ) {
38
- if ( ! arg . match ( / ^ - ? [ 0 - 9 . ] + $ / ) ) {
39
- throw new Error (
40
- `while converting number to string, invalid number value '${ arg } '`
41
- ) ;
42
- }
43
- return arg ;
44
- } else if ( typeof arg === "number" ) {
45
- return String ( arg ) ;
46
- } else if (
47
- typeof arg === "object" &&
48
- arg . toString &&
49
- ( arg . toTwos || arg . dividedToIntegerBy )
50
- ) {
51
- if ( arg . toPrecision ) {
52
- return String ( arg . toPrecision ( ) ) ;
53
- } else {
54
- return arg . toString ( 10 ) ;
55
- }
56
- }
57
- throw new Error (
58
- `while converting number to string, invalid number value '${ arg } ' type ${ typeof arg } .`
59
- ) ;
60
- }
61
-
62
- function fromFFGOne ( ffgOneInput , unit , optionsInput ) {
63
- var ffgOne = numberToBN ( ffgOneInput ) ;
64
- var negative = ffgOne . lt ( zero ) ;
65
- const base = getValueOfUnit ( unit ) ;
66
- const baseLength = unitMap [ unit ] . length - 1 || 1 ;
67
- const options = optionsInput || { } ;
19
+ Object . keys ( rawUnits ) . map ( function ( unit ) {
20
+ unit = unit . toLowerCase ( )
21
+ units [ unit ] = new BigNumber ( rawUnits [ unit ] , 10 )
22
+ } )
68
23
69
- if ( negative ) {
70
- ffgOne = ffgOne . mul ( negative1 ) ;
71
- }
24
+ Units . units = rawUnits
72
25
73
- var fraction = ffgOne . mod ( base ) . toString ( 10 ) ;
26
+ var re = RegExp ( / ^ [ 0 - 9 ] + \. ? [ 0 - 9 ] * $ / )
74
27
75
- while ( fraction . length < baseLength ) {
76
- fraction = `0${ fraction } ` ;
28
+ Units . convert = function ( value , from , to ) {
29
+ if ( ! re . test ( value ) ) {
30
+ throw new Error ( 'Unsupported value' )
77
31
}
78
32
79
- if ( ! options . pad ) {
80
- fraction = fraction . match ( / ^ ( [ 0 - 9 ] * [ 1 - 9 ] | 0 ) ( 0 * ) / ) [ 1 ] ;
33
+ from = from . toLowerCase ( )
34
+ if ( ! units [ from ] ) {
35
+ throw new Error ( 'Unsupported input unit' )
81
36
}
82
37
83
- var whole = ffgOne . div ( base ) . toString ( 10 ) ;
84
-
85
- if ( options . commify ) {
86
- whole = whole . replace ( / \B (? = ( \d { 3 } ) + (? ! \d ) ) / g, "," ) ;
38
+ to = to . toLowerCase ( )
39
+ if ( ! units [ to ] ) {
40
+ throw new Error ( 'Unsupported output unit' )
87
41
}
88
42
89
- var value = `${ whole } ${ fraction == "0" ? "" : `.${ fraction } ` } ` ;
90
-
91
- if ( negative ) {
92
- value = `-${ value } ` ;
93
- }
94
-
95
- return value ;
43
+ return new BigNumber ( value , 10 ) . mul ( units [ from ] ) . round ( 0 , BigNumber . ROUND_DOWN ) . div ( units [ to ] ) . toString ( 10 )
96
44
}
97
45
98
- function toFFGOne ( ffgInput , unit ) {
99
- var ffg = numberToString ( ffgInput ) ; // eslint-disable-line
100
- const base = getValueOfUnit ( unit ) ;
101
- const baseLength = unitMap [ unit ] . length - 1 || 1 ;
102
-
103
- // Is it negative?
104
- var negative = ffg . substring ( 0 , 1 ) === "-" ; // eslint-disable-line
105
- if ( negative ) {
106
- ffg = ffg . substring ( 1 ) ;
107
- }
108
-
109
- if ( ffg === "." ) {
110
- throw new Error (
111
- `while converting number ${ ffgInput } to ffgOne, invalid value`
112
- ) ;
113
- }
114
-
115
- // Split it into a whole and fractional part
116
- var comps = ffg . split ( "." ) ; // eslint-disable-line
117
- if ( comps . length > 2 ) {
118
- throw new Error (
119
- `while converting number ${ ffgInput } to ffgOne, too many decimal points`
120
- ) ;
121
- }
122
-
123
- var whole = comps [ 0 ] ,
124
- fraction = comps [ 1 ] ; // eslint-disable-line
125
-
126
- if ( ! whole ) {
127
- whole = "0" ;
128
- }
129
- if ( ! fraction ) {
130
- fraction = "0" ;
131
- }
132
- if ( fraction . length > baseLength ) {
133
- throw new Error (
134
- `while converting number ${ ffgInput } to ffgOne, too many decimal places`
135
- ) ;
136
- }
137
-
138
- while ( fraction . length < baseLength ) {
139
- fraction += "0" ;
46
+ Units . lazyConvert = function ( value , to ) {
47
+ var tmp = value . split ( ' ' )
48
+ if ( tmp . length !== 2 ) {
49
+ throw new Error ( 'Invalid input' )
140
50
}
141
-
142
- whole = new BN ( whole ) ;
143
- fraction = new BN ( fraction ) ;
144
- let ffgOne = whole . mul ( base ) . add ( fraction ) ; // eslint-disable-line
145
-
146
- if ( negative ) {
147
- ffgOne = ffgOne . mul ( negative1 ) ;
148
- }
149
-
150
- return new BN ( ffgOne . toString ( 10 ) , 10 ) ;
51
+ return Units . convert ( tmp [ 0 ] , tmp [ 1 ] , to ) + ' ' + to
151
52
}
152
53
153
- export default {
154
- fromFFGOne,
155
- toFFGOne,
156
- } ;
54
+ export {
55
+ Units
56
+ }
0 commit comments