@keplr-wallet/unit 0.9.10-rc.0 → 0.9.11-rc.3

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.
Files changed (76) hide show
  1. package/build/coin-pretty.d.ts +13 -3
  2. package/build/coin-pretty.js +36 -25
  3. package/build/coin-pretty.js.map +1 -1
  4. package/build/coin-pretty.spec.js +194 -9
  5. package/build/coin-pretty.spec.js.map +1 -1
  6. package/build/coin-utils.js +8 -4
  7. package/build/coin-utils.js.map +1 -1
  8. package/build/coin-utils.spec.js +16 -0
  9. package/build/coin-utils.spec.js.map +1 -1
  10. package/build/coin.js +1 -1
  11. package/build/coin.js.map +1 -1
  12. package/build/coin.spec.js +15 -0
  13. package/build/coin.spec.js.map +1 -1
  14. package/build/dec-utils.d.ts +8 -1
  15. package/build/dec-utils.js +22 -20
  16. package/build/dec-utils.js.map +1 -1
  17. package/build/dec-utils.spec.js +19 -0
  18. package/build/dec-utils.spec.js.map +1 -1
  19. package/build/decimal.d.ts +22 -9
  20. package/build/decimal.js +102 -29
  21. package/build/decimal.js.map +1 -1
  22. package/build/decimal.spec.js +296 -3
  23. package/build/decimal.spec.js.map +1 -1
  24. package/build/etc.d.ts +4 -0
  25. package/build/etc.js +66 -0
  26. package/build/etc.js.map +1 -0
  27. package/build/etc.spec.d.ts +1 -0
  28. package/build/etc.spec.js +66 -0
  29. package/build/etc.spec.js.map +1 -0
  30. package/build/index.d.ts +1 -0
  31. package/build/index.js +1 -0
  32. package/build/index.js.map +1 -1
  33. package/build/int-pretty.d.ts +16 -4
  34. package/build/int-pretty.js +74 -36
  35. package/build/int-pretty.js.map +1 -1
  36. package/build/int-pretty.spec.js +261 -94
  37. package/build/int-pretty.spec.js.map +1 -1
  38. package/build/int.d.ts +17 -12
  39. package/build/int.js +69 -16
  40. package/build/int.js.map +1 -1
  41. package/build/int.spec.d.ts +1 -0
  42. package/build/int.spec.js +161 -0
  43. package/build/int.spec.js.map +1 -0
  44. package/build/price-pretty.d.ts +13 -3
  45. package/build/price-pretty.js +37 -26
  46. package/build/price-pretty.js.map +1 -1
  47. package/build/price-pretty.spec.js +59 -2
  48. package/build/price-pretty.spec.js.map +1 -1
  49. package/build/rate-pretty.d.ts +57 -0
  50. package/build/rate-pretty.js +128 -0
  51. package/build/rate-pretty.js.map +1 -0
  52. package/build/rate-pretty.spec.d.ts +1 -0
  53. package/build/rate-pretty.spec.js +38 -0
  54. package/build/rate-pretty.spec.js.map +1 -0
  55. package/package.json +3 -3
  56. package/src/coin-pretty.spec.ts +304 -11
  57. package/src/coin-pretty.ts +56 -29
  58. package/src/coin-utils.spec.ts +32 -0
  59. package/src/coin-utils.ts +12 -4
  60. package/src/coin.spec.ts +20 -0
  61. package/src/coin.ts +1 -1
  62. package/src/dec-utils.spec.ts +39 -0
  63. package/src/dec-utils.ts +25 -20
  64. package/src/decimal.spec.ts +361 -3
  65. package/src/decimal.ts +135 -56
  66. package/src/etc.spec.ts +73 -0
  67. package/src/etc.ts +73 -0
  68. package/src/index.ts +1 -0
  69. package/src/int-pretty.spec.ts +296 -101
  70. package/src/int-pretty.ts +87 -34
  71. package/src/int.spec.ts +212 -0
  72. package/src/int.ts +94 -26
  73. package/src/price-pretty.spec.ts +106 -2
  74. package/src/price-pretty.ts +50 -30
  75. package/src/rate-pretty.spec.ts +52 -0
  76. package/src/rate-pretty.ts +165 -0
