@keplr-wallet/unit 0.9.9-rc.0 → 0.9.11-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/coin-pretty.d.ts +13 -3
- package/build/coin-pretty.js +36 -25
- package/build/coin-pretty.js.map +1 -1
- package/build/coin-pretty.spec.js +194 -9
- package/build/coin-pretty.spec.js.map +1 -1
- package/build/coin-utils.js +8 -4
- package/build/coin-utils.js.map +1 -1
- package/build/coin-utils.spec.js +16 -0
- package/build/coin-utils.spec.js.map +1 -1
- package/build/coin.js +1 -1
- package/build/coin.js.map +1 -1
- package/build/coin.spec.js +15 -0
- package/build/coin.spec.js.map +1 -1
- package/build/dec-utils.d.ts +8 -1
- package/build/dec-utils.js +22 -20
- package/build/dec-utils.js.map +1 -1
- package/build/dec-utils.spec.js +19 -0
- package/build/dec-utils.spec.js.map +1 -1
- package/build/decimal.d.ts +18 -9
- package/build/decimal.js +63 -27
- package/build/decimal.js.map +1 -1
- package/build/decimal.spec.js +218 -2
- package/build/decimal.spec.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/int-pretty.d.ts +16 -4
- package/build/int-pretty.js +74 -36
- package/build/int-pretty.js.map +1 -1
- package/build/int-pretty.spec.js +261 -94
- package/build/int-pretty.spec.js.map +1 -1
- package/build/int.d.ts +14 -2
- package/build/int.js +48 -0
- package/build/int.js.map +1 -1
- package/build/int.spec.d.ts +1 -0
- package/build/int.spec.js +133 -0
- package/build/int.spec.js.map +1 -0
- package/build/price-pretty.d.ts +13 -3
- package/build/price-pretty.js +37 -26
- package/build/price-pretty.js.map +1 -1
- package/build/price-pretty.spec.js +59 -2
- package/build/price-pretty.spec.js.map +1 -1
- package/build/rate-pretty.d.ts +57 -0
- package/build/rate-pretty.js +128 -0
- package/build/rate-pretty.js.map +1 -0
- package/build/rate-pretty.spec.d.ts +1 -0
- package/build/rate-pretty.spec.js +38 -0
- package/build/rate-pretty.spec.js.map +1 -0
- package/package.json +3 -3
- package/src/coin-pretty.spec.ts +304 -11
- package/src/coin-pretty.ts +56 -29
- package/src/coin-utils.spec.ts +32 -0
- package/src/coin-utils.ts +12 -4
- package/src/coin.spec.ts +20 -0
- package/src/coin.ts +1 -1
- package/src/dec-utils.spec.ts +39 -0
- package/src/dec-utils.ts +25 -20
- package/src/decimal.spec.ts +269 -2
- package/src/decimal.ts +84 -54
- package/src/index.ts +1 -0
- package/src/int-pretty.spec.ts +296 -101
- package/src/int-pretty.ts +87 -34
- package/src/int.spec.ts +178 -0
- package/src/int.ts +66 -2
- package/src/price-pretty.spec.ts +106 -2
- package/src/price-pretty.ts +50 -30
- package/src/rate-pretty.spec.ts +52 -0
- package/src/rate-pretty.ts +165 -0
package/src/price-pretty.ts
CHANGED
@@ -3,6 +3,7 @@ import { Dec } from "./decimal";
|
|
3
3
|
import { FiatCurrency } from "@keplr-wallet/types";
|
4
4
|
import { DeepReadonly } from "utility-types";
|
5
5
|
import { DecUtils } from "./dec-utils";
|
6
|
+
import bigInteger from "big-integer";
|
6
7
|
|
7
8
|
export type PricePrettyOptions = {
|
8
9
|
separator: string;
|
@@ -23,19 +24,14 @@ export class PricePretty {
|
|
23
24
|
|
24
25
|
constructor(
|
25
26
|
protected _fiatCurrency: FiatCurrency,
|
26
|
-
protected amount: Dec | { toDec(): Dec }
|
27
|
+
protected amount: Dec | { toDec(): Dec } | bigInteger.BigNumber
|
27
28
|
) {
|
28
|
-
|
29
|
-
this.intPretty = new IntPretty(amount.toDec());
|
30
|
-
} else {
|
31
|
-
this.intPretty = new IntPretty(amount);
|
32
|
-
}
|
33
|
-
|
34
|
-
this.intPretty = this.intPretty
|
29
|
+
this.intPretty = new IntPretty(amount)
|
35
30
|
.maxDecimals(_fiatCurrency.maxDecimals)
|
36
31
|
.shrink(true)
|
37
32
|
.trim(true)
|
38
|
-
.locale(false)
|
33
|
+
.locale(false)
|
34
|
+
.inequalitySymbol(true);
|
39
35
|
|
40
36
|
this._options.locale = _fiatCurrency.locale;
|
41
37
|
}
|
@@ -77,22 +73,30 @@ export class PricePretty {
|
|
77
73
|
return pretty;
|
78
74
|
}
|
79
75
|
|
80
|
-
|
76
|
+
moveDecimalPointLeft(delta: number): PricePretty {
|
81
77
|
const pretty = this.clone();
|
82
|
-
pretty.intPretty = pretty.intPretty.
|
78
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointLeft(delta);
|
83
79
|
return pretty;
|
84
80
|
}
|
85
81
|
|
86
|
-
|
82
|
+
moveDecimalPointRight(delta: number): PricePretty {
|
87
83
|
const pretty = this.clone();
|
88
|
-
pretty.intPretty = pretty.intPretty.
|
84
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointRight(delta);
|
89
85
|
return pretty;
|
90
86
|
}
|
91
87
|
|
88
|
+
/**
|
89
|
+
* @deprecated Use`moveDecimalPointLeft`
|
90
|
+
*/
|
91
|
+
increasePrecision(delta: number): PricePretty {
|
92
|
+
return this.moveDecimalPointLeft(delta);
|
93
|
+
}
|
94
|
+
|
95
|
+
/**
|
96
|
+
* @deprecated Use`moveDecimalPointRight`
|
97
|
+
*/
|
92
98
|
decreasePrecision(delta: number): PricePretty {
|
93
|
-
|
94
|
-
pretty.intPretty = pretty.intPretty.decreasePrecision(delta);
|
95
|
-
return pretty;
|
99
|
+
return this.moveDecimalPointRight(delta);
|
96
100
|
}
|
97
101
|
|
98
102
|
maxDecimals(max: number): PricePretty {
|
@@ -101,6 +105,18 @@ export class PricePretty {
|
|
101
105
|
return pretty;
|
102
106
|
}
|
103
107
|
|
108
|
+
inequalitySymbol(bool: boolean): PricePretty {
|
109
|
+
const pretty = this.clone();
|
110
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbol(bool);
|
111
|
+
return pretty;
|
112
|
+
}
|
113
|
+
|
114
|
+
inequalitySymbolSeparator(str: string): PricePretty {
|
115
|
+
const pretty = this.clone();
|
116
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbolSeparator(str);
|
117
|
+
return pretty;
|
118
|
+
}
|
119
|
+
|
104
120
|
trim(bool: boolean): PricePretty {
|
105
121
|
const pretty = this.clone();
|
106
122
|
pretty.intPretty = pretty.intPretty.trim(bool);
|
@@ -175,29 +191,33 @@ export class PricePretty {
|
|
175
191
|
|
176
192
|
const dec = this.toDec();
|
177
193
|
const options = this.options;
|
178
|
-
|
179
|
-
|
180
|
-
|
194
|
+
|
195
|
+
if (
|
196
|
+
options.inequalitySymbol &&
|
197
|
+
!dec.isZero() &&
|
198
|
+
dec.abs().lt(DecUtils.getTenExponentN(-options.maxDecimals))
|
199
|
+
) {
|
200
|
+
return this.intPretty.toStringWithSymbols(
|
201
|
+
`${symbol}${this._options.separator}`,
|
202
|
+
""
|
181
203
|
);
|
182
|
-
if (dec.lt(precision)) {
|
183
|
-
const precisionLocaleString = parseFloat(
|
184
|
-
precision.toString(options.maxDecimals)
|
185
|
-
).toLocaleString(options.locale, {
|
186
|
-
maximumFractionDigits: options.maxDecimals,
|
187
|
-
});
|
188
|
-
|
189
|
-
return `< ${symbol}${this._options.separator}${precisionLocaleString}`;
|
190
|
-
}
|
191
204
|
}
|
192
205
|
|
193
|
-
|
206
|
+
let localeString = parseFloat(this.intPretty.toString()).toLocaleString(
|
194
207
|
options.locale,
|
195
208
|
{
|
196
209
|
maximumFractionDigits: options.maxDecimals,
|
197
210
|
}
|
198
211
|
);
|
199
212
|
|
200
|
-
|
213
|
+
const isNeg = localeString.charAt(0) === "-";
|
214
|
+
if (isNeg) {
|
215
|
+
localeString = localeString.slice(1);
|
216
|
+
}
|
217
|
+
|
218
|
+
return `${isNeg ? "-" : ""}${symbol}${
|
219
|
+
this._options.separator
|
220
|
+
}${localeString}`;
|
201
221
|
}
|
202
222
|
|
203
223
|
clone(): PricePretty {
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import { RatePretty } from "./rate-pretty";
|
2
|
+
import { Dec } from "./decimal";
|
3
|
+
|
4
|
+
describe("Test RatePretty", () => {
|
5
|
+
it("Basic test for RatePretty", () => {
|
6
|
+
const pretty = new RatePretty(new Dec("0.3"));
|
7
|
+
|
8
|
+
expect(pretty.toDec().equals(new Dec("0.3"))).toBe(true);
|
9
|
+
expect(pretty.toDec().toString(1)).toBe("0.3");
|
10
|
+
|
11
|
+
expect(pretty.toString()).toBe("30%");
|
12
|
+
expect(pretty.moveDecimalPointLeft(1).toString()).toBe("3%");
|
13
|
+
expect(pretty.moveDecimalPointRight(1).toString()).toBe("300%");
|
14
|
+
|
15
|
+
expect(pretty.add(new Dec("0.1")).toString()).toBe("40%");
|
16
|
+
expect(pretty.add(new Dec("0.1")).toDec().toString(1)).toBe("0.4");
|
17
|
+
expect(pretty.sub(new Dec("0.1")).toString()).toBe("20%");
|
18
|
+
expect(pretty.sub(new Dec("0.1")).toDec().toString(1)).toBe("0.2");
|
19
|
+
expect(pretty.mul(new Dec("0.1")).toString()).toBe("3%");
|
20
|
+
expect(pretty.mul(new Dec("0.1")).toDec().toString(2)).toBe("0.03");
|
21
|
+
expect(pretty.quo(new Dec("0.1")).toString()).toBe("300%");
|
22
|
+
expect(pretty.quo(new Dec("0.1")).toDec().toString(1)).toBe("3.0");
|
23
|
+
|
24
|
+
expect(new RatePretty(new Dec("0.001")).toString()).toBe("0.1%");
|
25
|
+
expect(new RatePretty(new Dec("0.00001")).toString()).toBe("0.001%");
|
26
|
+
expect(new RatePretty(new Dec("0.000001")).toString()).toBe("< 0.001%");
|
27
|
+
|
28
|
+
expect(new RatePretty(new Dec("0")).toString()).toBe("0%");
|
29
|
+
expect(new RatePretty(new Dec("-0")).toString()).toBe("0%");
|
30
|
+
|
31
|
+
expect(
|
32
|
+
new RatePretty(new Dec("0.000001")).separator(" ").symbol("?").toString()
|
33
|
+
).toBe("< 0.001 ?");
|
34
|
+
|
35
|
+
expect(
|
36
|
+
new RatePretty(new Dec("0.000001")).inequalitySymbol(false).toString()
|
37
|
+
).toBe("0%");
|
38
|
+
expect(
|
39
|
+
new RatePretty(new Dec("0.000001"))
|
40
|
+
.inequalitySymbol(false)
|
41
|
+
.maxDecimals(4)
|
42
|
+
.toString()
|
43
|
+
).toBe("0.0001%");
|
44
|
+
|
45
|
+
expect(new RatePretty(new Dec("-0.000001")).toString()).toBe("> -0.001%");
|
46
|
+
|
47
|
+
expect(
|
48
|
+
new RatePretty(new Dec("-0.000001")).inequalitySymbol(false).toString()
|
49
|
+
// TODO: Delete the case of "-0". Return "0"
|
50
|
+
).toBe("-0%");
|
51
|
+
});
|
52
|
+
});
|
@@ -0,0 +1,165 @@
|
|
1
|
+
import { IntPretty, IntPrettyOptions } from "./int-pretty";
|
2
|
+
import { Dec } from "./decimal";
|
3
|
+
import { DeepReadonly } from "utility-types";
|
4
|
+
import bigInteger from "big-integer";
|
5
|
+
|
6
|
+
export type RatePrettyOptions = {
|
7
|
+
separator: string;
|
8
|
+
symbol: string;
|
9
|
+
};
|
10
|
+
|
11
|
+
/**
|
12
|
+
* RatePretty treats `Dec` in rate form for easy calculation, and displays it as a percentage to the user by using toString().
|
13
|
+
* By default, if the value is less than maxDeciamls, it is displayed using an inequality sign (Ex. < 0.001%)
|
14
|
+
*/
|
15
|
+
export class RatePretty {
|
16
|
+
protected intPretty: IntPretty;
|
17
|
+
|
18
|
+
protected _options: RatePrettyOptions = {
|
19
|
+
separator: "",
|
20
|
+
symbol: "%",
|
21
|
+
};
|
22
|
+
|
23
|
+
constructor(protected amount: Dec | { toDec(): Dec } | bigInteger.BigNumber) {
|
24
|
+
this.intPretty = new IntPretty(amount);
|
25
|
+
|
26
|
+
this.intPretty = this.intPretty
|
27
|
+
.maxDecimals(3)
|
28
|
+
.shrink(false)
|
29
|
+
.trim(true)
|
30
|
+
.locale(true)
|
31
|
+
.inequalitySymbol(true);
|
32
|
+
}
|
33
|
+
|
34
|
+
get options(): DeepReadonly<
|
35
|
+
Omit<IntPrettyOptions, "locale"> & RatePrettyOptions
|
36
|
+
> {
|
37
|
+
return {
|
38
|
+
...this.intPretty.options,
|
39
|
+
...this._options,
|
40
|
+
};
|
41
|
+
}
|
42
|
+
|
43
|
+
separator(str: string): RatePretty {
|
44
|
+
const pretty = this.clone();
|
45
|
+
pretty._options.separator = str;
|
46
|
+
return pretty;
|
47
|
+
}
|
48
|
+
|
49
|
+
symbol(str: string): RatePretty {
|
50
|
+
const pretty = this.clone();
|
51
|
+
pretty._options.symbol = str;
|
52
|
+
return pretty;
|
53
|
+
}
|
54
|
+
|
55
|
+
moveDecimalPointLeft(delta: number): RatePretty {
|
56
|
+
const pretty = this.clone();
|
57
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointLeft(delta);
|
58
|
+
return pretty;
|
59
|
+
}
|
60
|
+
|
61
|
+
moveDecimalPointRight(delta: number): RatePretty {
|
62
|
+
const pretty = this.clone();
|
63
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointRight(delta);
|
64
|
+
return pretty;
|
65
|
+
}
|
66
|
+
|
67
|
+
maxDecimals(max: number): RatePretty {
|
68
|
+
const pretty = this.clone();
|
69
|
+
pretty.intPretty = pretty.intPretty.maxDecimals(max);
|
70
|
+
return pretty;
|
71
|
+
}
|
72
|
+
|
73
|
+
inequalitySymbol(bool: boolean): RatePretty {
|
74
|
+
const pretty = this.clone();
|
75
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbol(bool);
|
76
|
+
return pretty;
|
77
|
+
}
|
78
|
+
|
79
|
+
inequalitySymbolSeparator(str: string): RatePretty {
|
80
|
+
const pretty = this.clone();
|
81
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbolSeparator(str);
|
82
|
+
return pretty;
|
83
|
+
}
|
84
|
+
|
85
|
+
trim(bool: boolean): RatePretty {
|
86
|
+
const pretty = this.clone();
|
87
|
+
pretty.intPretty = pretty.intPretty.trim(bool);
|
88
|
+
return pretty;
|
89
|
+
}
|
90
|
+
|
91
|
+
shrink(bool: boolean): RatePretty {
|
92
|
+
const pretty = this.clone();
|
93
|
+
pretty.intPretty = pretty.intPretty.shrink(bool);
|
94
|
+
return pretty;
|
95
|
+
}
|
96
|
+
|
97
|
+
locale(locale: boolean): RatePretty {
|
98
|
+
const pretty = this.clone();
|
99
|
+
pretty.intPretty = pretty.intPretty.locale(locale);
|
100
|
+
return pretty;
|
101
|
+
}
|
102
|
+
|
103
|
+
/**
|
104
|
+
* Ready indicates the actual value is ready to show the users.
|
105
|
+
* Even if the ready option is false, it expects that the value can be shown to users (probably as 0).
|
106
|
+
* The method that returns prettied value may return `undefined` or `null` if the value is not ready.
|
107
|
+
* But, alternatively, it can return the 0 value that can be shown the users anyway, but indicates that the value is not ready.
|
108
|
+
* @param bool
|
109
|
+
*/
|
110
|
+
ready(bool: boolean): RatePretty {
|
111
|
+
const pretty = this.clone();
|
112
|
+
pretty.intPretty = pretty.intPretty.ready(bool);
|
113
|
+
return pretty;
|
114
|
+
}
|
115
|
+
|
116
|
+
get isReady(): boolean {
|
117
|
+
return this.intPretty.isReady;
|
118
|
+
}
|
119
|
+
|
120
|
+
add(target: Dec | { toDec(): Dec }): RatePretty {
|
121
|
+
const pretty = this.clone();
|
122
|
+
pretty.intPretty = pretty.intPretty.add(target);
|
123
|
+
return pretty;
|
124
|
+
}
|
125
|
+
|
126
|
+
sub(target: Dec | { toDec(): Dec }): RatePretty {
|
127
|
+
const pretty = this.clone();
|
128
|
+
pretty.intPretty = pretty.intPretty.sub(target);
|
129
|
+
return pretty;
|
130
|
+
}
|
131
|
+
|
132
|
+
mul(target: Dec | { toDec(): Dec }): RatePretty {
|
133
|
+
const pretty = this.clone();
|
134
|
+
pretty.intPretty = pretty.intPretty.mul(target);
|
135
|
+
return pretty;
|
136
|
+
}
|
137
|
+
|
138
|
+
quo(target: Dec | { toDec(): Dec }): RatePretty {
|
139
|
+
const pretty = this.clone();
|
140
|
+
pretty.intPretty = pretty.intPretty.quo(target);
|
141
|
+
return pretty;
|
142
|
+
}
|
143
|
+
|
144
|
+
toDec(): Dec {
|
145
|
+
return this.intPretty.toDec();
|
146
|
+
}
|
147
|
+
|
148
|
+
toString(): string {
|
149
|
+
return this.intPretty
|
150
|
+
.moveDecimalPointRight(2)
|
151
|
+
.toStringWithSymbols(
|
152
|
+
"",
|
153
|
+
`${this._options.separator}${this._options.symbol}`
|
154
|
+
);
|
155
|
+
}
|
156
|
+
|
157
|
+
clone(): RatePretty {
|
158
|
+
const pretty = new RatePretty(this.amount);
|
159
|
+
pretty._options = {
|
160
|
+
...this._options,
|
161
|
+
};
|
162
|
+
pretty.intPretty = this.intPretty.clone();
|
163
|
+
return pretty;
|
164
|
+
}
|
165
|
+
}
|