@keplr-wallet/unit 0.9.9 → 0.9.11-rc.2
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 +22 -9
- package/build/decimal.js +102 -29
- package/build/decimal.js.map +1 -1
- package/build/decimal.spec.js +296 -3
- package/build/decimal.spec.js.map +1 -1
- package/build/etc.d.ts +4 -0
- package/build/etc.js +63 -0
- package/build/etc.js.map +1 -0
- package/build/etc.spec.d.ts +1 -0
- package/build/etc.spec.js +66 -0
- package/build/etc.spec.js.map +1 -0
- 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 +17 -12
- package/build/int.js +69 -16
- package/build/int.js.map +1 -1
- package/build/int.spec.d.ts +1 -0
- package/build/int.spec.js +161 -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 +361 -3
- package/src/decimal.ts +135 -56
- package/src/etc.spec.ts +73 -0
- package/src/etc.ts +70 -0
- 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 +212 -0
- package/src/int.ts +94 -26
- 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/coin-pretty.spec.ts
CHANGED
@@ -1,8 +1,104 @@
|
|
1
1
|
import { CoinPretty } from "./coin-pretty";
|
2
2
|
import { Dec } from "./decimal";
|
3
3
|
import { Int } from "./int";
|
4
|
+
import { CoinUtils } from "./coin-utils";
|
5
|
+
import { DecUtils } from "./dec-utils";
|
4
6
|
|
5
7
|
describe("Test CoinPretty", () => {
|
8
|
+
it("Test creation of CoinPretty", () => {
|
9
|
+
expect(
|
10
|
+
new CoinPretty(
|
11
|
+
{
|
12
|
+
coinDenom: "ATOM",
|
13
|
+
coinMinimalDenom: "uatom",
|
14
|
+
coinDecimals: 6,
|
15
|
+
},
|
16
|
+
new Int(1234)
|
17
|
+
)
|
18
|
+
.toDec()
|
19
|
+
.equals(new Dec("0.001234"))
|
20
|
+
).toBe(true);
|
21
|
+
expect(
|
22
|
+
new CoinPretty(
|
23
|
+
{
|
24
|
+
coinDenom: "ATOM",
|
25
|
+
coinMinimalDenom: "uatom",
|
26
|
+
coinDecimals: 6,
|
27
|
+
},
|
28
|
+
new Int(1234)
|
29
|
+
).toString()
|
30
|
+
).toBe("0.001234 ATOM");
|
31
|
+
|
32
|
+
expect(
|
33
|
+
new CoinPretty(
|
34
|
+
{
|
35
|
+
coinDenom: "ATOM",
|
36
|
+
coinMinimalDenom: "uatom",
|
37
|
+
coinDecimals: 6,
|
38
|
+
},
|
39
|
+
1234
|
40
|
+
)
|
41
|
+
.toDec()
|
42
|
+
.equals(new Dec("0.001234"))
|
43
|
+
).toBe(true);
|
44
|
+
expect(
|
45
|
+
new CoinPretty(
|
46
|
+
{
|
47
|
+
coinDenom: "ATOM",
|
48
|
+
coinMinimalDenom: "uatom",
|
49
|
+
coinDecimals: 6,
|
50
|
+
},
|
51
|
+
1234
|
52
|
+
).toString()
|
53
|
+
).toBe("0.001234 ATOM");
|
54
|
+
|
55
|
+
expect(
|
56
|
+
new CoinPretty(
|
57
|
+
{
|
58
|
+
coinDenom: "ATOM",
|
59
|
+
coinMinimalDenom: "uatom",
|
60
|
+
coinDecimals: 6,
|
61
|
+
},
|
62
|
+
"1234.5"
|
63
|
+
)
|
64
|
+
.toDec()
|
65
|
+
.equals(new Dec("0.0012345"))
|
66
|
+
).toBe(true);
|
67
|
+
expect(
|
68
|
+
new CoinPretty(
|
69
|
+
{
|
70
|
+
coinDenom: "ATOM",
|
71
|
+
coinMinimalDenom: "uatom",
|
72
|
+
coinDecimals: 6,
|
73
|
+
},
|
74
|
+
"1234.5"
|
75
|
+
).toString()
|
76
|
+
).toBe("0.001234 ATOM");
|
77
|
+
|
78
|
+
expect(
|
79
|
+
new CoinPretty(
|
80
|
+
{
|
81
|
+
coinDenom: "ATOM",
|
82
|
+
coinMinimalDenom: "uatom",
|
83
|
+
coinDecimals: 6,
|
84
|
+
},
|
85
|
+
new Dec("1234.5")
|
86
|
+
)
|
87
|
+
.toDec()
|
88
|
+
.equals(new Dec("0.0012345"))
|
89
|
+
).toBe(true);
|
90
|
+
expect(
|
91
|
+
new CoinPretty(
|
92
|
+
{
|
93
|
+
coinDenom: "ATOM",
|
94
|
+
coinMinimalDenom: "uatom",
|
95
|
+
coinDecimals: 6,
|
96
|
+
},
|
97
|
+
new Dec("1234.5")
|
98
|
+
).toString()
|
99
|
+
).toBe("0.001234 ATOM");
|
100
|
+
});
|
101
|
+
|
6
102
|
it("Basic test for CoinPretty", () => {
|
7
103
|
const pretty = new CoinPretty(
|
8
104
|
{
|
@@ -21,10 +117,11 @@ describe("Test CoinPretty", () => {
|
|
21
117
|
expect(pretty.decreasePrecision(1).trim(true).toString()).toBe(
|
22
118
|
"0.01234 ATOM"
|
23
119
|
);
|
24
|
-
expect(pretty.
|
25
|
-
expect(pretty.
|
26
|
-
expect(pretty.
|
27
|
-
|
120
|
+
expect(pretty.moveDecimalPointLeft(1).toString()).toBe("0.000123 ATOM");
|
121
|
+
expect(pretty.moveDecimalPointRight(1).toString()).toBe("0.012340 ATOM");
|
122
|
+
expect(pretty.moveDecimalPointRight(1).trim(true).toString()).toBe(
|
123
|
+
"0.01234 ATOM"
|
124
|
+
);
|
28
125
|
|
29
126
|
expect(pretty.maxDecimals(7).add(new Dec("0.1")).toString()).toBe(
|
30
127
|
"0.0012341 ATOM"
|
@@ -110,17 +207,12 @@ describe("Test CoinPretty", () => {
|
|
110
207
|
new Dec("12.1234")
|
111
208
|
);
|
112
209
|
|
113
|
-
expect(pretty.options.precision).toBe(10);
|
114
210
|
expect(pretty.toString()).toBe("0.000012 ATOM");
|
115
211
|
expect(pretty.maxDecimals(10).toString()).toBe("0.0000121234 ATOM");
|
116
212
|
expect(pretty.increasePrecision(1).toString()).toBe("0.000001 ATOM");
|
117
213
|
expect(pretty.decreasePrecision(1).toString()).toBe("0.000121 ATOM");
|
118
|
-
expect(pretty.
|
119
|
-
expect(pretty.
|
120
|
-
expect(pretty.precision(-2).toString()).toBe("12,123,400.000000 ATOM");
|
121
|
-
expect(pretty.precision(-2).shrink(true).toString()).toBe(
|
122
|
-
"12,123,400.0 ATOM"
|
123
|
-
);
|
214
|
+
expect(pretty.moveDecimalPointLeft(1).toString()).toBe("0.000001 ATOM");
|
215
|
+
expect(pretty.moveDecimalPointRight(1).toString()).toBe("0.000121 ATOM");
|
124
216
|
|
125
217
|
expect(pretty.maxDecimals(7).add(new Dec("0.1")).toString()).toBe(
|
126
218
|
"0.0000122 ATOM"
|
@@ -222,4 +314,205 @@ describe("Test CoinPretty", () => {
|
|
222
314
|
amount: "12345600",
|
223
315
|
});
|
224
316
|
});
|
317
|
+
|
318
|
+
it("Test CoinPretty's setCurrency", () => {
|
319
|
+
const pretty = new CoinPretty(
|
320
|
+
{
|
321
|
+
coinDenom: "ATOM",
|
322
|
+
coinMinimalDenom: "uatom",
|
323
|
+
coinDecimals: 6,
|
324
|
+
},
|
325
|
+
new Int("123456")
|
326
|
+
);
|
327
|
+
|
328
|
+
expect(
|
329
|
+
pretty
|
330
|
+
.setCurrency({
|
331
|
+
coinDenom: "TEST",
|
332
|
+
coinMinimalDenom: "utest",
|
333
|
+
coinDecimals: 6,
|
334
|
+
})
|
335
|
+
.toString()
|
336
|
+
).toBe("0.123456 TEST");
|
337
|
+
|
338
|
+
expect(
|
339
|
+
pretty
|
340
|
+
.setCurrency({
|
341
|
+
coinDenom: "TEST",
|
342
|
+
coinMinimalDenom: "utest",
|
343
|
+
coinDecimals: 3,
|
344
|
+
})
|
345
|
+
.toString()
|
346
|
+
).toBe("123.456000 TEST");
|
347
|
+
|
348
|
+
expect(
|
349
|
+
pretty
|
350
|
+
.setCurrency({
|
351
|
+
coinDenom: "TEST",
|
352
|
+
coinMinimalDenom: "utest",
|
353
|
+
coinDecimals: 3,
|
354
|
+
})
|
355
|
+
.moveDecimalPointLeft(2)
|
356
|
+
.moveDecimalPointRight(1)
|
357
|
+
.trim(true)
|
358
|
+
.toString()
|
359
|
+
).toBe("12.3456 TEST");
|
360
|
+
});
|
361
|
+
|
362
|
+
it("Test CoinPretty's toString()", () => {
|
363
|
+
const tests: {
|
364
|
+
base: CoinPretty;
|
365
|
+
pre: (pretty: CoinPretty) => CoinPretty;
|
366
|
+
res: string;
|
367
|
+
}[] = [
|
368
|
+
{
|
369
|
+
base: new CoinPretty(
|
370
|
+
{
|
371
|
+
coinDenom: "ATOM",
|
372
|
+
coinMinimalDenom: "uatom",
|
373
|
+
coinDecimals: 6,
|
374
|
+
},
|
375
|
+
new Int("12345600")
|
376
|
+
),
|
377
|
+
pre: (pretty) => pretty,
|
378
|
+
res: "12.345600 ATOM",
|
379
|
+
},
|
380
|
+
{
|
381
|
+
base: new CoinPretty(
|
382
|
+
{
|
383
|
+
coinDenom: "ATOM",
|
384
|
+
coinMinimalDenom: "uatom",
|
385
|
+
coinDecimals: 6,
|
386
|
+
},
|
387
|
+
new Int("12345600")
|
388
|
+
),
|
389
|
+
pre: (pretty) => pretty.hideDenom(true),
|
390
|
+
res: "12.345600",
|
391
|
+
},
|
392
|
+
{
|
393
|
+
base: new CoinPretty(
|
394
|
+
{
|
395
|
+
coinDenom: "ATOM",
|
396
|
+
coinMinimalDenom: "uatom",
|
397
|
+
coinDecimals: 6,
|
398
|
+
},
|
399
|
+
new Int("12345600")
|
400
|
+
),
|
401
|
+
pre: (pretty) => pretty.separator(""),
|
402
|
+
res: "12.345600ATOM",
|
403
|
+
},
|
404
|
+
{
|
405
|
+
base: new CoinPretty(
|
406
|
+
{
|
407
|
+
coinDenom: "ATOM",
|
408
|
+
coinMinimalDenom: "uatom",
|
409
|
+
coinDecimals: 6,
|
410
|
+
},
|
411
|
+
new Int("12345600")
|
412
|
+
),
|
413
|
+
pre: (pretty) => pretty.maxDecimals(3),
|
414
|
+
res: "12.345 ATOM",
|
415
|
+
},
|
416
|
+
{
|
417
|
+
base: new CoinPretty(
|
418
|
+
{
|
419
|
+
coinDenom: "ATOM",
|
420
|
+
coinMinimalDenom: "uatom",
|
421
|
+
coinDecimals: 6,
|
422
|
+
},
|
423
|
+
new Int("12345600")
|
424
|
+
),
|
425
|
+
pre: (pretty) => pretty.maxDecimals(3).separator(""),
|
426
|
+
res: "12.345ATOM",
|
427
|
+
},
|
428
|
+
{
|
429
|
+
base: new CoinPretty(
|
430
|
+
{
|
431
|
+
coinDenom: "ATOM",
|
432
|
+
coinMinimalDenom: "uatom",
|
433
|
+
coinDecimals: 6,
|
434
|
+
},
|
435
|
+
new Int("12345600")
|
436
|
+
),
|
437
|
+
pre: (pretty) => pretty.lowerCase(true),
|
438
|
+
res: "12.345600 atom",
|
439
|
+
},
|
440
|
+
{
|
441
|
+
base: new CoinPretty(
|
442
|
+
{
|
443
|
+
coinDenom: "AtoM",
|
444
|
+
coinMinimalDenom: "uatom",
|
445
|
+
coinDecimals: 6,
|
446
|
+
},
|
447
|
+
new Int("12345600")
|
448
|
+
),
|
449
|
+
pre: (pretty) => pretty.upperCase(true),
|
450
|
+
res: "12.345600 ATOM",
|
451
|
+
},
|
452
|
+
{
|
453
|
+
base: new CoinPretty(
|
454
|
+
{
|
455
|
+
coinDenom: "ATOM",
|
456
|
+
coinMinimalDenom: "uatom",
|
457
|
+
coinDecimals: 6,
|
458
|
+
},
|
459
|
+
new Int("1234560000")
|
460
|
+
),
|
461
|
+
pre: (pretty) => pretty,
|
462
|
+
res: "1,234.560000 ATOM",
|
463
|
+
},
|
464
|
+
{
|
465
|
+
base: new CoinPretty(
|
466
|
+
{
|
467
|
+
coinDenom: "ATOM",
|
468
|
+
coinMinimalDenom: "uatom",
|
469
|
+
coinDecimals: 6,
|
470
|
+
},
|
471
|
+
new Int("1234560000")
|
472
|
+
),
|
473
|
+
pre: (pretty) => pretty.locale(false),
|
474
|
+
res: "1234.560000 ATOM",
|
475
|
+
},
|
476
|
+
{
|
477
|
+
base: new CoinPretty(
|
478
|
+
{
|
479
|
+
coinDenom: "ATOM",
|
480
|
+
coinMinimalDenom: "uatom",
|
481
|
+
coinDecimals: 6,
|
482
|
+
},
|
483
|
+
new Int("1234560000")
|
484
|
+
),
|
485
|
+
pre: (pretty) => pretty.shrink(true),
|
486
|
+
res:
|
487
|
+
CoinUtils.shrinkDecimals(
|
488
|
+
new Dec("1234560000").mul(DecUtils.getTenExponentN(-6)),
|
489
|
+
0,
|
490
|
+
6,
|
491
|
+
true
|
492
|
+
) + " ATOM",
|
493
|
+
},
|
494
|
+
{
|
495
|
+
base: new CoinPretty(
|
496
|
+
{
|
497
|
+
coinDenom: "ATOM",
|
498
|
+
coinMinimalDenom: "uatom",
|
499
|
+
coinDecimals: 6,
|
500
|
+
},
|
501
|
+
new Int("1234560000")
|
502
|
+
),
|
503
|
+
pre: (pretty) => pretty.locale(false).shrink(true),
|
504
|
+
res:
|
505
|
+
CoinUtils.shrinkDecimals(
|
506
|
+
new Dec("1234560000").mul(DecUtils.getTenExponentN(-6)),
|
507
|
+
0,
|
508
|
+
6,
|
509
|
+
false
|
510
|
+
) + " ATOM",
|
511
|
+
},
|
512
|
+
];
|
513
|
+
|
514
|
+
for (const test of tests) {
|
515
|
+
expect(test.pre(test.base).toString()).toBe(test.res);
|
516
|
+
}
|
517
|
+
});
|
225
518
|
});
|
package/src/coin-pretty.ts
CHANGED
@@ -3,6 +3,7 @@ import { Dec } from "./decimal";
|
|
3
3
|
import { AppCurrency } 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 CoinPrettyOptions = {
|
8
9
|
separator: string;
|
@@ -23,23 +24,19 @@ export class CoinPretty {
|
|
23
24
|
|
24
25
|
constructor(
|
25
26
|
protected _currency: AppCurrency,
|
26
|
-
protected amount: Dec | { toDec(): Dec }
|
27
|
+
protected amount: Dec | { toDec(): Dec } | bigInteger.BigNumber
|
27
28
|
) {
|
28
|
-
if ("toDec" in this.amount) {
|
29
|
+
if (typeof this.amount === "object" && "toDec" in this.amount) {
|
29
30
|
this.amount = this.amount.toDec();
|
31
|
+
} else if (!(this.amount instanceof Dec)) {
|
32
|
+
this.amount = new Dec(this.amount);
|
30
33
|
}
|
31
34
|
|
32
|
-
this.intPretty = new IntPretty(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
.precision(_currency.coinDecimals);
|
38
|
-
} else {
|
39
|
-
this.intPretty = this.intPretty
|
40
|
-
.maxDecimals(_currency.coinDecimals)
|
41
|
-
.mul(DecUtils.getPrecisionDec(-_currency.coinDecimals));
|
42
|
-
}
|
35
|
+
this.intPretty = new IntPretty(
|
36
|
+
this.amount.quoTruncate(
|
37
|
+
DecUtils.getTenExponentNInPrecisionRange(_currency.coinDecimals)
|
38
|
+
)
|
39
|
+
).maxDecimals(_currency.coinDecimals);
|
43
40
|
}
|
44
41
|
|
45
42
|
get options(): DeepReadonly<IntPrettyOptions & CoinPrettyOptions> {
|
@@ -58,11 +55,11 @@ export class CoinPretty {
|
|
58
55
|
}
|
59
56
|
|
60
57
|
setCurrency(currency: AppCurrency): CoinPretty {
|
61
|
-
const pretty =
|
62
|
-
pretty.
|
63
|
-
|
64
|
-
|
65
|
-
pretty.
|
58
|
+
const pretty = this.clone();
|
59
|
+
pretty.intPretty = this.intPretty.moveDecimalPointRight(
|
60
|
+
this._currency.coinDecimals - currency.coinDecimals
|
61
|
+
);
|
62
|
+
pretty._currency = currency;
|
66
63
|
return pretty;
|
67
64
|
}
|
68
65
|
|
@@ -92,22 +89,30 @@ export class CoinPretty {
|
|
92
89
|
return pretty;
|
93
90
|
}
|
94
91
|
|
95
|
-
|
92
|
+
moveDecimalPointLeft(delta: number): CoinPretty {
|
96
93
|
const pretty = this.clone();
|
97
|
-
pretty.intPretty = pretty.intPretty.
|
94
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointLeft(delta);
|
98
95
|
return pretty;
|
99
96
|
}
|
100
97
|
|
101
|
-
|
98
|
+
moveDecimalPointRight(delta: number): CoinPretty {
|
102
99
|
const pretty = this.clone();
|
103
|
-
pretty.intPretty = pretty.intPretty.
|
100
|
+
pretty.intPretty = pretty.intPretty.moveDecimalPointRight(delta);
|
104
101
|
return pretty;
|
105
102
|
}
|
106
103
|
|
104
|
+
/**
|
105
|
+
* @deprecated Use`moveDecimalPointLeft`
|
106
|
+
*/
|
107
|
+
increasePrecision(delta: number): CoinPretty {
|
108
|
+
return this.moveDecimalPointLeft(delta);
|
109
|
+
}
|
110
|
+
|
111
|
+
/**
|
112
|
+
* @deprecated Use`moveDecimalPointRight`
|
113
|
+
*/
|
107
114
|
decreasePrecision(delta: number): CoinPretty {
|
108
|
-
|
109
|
-
pretty.intPretty = pretty.intPretty.decreasePrecision(delta);
|
110
|
-
return pretty;
|
115
|
+
return this.moveDecimalPointRight(delta);
|
111
116
|
}
|
112
117
|
|
113
118
|
maxDecimals(max: number): CoinPretty {
|
@@ -116,6 +121,18 @@ export class CoinPretty {
|
|
116
121
|
return pretty;
|
117
122
|
}
|
118
123
|
|
124
|
+
inequalitySymbol(bool: boolean): CoinPretty {
|
125
|
+
const pretty = this.clone();
|
126
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbol(bool);
|
127
|
+
return pretty;
|
128
|
+
}
|
129
|
+
|
130
|
+
inequalitySymbolSeparator(str: string): CoinPretty {
|
131
|
+
const pretty = this.clone();
|
132
|
+
pretty.intPretty = pretty.intPretty.inequalitySymbolSeparator(str);
|
133
|
+
return pretty;
|
134
|
+
}
|
135
|
+
|
119
136
|
trim(bool: boolean): CoinPretty {
|
120
137
|
const pretty = this.clone();
|
121
138
|
pretty.intPretty = pretty.intPretty.trim(bool);
|
@@ -171,7 +188,11 @@ export class CoinPretty {
|
|
171
188
|
pretty.intPretty = pretty.intPretty.add(
|
172
189
|
isCoinPretty
|
173
190
|
? target
|
174
|
-
: target.mul(
|
191
|
+
: target.mul(
|
192
|
+
DecUtils.getTenExponentNInPrecisionRange(
|
193
|
+
-this._currency.coinDecimals
|
194
|
+
)
|
195
|
+
)
|
175
196
|
);
|
176
197
|
return pretty;
|
177
198
|
}
|
@@ -196,7 +217,11 @@ export class CoinPretty {
|
|
196
217
|
pretty.intPretty = pretty.intPretty.sub(
|
197
218
|
isCoinPretty
|
198
219
|
? target
|
199
|
-
: target.mul(
|
220
|
+
: target.mul(
|
221
|
+
DecUtils.getTenExponentNInPrecisionRange(
|
222
|
+
-this._currency.coinDecimals
|
223
|
+
)
|
224
|
+
)
|
200
225
|
);
|
201
226
|
return pretty;
|
202
227
|
}
|
@@ -222,7 +247,9 @@ export class CoinPretty {
|
|
222
247
|
amount: string;
|
223
248
|
} {
|
224
249
|
const amount = this.toDec()
|
225
|
-
.
|
250
|
+
.mulTruncate(
|
251
|
+
DecUtils.getTenExponentNInPrecisionRange(this.currency.coinDecimals)
|
252
|
+
)
|
226
253
|
.truncate();
|
227
254
|
|
228
255
|
return {
|
@@ -247,7 +274,7 @@ export class CoinPretty {
|
|
247
274
|
separator = "";
|
248
275
|
}
|
249
276
|
|
250
|
-
return
|
277
|
+
return this.intPretty.toStringWithSymbols("", `${separator}${denom}`);
|
251
278
|
}
|
252
279
|
|
253
280
|
clone(): CoinPretty {
|
package/src/coin-utils.spec.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import { CoinUtils } from "./coin-utils";
|
2
|
+
import { Dec } from "./decimal";
|
2
3
|
|
3
4
|
describe("Test CoinUtils", () => {
|
4
5
|
it("Test integerStringToUSLocaleString", () => {
|
@@ -11,5 +12,36 @@ describe("Test CoinUtils", () => {
|
|
11
12
|
expect(CoinUtils.integerStringToUSLocaleString("123")).toBe("123");
|
12
13
|
expect(CoinUtils.integerStringToUSLocaleString("12")).toBe("12");
|
13
14
|
expect(CoinUtils.integerStringToUSLocaleString("1234")).toBe("1,234");
|
15
|
+
|
16
|
+
expect(CoinUtils.integerStringToUSLocaleString("-1234")).toBe("-1,234");
|
17
|
+
expect(CoinUtils.integerStringToUSLocaleString("-123456789")).toBe(
|
18
|
+
"-123,456,789"
|
19
|
+
);
|
20
|
+
});
|
21
|
+
|
22
|
+
it("Test shrink", () => {
|
23
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234.56789), 3, 6)).toBe(
|
24
|
+
"1234.567"
|
25
|
+
);
|
26
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234.56789), 3, 6, true)).toBe(
|
27
|
+
"1,234.567"
|
28
|
+
);
|
29
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234), 3, 6)).toBe("1234.000");
|
30
|
+
|
31
|
+
expect(CoinUtils.shrinkDecimals(new Dec(-1234.56789), 3, 6)).toBe(
|
32
|
+
"-1234.567"
|
33
|
+
);
|
34
|
+
expect(CoinUtils.shrinkDecimals(new Dec(-1234.56789), 3, 6, true)).toBe(
|
35
|
+
"-1,234.567"
|
36
|
+
);
|
37
|
+
|
38
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234.56789), 0, 0)).toBe("1234");
|
39
|
+
expect(CoinUtils.shrinkDecimals(new Dec(-1234.56789), 0, 0)).toBe("-1234");
|
40
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234.56789), 1, 1)).toBe("1234.5");
|
41
|
+
expect(CoinUtils.shrinkDecimals(new Dec(-1234.56789), 1, 1)).toBe(
|
42
|
+
"-1234.5"
|
43
|
+
);
|
44
|
+
expect(CoinUtils.shrinkDecimals(new Dec(1234), 1, 1)).toBe("1234.0");
|
45
|
+
expect(CoinUtils.shrinkDecimals(new Dec(-1234), 1, 1)).toBe("-1234.0");
|
14
46
|
});
|
15
47
|
});
|
package/src/coin-utils.ts
CHANGED
@@ -123,21 +123,29 @@ export class CoinUtils {
|
|
123
123
|
return "0";
|
124
124
|
}
|
125
125
|
|
126
|
-
const
|
127
|
-
|
126
|
+
const isNeg = dec.isNegative();
|
127
|
+
|
128
|
+
const integer = dec.abs().truncate();
|
129
|
+
const fraction = dec.abs().sub(new Dec(integer));
|
128
130
|
|
129
131
|
const decimals = Math.max(
|
130
132
|
maxDecimals - integer.toString().length + 1,
|
131
133
|
minDecimals
|
132
134
|
);
|
133
135
|
|
134
|
-
const fractionStr =
|
136
|
+
const fractionStr =
|
137
|
+
decimals === 0 ? "" : fraction.toString(decimals).replace("0.", "");
|
135
138
|
|
136
139
|
const integerStr = locale
|
137
140
|
? CoinUtils.integerStringToUSLocaleString(integer.toString())
|
138
141
|
: integer.toString();
|
139
142
|
|
140
|
-
return
|
143
|
+
return (
|
144
|
+
(isNeg ? "-" : "") +
|
145
|
+
integerStr +
|
146
|
+
(fractionStr.length > 0 ? "." : "") +
|
147
|
+
fractionStr
|
148
|
+
);
|
141
149
|
}
|
142
150
|
|
143
151
|
/**
|
package/src/coin.spec.ts
CHANGED
@@ -6,15 +6,35 @@ describe("Test coin", () => {
|
|
6
6
|
|
7
7
|
expect(coin.denom).toBe("test");
|
8
8
|
expect(coin.amount.toString()).toBe("1000");
|
9
|
+
expect(coin.toString()).toBe("1000test");
|
9
10
|
|
10
11
|
coin = Coin.parse("1000tesT");
|
11
12
|
|
12
13
|
expect(coin.denom).toBe("tesT");
|
13
14
|
expect(coin.amount.toString()).toBe("1000");
|
15
|
+
expect(coin.toString()).toBe("1000tesT");
|
14
16
|
|
15
17
|
coin = Coin.parse("1000TEST");
|
16
18
|
|
17
19
|
expect(coin.denom).toBe("TEST");
|
18
20
|
expect(coin.amount.toString()).toBe("1000");
|
21
|
+
expect(coin.toString()).toBe("1000TEST");
|
22
|
+
|
23
|
+
coin = Coin.parse("1000 TEST");
|
24
|
+
|
25
|
+
expect(coin.denom).toBe("TEST");
|
26
|
+
expect(coin.amount.toString()).toBe("1000");
|
27
|
+
expect(coin.toString()).toBe("1000TEST");
|
28
|
+
|
29
|
+
coin = Coin.parse("1000 TEST");
|
30
|
+
|
31
|
+
expect(coin.denom).toBe("TEST");
|
32
|
+
expect(coin.amount.toString()).toBe("1000");
|
33
|
+
expect(coin.toString()).toBe("1000TEST");
|
34
|
+
|
35
|
+
expect(() => Coin.parse("ascasc")).toThrow();
|
36
|
+
expect(() => Coin.parse("ascasc asd")).toThrow();
|
37
|
+
expect(() => Coin.parse("100 ascasc asd")).toThrow();
|
38
|
+
expect(() => Coin.parse("asd 100")).toThrow();
|
19
39
|
});
|
20
40
|
});
|
package/src/coin.ts
CHANGED
@@ -3,7 +3,7 @@ import bigInteger from "big-integer";
|
|
3
3
|
|
4
4
|
export class Coin {
|
5
5
|
public static parse(str: string): Coin {
|
6
|
-
const re = new RegExp("([0-9]+)[ ]*([a-zA-Z]+)");
|
6
|
+
const re = new RegExp("([0-9]+)[ ]*([a-zA-Z]+)$");
|
7
7
|
const execed = re.exec(str);
|
8
8
|
if (!execed || execed.length !== 3) {
|
9
9
|
throw new Error("Invalid coin str");
|
package/src/dec-utils.spec.ts
CHANGED
@@ -2,6 +2,13 @@ import { DecUtils } from "./dec-utils";
|
|
2
2
|
import { Dec } from "./decimal";
|
3
3
|
|
4
4
|
describe("Test DecUtils", () => {
|
5
|
+
it("Test trim in DecUtils", () => {
|
6
|
+
expect(DecUtils.trim("0.00100")).toBe("0.001");
|
7
|
+
expect(DecUtils.trim("0.00")).toBe("0");
|
8
|
+
expect(DecUtils.trim("-0.00100")).toBe("-0.001");
|
9
|
+
expect(DecUtils.trim("-0.00")).toBe("-0");
|
10
|
+
});
|
11
|
+
|
5
12
|
it("getPrecisionDec should return the (10^precision)", () => {
|
6
13
|
expect(DecUtils.getPrecisionDec(-1).toString()).toBe(
|
7
14
|
new Dec("0.1").toString()
|
@@ -34,4 +41,36 @@ describe("Test DecUtils", () => {
|
|
34
41
|
DecUtils.getPrecisionDec(-19);
|
35
42
|
}).toThrow();
|
36
43
|
});
|
44
|
+
|
45
|
+
it("getTenExponentN should return same cached result", () => {
|
46
|
+
for (let i = 0; i < 3; i++) {
|
47
|
+
expect(DecUtils.getTenExponentN(5).toString()).toBe(
|
48
|
+
new Dec(100000).toString()
|
49
|
+
);
|
50
|
+
}
|
51
|
+
});
|
52
|
+
|
53
|
+
it("Test getTenExponentN", () => {
|
54
|
+
expect(DecUtils.getTenExponentN(0).toString()).toBe(
|
55
|
+
new Dec("1").toString()
|
56
|
+
);
|
57
|
+
|
58
|
+
expect(DecUtils.getTenExponentN(1).toString()).toBe(
|
59
|
+
new Dec("10").toString()
|
60
|
+
);
|
61
|
+
|
62
|
+
expect(DecUtils.getTenExponentN(10).toString()).toBe(
|
63
|
+
new Dec("10000000000").toString()
|
64
|
+
);
|
65
|
+
|
66
|
+
expect(DecUtils.getTenExponentN(20).toString()).toBe(
|
67
|
+
new Dec("100000000000000000000").toString()
|
68
|
+
);
|
69
|
+
|
70
|
+
expect(DecUtils.getTenExponentN(-18).toString()).toBe(
|
71
|
+
new Dec("0.000000000000000001").toString()
|
72
|
+
);
|
73
|
+
|
74
|
+
expect(() => DecUtils.getTenExponentN(-19)).toThrow();
|
75
|
+
});
|
37
76
|
});
|