@@ -16,14 +16,38 @@ describe("Test PricePretty", () => {
16
16
  expect(pretty.toString()).toBe("$12.1");
17
17
  expect(pretty.increasePrecision(1).toString()).toBe("$1.21");
18
18
  expect(pretty.decreasePrecision(1).toString()).toBe("$121");
19
- expect(pretty.precision(0).toString()).toBe("$121,234");
20
- expect(pretty.precision(-2).toString()).toBe("$12,123,400");
19
+ expect(pretty.moveDecimalPointLeft(1).toString()).toBe("$1.21");
20
+ expect(pretty.moveDecimalPointRight(1).toString()).toBe("$121");
21
21
 
22
22
  expect(pretty.add(new Dec("0.1")).toString()).toBe("$12.2");
23
23
  expect(pretty.sub(new Dec("0.1")).toString()).toBe("$12");
24
24
  expect(pretty.mul(new Dec("0.1")).toString()).toBe("$1.21");
25
25
  expect(pretty.quo(new Dec("0.1")).toString()).toBe("$121");
26
26
 
27
+ expect(
28
+ new PricePretty(
29
+ {
30
+ currency: "usd",
31
+ symbol: "$",
32
+ maxDecimals: 2,
33
+ locale: "en-US",
34
+ },
35
+ new Dec("0")
36
+ ).toString()
37
+ ).toBe("$0");
38
+
39
+ expect(
40
+ new PricePretty(
41
+ {
42
+ currency: "usd",
43
+ symbol: "$",
44
+ maxDecimals: 2,
45
+ locale: "en-US",
46
+ },
47
+ new Dec("-0")
48
+ ).toString()
49
+ ).toBe("$0");
50
+
27
51
  expect(
28
52
  new PricePretty(
29
53
  {
@@ -35,5 +59,85 @@ describe("Test PricePretty", () => {
35
59
  new Dec("0.001")
36
60
  ).toString()
37
61
  ).toBe("< $0.01");
62
+
63
+ expect(
64
+ new PricePretty(
65
+ {
66
+ currency: "usd",
67
+ symbol: "$",
68
+ maxDecimals: 2,
69
+ locale: "en-US",
70
+ },
71
+ new Dec("0.001")
72
+ )
73
+ .inequalitySymbol(false)
74
+ .toString()
75
+ ).toBe("$0");
76
+
77
+ expect(
78
+ new PricePretty(
79
+ {
80
+ currency: "usd",
81
+ symbol: "$",
82
+ maxDecimals: 2,
83
+ locale: "en-US",
84
+ },
85
+ new Dec("-0.001")
86
+ ).toString()
87
+ ).toBe("> -$0.01");
88
+
89
+ expect(
90
+ new PricePretty(
91
+ {
92
+ currency: "usd",
93
+ symbol: "$",
94
+ maxDecimals: 2,
95
+ locale: "en-US",
96
+ },
97
+ new Dec("-0.001")
98
+ )
99
+ .inequalitySymbol(false)
100
+ .toString()
101
+ // TODO: Delete the case of "-0". Return "0"
102
+ ).toBe("-$0");
103
+
104
+ expect(
105
+ new PricePretty(
106
+ {
107
+ currency: "usd",
108
+ symbol: "$",
109
+ maxDecimals: 3,
110
+ locale: "en-US",
111
+ },
112
+ new Dec("0.001")
113
+ ).toString()
114
+ ).toBe("$0.001");
115
+
116
+ expect(
117
+ new PricePretty(
118
+ {
119
+ currency: "usd",
120
+ symbol: "$",
121
+ maxDecimals: 3,
122
+ locale: "en-US",
123
+ },
124
+ new Dec("-0.001")
125
+ ).toString()
126
+ ).toBe("-$0.001");
127
+
128
+ // PricePretty's maxDecimals behave differently than IntPretty.
129
+ expect(
130
+ new PricePretty(
131
+ {
132
+ currency: "usd",
133
+ symbol: "$",
134
+ maxDecimals: 4,
135
+ locale: "en-US",
136
+ },
137
+ new Dec("0.001")
138
+ )
139
+ .trim(false)
140
+ .toString()
141
+ ).toBe("$0.001");
38
142
  });
39
143
  });
@@ -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
- if ("toDec" in amount) {
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
- precision(prec: number): PricePretty {
76
+ moveDecimalPointLeft(delta: number): PricePretty {
81
77
  const pretty = this.clone();
82
- pretty.intPretty = pretty.intPretty.precision(prec);
78
+ pretty.intPretty = pretty.intPretty.moveDecimalPointLeft(delta);
83
79
  return pretty;
84
80
  }
85
81
 
86
- increasePrecision(delta: number): PricePretty {
82
+ moveDecimalPointRight(delta: number): PricePretty {
87
83
  const pretty = this.clone();
88
- pretty.intPretty = pretty.intPretty.increasePrecision(delta);
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
- const pretty = this.clone();
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
- if (dec.gt(new Dec(0))) {
179
- const precision = new Dec(1).quo(
180
- DecUtils.getPrecisionDec(this.options.maxDecimals)
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
- const localeString = parseFloat(this.intPretty.toString()).toLocaleString(
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
- return `${symbol}${this._options.separator}${localeString}`;
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
+ }