jg-bignumber 1.0.0 → 1.1.0
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/API.md +754 -0
- package/CHANGELOG.md +23 -9
- package/README.md +1 -17
- package/dist/calculations/calculations.d.ts +39 -17
- package/dist/calculations/calculations.js +39 -17
- package/dist/calculations/calculations.js.map +1 -1
- package/dist/processing/processing.d.ts +29 -16
- package/dist/processing/processing.js +35 -19
- package/dist/processing/processing.js.map +1 -1
- package/dist/utils/utils.js +9 -6
- package/dist/utils/utils.js.map +1 -1
- package/dist/validations/validations.js +8 -4
- package/dist/validations/validations.js.map +1 -1
- package/package.json +8 -4
package/API.md
ADDED
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
# <a id="api-reference"></a>jg-bignumber API reference
|
|
2
|
+
|
|
3
|
+
Every export of `jg-bignumber@1.1.0`, 29 in all, with its signature, its errors, and an example. It's generated from the package's TSDoc.
|
|
4
|
+
|
|
5
|
+
- Functions: [`adjustByPercentage`](#api-adjustByPercentage), [`calculateExchange`](#api-calculateExchange), [`calculateExchangeFee`](#api-calculateExchangeFee), [`calculateMax`](#api-calculateMax), [`calculateMean`](#api-calculateMean), [`calculateMedian`](#api-calculateMedian), [`calculateMin`](#api-calculateMin), [`calculatePercentageChange`](#api-calculatePercentageChange), [`calculatePercentageRepresentation`](#api-calculatePercentageRepresentation), [`calculateSum`](#api-calculateSum), [`calculateWeightedEntry`](#api-calculateWeightedEntry), [`getBigNumber`](#api-getBigNumber), [`isBigNumber`](#api-isBigNumber), [`isFloat`](#api-isFloat), [`isInteger`](#api-isInteger), [`isNumber`](#api-isNumber), [`prettifyValue`](#api-prettifyValue), [`processValue`](#api-processValue)
|
|
6
|
+
- Constants: [`BigNumber`](#api-BigNumber), [`ERRORS`](#api-ERRORS)
|
|
7
|
+
- Types: [`IBigNumber`](#api-IBigNumber), [`IBigNumberFormat`](#api-IBigNumberFormat), [`IBigNumberRoundingMode`](#api-IBigNumberRoundingMode), [`IBigNumberRoundingModeName`](#api-IBigNumberRoundingModeName), [`IBigNumberToType`](#api-IBigNumberToType), [`IBigNumberValue`](#api-IBigNumberValue), [`IConfig`](#api-IConfig), [`IOutput`](#api-IOutput), [`IType`](#api-IType)
|
|
8
|
+
|
|
9
|
+
## <a id="api-functions"></a>Functions
|
|
10
|
+
|
|
11
|
+
### <a id="api-adjustByPercentage"></a>`adjustByPercentage`
|
|
12
|
+
|
|
13
|
+
*Function*
|
|
14
|
+
|
|
15
|
+
Changes a value by a percentage: a positive percentage increases it, and a negative one decreases it, to 0 at most.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
adjustByPercentage<T extends Partial<IConfig>>(
|
|
19
|
+
value: Value,
|
|
20
|
+
percentage: Value,
|
|
21
|
+
config?: T,
|
|
22
|
+
): IOutput<T>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Parameters**
|
|
26
|
+
|
|
27
|
+
- `value` `Value`: The value to change.
|
|
28
|
+
- `percentage` `Value`: The percentage to change it by.
|
|
29
|
+
- `config?` `T`: How the result is processed.
|
|
30
|
+
|
|
31
|
+
**Returns** `IOutput<T>`: The changed value.
|
|
32
|
+
|
|
33
|
+
**Throws**
|
|
34
|
+
|
|
35
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
36
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
37
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
38
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
39
|
+
- `NEGATIVE_VALUE_NOT_ALLOWED`: if the value is not positive
|
|
40
|
+
|
|
41
|
+
**Example**
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import { adjustByPercentage } from 'jg-bignumber';
|
|
45
|
+
|
|
46
|
+
adjustByPercentage(100, 25); // => 125
|
|
47
|
+
adjustByPercentage(100, -10); // => 90
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### <a id="api-calculateExchange"></a>`calculateExchange`
|
|
51
|
+
|
|
52
|
+
*Function*
|
|
53
|
+
|
|
54
|
+
Calculates the amount of an asset an exchange gives for a value at a rate, such as how much BTC 100 USDT buys at 65,000 USDT per BTC.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
calculateExchange<T extends Partial<IConfig>>(
|
|
58
|
+
value: Value,
|
|
59
|
+
rate: Value,
|
|
60
|
+
config?: T,
|
|
61
|
+
): IOutput<T>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Parameters**
|
|
65
|
+
|
|
66
|
+
- `value` `Value`: The amount being exchanged.
|
|
67
|
+
- `rate` `Value`: The price of one unit of the asset received.
|
|
68
|
+
- `config?` `T`: How the result is processed.
|
|
69
|
+
|
|
70
|
+
**Returns** `IOutput<T>`: The amount received.
|
|
71
|
+
|
|
72
|
+
**Throws**
|
|
73
|
+
|
|
74
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
75
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
76
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
77
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
78
|
+
- `NEGATIVE_VALUE_NOT_ALLOWED`: if the value or rate are not positive
|
|
79
|
+
|
|
80
|
+
**Example**
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { calculateExchange } from 'jg-bignumber';
|
|
84
|
+
|
|
85
|
+
calculateExchange(100, 65000, { decimalPlaces: 8 }); // => 0.00153846
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### <a id="api-calculateExchangeFee"></a>`calculateExchangeFee`
|
|
89
|
+
|
|
90
|
+
*Function*
|
|
91
|
+
|
|
92
|
+
Calculates the fee charged for an exchange, as a percentage of the value exchanged.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
calculateExchangeFee<T extends Partial<IConfig>>(
|
|
96
|
+
value: Value,
|
|
97
|
+
feePercentage: Value,
|
|
98
|
+
config?: T,
|
|
99
|
+
): IOutput<T>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Parameters**
|
|
103
|
+
|
|
104
|
+
- `value` `Value`: The amount being exchanged.
|
|
105
|
+
- `feePercentage` `Value`: The fee, as a percentage.
|
|
106
|
+
- `config?` `T`: How the result is processed.
|
|
107
|
+
|
|
108
|
+
**Returns** `IOutput<T>`: The fee.
|
|
109
|
+
|
|
110
|
+
**Throws**
|
|
111
|
+
|
|
112
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
113
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
114
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
115
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
116
|
+
- `NEGATIVE_VALUE_NOT_ALLOWED`: if the value or feePercentage are not positive
|
|
117
|
+
|
|
118
|
+
**Example**
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { calculateExchangeFee } from 'jg-bignumber';
|
|
122
|
+
|
|
123
|
+
calculateExchangeFee(1000, 0.1); // => 1
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### <a id="api-calculateMax"></a>`calculateMax`
|
|
127
|
+
|
|
128
|
+
*Function*
|
|
129
|
+
|
|
130
|
+
Finds the largest value in a list. An empty list returns 0.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
calculateMax<T extends Partial<IConfig>>(
|
|
134
|
+
values: Value[],
|
|
135
|
+
config?: T,
|
|
136
|
+
): IOutput<T>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Parameters**
|
|
140
|
+
|
|
141
|
+
- `values` `Value[]`: The values.
|
|
142
|
+
- `config?` `T`: How the result is processed.
|
|
143
|
+
|
|
144
|
+
**Returns** `IOutput<T>`: The largest value.
|
|
145
|
+
|
|
146
|
+
**Throws**
|
|
147
|
+
|
|
148
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
149
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
150
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
151
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
152
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
153
|
+
|
|
154
|
+
**Example**
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { calculateMax } from 'jg-bignumber';
|
|
158
|
+
|
|
159
|
+
calculateMax([3, '1.5', 2]); // => 3
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### <a id="api-calculateMean"></a>`calculateMean`
|
|
163
|
+
|
|
164
|
+
*Function*
|
|
165
|
+
|
|
166
|
+
Calculates the mean of a list of values. An empty list returns 0.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
calculateMean<T extends Partial<IConfig>>(
|
|
170
|
+
values: Value[],
|
|
171
|
+
config?: T,
|
|
172
|
+
): IOutput<T>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
**Parameters**
|
|
176
|
+
|
|
177
|
+
- `values` `Value[]`: The values.
|
|
178
|
+
- `config?` `T`: How the result is processed.
|
|
179
|
+
|
|
180
|
+
**Returns** `IOutput<T>`: The mean.
|
|
181
|
+
|
|
182
|
+
**Throws**
|
|
183
|
+
|
|
184
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
185
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
186
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
187
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
188
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
189
|
+
|
|
190
|
+
**Example**
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { calculateMean } from 'jg-bignumber';
|
|
194
|
+
|
|
195
|
+
calculateMean([1, 2, 4]); // => 2.33
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### <a id="api-calculateMedian"></a>`calculateMedian`
|
|
199
|
+
|
|
200
|
+
*Function*
|
|
201
|
+
|
|
202
|
+
Calculates the median of a list of values: the middle value, or the mean of the two middle values when the list has an even length. An empty list returns 0.
|
|
203
|
+
|
|
204
|
+
```ts
|
|
205
|
+
calculateMedian<T extends Partial<IConfig>>(
|
|
206
|
+
values: Value[],
|
|
207
|
+
config?: T,
|
|
208
|
+
): IOutput<T>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
**Parameters**
|
|
212
|
+
|
|
213
|
+
- `values` `Value[]`: The values.
|
|
214
|
+
- `config?` `T`: How the result is processed.
|
|
215
|
+
|
|
216
|
+
**Returns** `IOutput<T>`: The median.
|
|
217
|
+
|
|
218
|
+
**Throws**
|
|
219
|
+
|
|
220
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
221
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
222
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
223
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
224
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
225
|
+
|
|
226
|
+
**Example**
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import { calculateMedian } from 'jg-bignumber';
|
|
230
|
+
|
|
231
|
+
calculateMedian([5, 1, 3]); // => 3
|
|
232
|
+
calculateMedian([4, 1, 3, 2]); // => 2.5
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### <a id="api-calculateMin"></a>`calculateMin`
|
|
236
|
+
|
|
237
|
+
*Function*
|
|
238
|
+
|
|
239
|
+
Finds the smallest value in a list. An empty list returns 0.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
calculateMin<T extends Partial<IConfig>>(
|
|
243
|
+
values: Value[],
|
|
244
|
+
config?: T,
|
|
245
|
+
): IOutput<T>
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**Parameters**
|
|
249
|
+
|
|
250
|
+
- `values` `Value[]`: The values.
|
|
251
|
+
- `config?` `T`: How the result is processed.
|
|
252
|
+
|
|
253
|
+
**Returns** `IOutput<T>`: The smallest value.
|
|
254
|
+
|
|
255
|
+
**Throws**
|
|
256
|
+
|
|
257
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
258
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
259
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
260
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
261
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
262
|
+
|
|
263
|
+
**Example**
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
import { calculateMin } from 'jg-bignumber';
|
|
267
|
+
|
|
268
|
+
calculateMin([3, '1.5', 2]); // => 1.5
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### <a id="api-calculatePercentageChange"></a>`calculatePercentageChange`
|
|
272
|
+
|
|
273
|
+
*Function*
|
|
274
|
+
|
|
275
|
+
Calculates the percentage a value changed by: positive when it increased, negative when it decreased, and 0 when it didn't change. The largest decrease is -100%, returned whenever the new value is 0 or less.
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
calculatePercentageChange<T extends Partial<IConfig>>(
|
|
279
|
+
oldValue: Value,
|
|
280
|
+
newValue: Value,
|
|
281
|
+
config?: T,
|
|
282
|
+
): IOutput<T>
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
**Parameters**
|
|
286
|
+
|
|
287
|
+
- `oldValue` `Value`: The value before the change.
|
|
288
|
+
- `newValue` `Value`: The value after the change.
|
|
289
|
+
- `config?` `T`: How the result is processed.
|
|
290
|
+
|
|
291
|
+
**Returns** `IOutput<T>`: The percentage change.
|
|
292
|
+
|
|
293
|
+
**Throws**
|
|
294
|
+
|
|
295
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
296
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
297
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
298
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
299
|
+
- `NEGATIVE_VALUE_NOT_ALLOWED`: if the oldValue is not positive
|
|
300
|
+
|
|
301
|
+
**Example**
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { calculatePercentageChange } from 'jg-bignumber';
|
|
305
|
+
|
|
306
|
+
calculatePercentageChange(100, 125); // => 25
|
|
307
|
+
calculatePercentageChange(100, 80); // => -20
|
|
308
|
+
calculatePercentageChange(100, 0); // => -100
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### <a id="api-calculatePercentageRepresentation"></a>`calculatePercentageRepresentation`
|
|
312
|
+
|
|
313
|
+
*Function*
|
|
314
|
+
|
|
315
|
+
Calculates what percentage of a total a value represents.
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
calculatePercentageRepresentation<T extends Partial<IConfig>>(
|
|
319
|
+
value: Value,
|
|
320
|
+
total: Value,
|
|
321
|
+
config?: T,
|
|
322
|
+
): IOutput<T>
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
**Parameters**
|
|
326
|
+
|
|
327
|
+
- `value` `Value`: The part.
|
|
328
|
+
- `total` `Value`: The total.
|
|
329
|
+
- `config?` `T`: How the result is processed.
|
|
330
|
+
|
|
331
|
+
**Returns** `IOutput<T>`: The percentage.
|
|
332
|
+
|
|
333
|
+
**Throws**
|
|
334
|
+
|
|
335
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
336
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
337
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
338
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
339
|
+
- `NEGATIVE_VALUE_NOT_ALLOWED`: if the value or total are not positive
|
|
340
|
+
|
|
341
|
+
**Example**
|
|
342
|
+
|
|
343
|
+
```ts
|
|
344
|
+
import { calculatePercentageRepresentation } from 'jg-bignumber';
|
|
345
|
+
|
|
346
|
+
calculatePercentageRepresentation(25, 200); // => 12.5
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### <a id="api-calculateSum"></a>`calculateSum`
|
|
350
|
+
|
|
351
|
+
*Function*
|
|
352
|
+
|
|
353
|
+
Adds up a list of values. An empty list sums to 0.
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
calculateSum<T extends Partial<IConfig>>(
|
|
357
|
+
values: Value[],
|
|
358
|
+
config?: T,
|
|
359
|
+
): IOutput<T>
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
**Parameters**
|
|
363
|
+
|
|
364
|
+
- `values` `Value[]`: The values.
|
|
365
|
+
- `config?` `T`: How the result is processed.
|
|
366
|
+
|
|
367
|
+
**Returns** `IOutput<T>`: The sum.
|
|
368
|
+
|
|
369
|
+
**Throws**
|
|
370
|
+
|
|
371
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
372
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
373
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
374
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
375
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
376
|
+
|
|
377
|
+
**Example**
|
|
378
|
+
|
|
379
|
+
```ts
|
|
380
|
+
import { calculateSum } from 'jg-bignumber';
|
|
381
|
+
|
|
382
|
+
calculateSum([0.1, '0.2', 0.3]); // => 0.6
|
|
383
|
+
calculateSum([0.1, 0.2], { type: 'string' }); // => '0.3'
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### <a id="api-calculateWeightedEntry"></a>`calculateWeightedEntry`
|
|
387
|
+
|
|
388
|
+
*Function*
|
|
389
|
+
|
|
390
|
+
Calculates the average entry price of a position bought in several trades, weighting each price by the amount bought. An empty list, or one whose amounts add up to 0, returns 0.
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
calculateWeightedEntry<T extends Partial<IConfig>>(
|
|
394
|
+
trades: [Value, Value][],
|
|
395
|
+
config?: T,
|
|
396
|
+
): IOutput<T>
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**Parameters**
|
|
400
|
+
|
|
401
|
+
- `trades` `[Value, Value][]`: The trades, each as a `[price, amount]` tuple.
|
|
402
|
+
- `config?` `T`: How the result is processed.
|
|
403
|
+
|
|
404
|
+
**Returns** `IOutput<T>`: The weighted average price.
|
|
405
|
+
|
|
406
|
+
**Throws**
|
|
407
|
+
|
|
408
|
+
- `INVALID_VALUE`: if any of the given values isn't a finite number or BigNumber throws an error
|
|
409
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
410
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
411
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
412
|
+
- `INVALID_VALUES_ARRAY`: if the provided values arg is not a valid array
|
|
413
|
+
|
|
414
|
+
**Example**
|
|
415
|
+
|
|
416
|
+
```ts
|
|
417
|
+
import { calculateWeightedEntry } from 'jg-bignumber';
|
|
418
|
+
|
|
419
|
+
calculateWeightedEntry([[100, 2], [130, 1]]); // => 110
|
|
420
|
+
calculateWeightedEntry([]); // => 0
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### <a id="api-getBigNumber"></a>`getBigNumber`
|
|
424
|
+
|
|
425
|
+
*Function*
|
|
426
|
+
|
|
427
|
+
Creates a BigNumber from a number, a numeric string, or a BigNumber, which is returned as is. Only finite values are accepted: `NaN` and `±Infinity` are rejected.
|
|
428
|
+
|
|
429
|
+
```ts
|
|
430
|
+
getBigNumber(value: Value): BigNumber
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
**Parameters**
|
|
434
|
+
|
|
435
|
+
- `value` `Value`: The value.
|
|
436
|
+
|
|
437
|
+
**Returns** `BigNumber`: The BigNumber.
|
|
438
|
+
|
|
439
|
+
**Throws**
|
|
440
|
+
|
|
441
|
+
- `INVALID_VALUE`: if the value isn't a finite number or BigNumber throws an error
|
|
442
|
+
|
|
443
|
+
**Example**
|
|
444
|
+
|
|
445
|
+
```ts
|
|
446
|
+
import { getBigNumber } from 'jg-bignumber';
|
|
447
|
+
|
|
448
|
+
getBigNumber('0.1').plus('0.2').toString(); // => '0.3'
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
### <a id="api-isBigNumber"></a>`isBigNumber`
|
|
452
|
+
|
|
453
|
+
*Function*
|
|
454
|
+
|
|
455
|
+
Checks whether a value is a BigNumber instance, from this package or any other copy of bignumber.js.
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
isBigNumber(value: unknown): value is BigNumber
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
**Parameters**
|
|
462
|
+
|
|
463
|
+
- `value` `unknown`: The value.
|
|
464
|
+
|
|
465
|
+
**Returns** `value is BigNumber`: Whether it's a BigNumber.
|
|
466
|
+
|
|
467
|
+
**Example**
|
|
468
|
+
|
|
469
|
+
```ts
|
|
470
|
+
import { getBigNumber, isBigNumber } from 'jg-bignumber';
|
|
471
|
+
|
|
472
|
+
isBigNumber(getBigNumber(1)); // => true
|
|
473
|
+
isBigNumber(1); // => false
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
### <a id="api-isFloat"></a>`isFloat`
|
|
477
|
+
|
|
478
|
+
*Function*
|
|
479
|
+
|
|
480
|
+
Checks whether a value is a finite number with a fractional part, in any of the supported types.
|
|
481
|
+
|
|
482
|
+
```ts
|
|
483
|
+
isFloat(value: unknown): boolean
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
**Parameters**
|
|
487
|
+
|
|
488
|
+
- `value` `unknown`: The value.
|
|
489
|
+
|
|
490
|
+
**Returns** `boolean`: Whether it's a float.
|
|
491
|
+
|
|
492
|
+
**Example**
|
|
493
|
+
|
|
494
|
+
```ts
|
|
495
|
+
import { isFloat } from 'jg-bignumber';
|
|
496
|
+
|
|
497
|
+
isFloat(12.5); // => true
|
|
498
|
+
isFloat('12'); // => false
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
### <a id="api-isInteger"></a>`isInteger`
|
|
502
|
+
|
|
503
|
+
*Function*
|
|
504
|
+
|
|
505
|
+
Checks whether a value is a finite integer, in any of the supported types.
|
|
506
|
+
|
|
507
|
+
```ts
|
|
508
|
+
isInteger(value: unknown): boolean
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
**Parameters**
|
|
512
|
+
|
|
513
|
+
- `value` `unknown`: The value.
|
|
514
|
+
|
|
515
|
+
**Returns** `boolean`: Whether it's an integer.
|
|
516
|
+
|
|
517
|
+
**Example**
|
|
518
|
+
|
|
519
|
+
```ts
|
|
520
|
+
import { isInteger } from 'jg-bignumber';
|
|
521
|
+
|
|
522
|
+
isInteger('12'); // => true
|
|
523
|
+
isInteger(12.5); // => false
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### <a id="api-isNumber"></a>`isNumber`
|
|
527
|
+
|
|
528
|
+
*Function*
|
|
529
|
+
|
|
530
|
+
Checks whether a value is a finite number, numeric string, or BigNumber.
|
|
531
|
+
|
|
532
|
+
```ts
|
|
533
|
+
isNumber(value: unknown): boolean
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
**Parameters**
|
|
537
|
+
|
|
538
|
+
- `value` `unknown`: The value.
|
|
539
|
+
|
|
540
|
+
**Returns** `boolean`: Whether it's a finite number.
|
|
541
|
+
|
|
542
|
+
**Example**
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
import { isNumber } from 'jg-bignumber';
|
|
546
|
+
|
|
547
|
+
isNumber('12.5'); // => true
|
|
548
|
+
isNumber(Infinity); // => false
|
|
549
|
+
isNumber('abc'); // => false
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
### <a id="api-prettifyValue"></a>`prettifyValue`
|
|
553
|
+
|
|
554
|
+
*Function*
|
|
555
|
+
|
|
556
|
+
Processes a value and formats it for people, with group separators, a prefix, and a suffix.
|
|
557
|
+
|
|
558
|
+
```ts
|
|
559
|
+
prettifyValue(
|
|
560
|
+
value: Value,
|
|
561
|
+
config?: { processing?: Partial<IConfig>; format?: Partial<Required<Format>> },
|
|
562
|
+
): string
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
**Parameters**
|
|
566
|
+
|
|
567
|
+
- `value` `Value`: The value.
|
|
568
|
+
- `config?` `{ processing?: Partial<IConfig>; format?: Partial<Required<Format>> }`: How the value is processed and formatted. Defaults to `{}`.
|
|
569
|
+
|
|
570
|
+
**Returns** `string`: The formatted value.
|
|
571
|
+
|
|
572
|
+
**Throws**
|
|
573
|
+
|
|
574
|
+
- `INVALID_VALUE`: if the value isn't a finite number or BigNumber throws an error
|
|
575
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
576
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
577
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
578
|
+
- `INVALID_BIGNUMBER_FORMAT`: if any of the format properties are invalid
|
|
579
|
+
|
|
580
|
+
**Example**
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
import { prettifyValue } from 'jg-bignumber';
|
|
584
|
+
|
|
585
|
+
prettifyValue(1234567.891); // => '1,234,567.89'
|
|
586
|
+
prettifyValue(1234.5, { processing: { decimalPlaces: 2 }, format: { prefix: '$' } }); // => '$1,234.5'
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
### <a id="api-processValue"></a>`processValue`
|
|
590
|
+
|
|
591
|
+
*Function*
|
|
592
|
+
|
|
593
|
+
Rounds a value and returns it as the type the configuration asks for: a number by default, or a string or a BigNumber.
|
|
594
|
+
|
|
595
|
+
```ts
|
|
596
|
+
processValue<T extends Partial<IConfig>>(
|
|
597
|
+
value: Value,
|
|
598
|
+
configuration?: T,
|
|
599
|
+
): IOutput<T>
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
**Parameters**
|
|
603
|
+
|
|
604
|
+
- `value` `Value`: The value.
|
|
605
|
+
- `configuration?` `T`: The decimal places, rounding mode, and output type.
|
|
606
|
+
|
|
607
|
+
**Returns** `IOutput<T>`: The processed value.
|
|
608
|
+
|
|
609
|
+
**Throws**
|
|
610
|
+
|
|
611
|
+
- `INVALID_VALUE`: if the value isn't a finite number or BigNumber throws an error
|
|
612
|
+
- `INVALID_DECIMAL_PLACES`: if the number of decimal places is invalid for any reason
|
|
613
|
+
- `INVALID_ROUNDING_MODE`: if the rounding mode name is not supported
|
|
614
|
+
- `INVALID_TYPE`: if the processing type is not supported
|
|
615
|
+
|
|
616
|
+
**Example**
|
|
617
|
+
|
|
618
|
+
```ts
|
|
619
|
+
import { processValue } from 'jg-bignumber';
|
|
620
|
+
|
|
621
|
+
processValue(512.155); // => 512.16
|
|
622
|
+
processValue(512.155, { decimalPlaces: 1, roundingMode: 'ROUND_FLOOR' }); // => 512.1
|
|
623
|
+
processValue('0.000001', { decimalPlaces: 6, type: 'string' }); // => '0.000001'
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
## <a id="api-constants"></a>Constants
|
|
627
|
+
|
|
628
|
+
### <a id="api-BigNumber"></a>`BigNumber`
|
|
629
|
+
|
|
630
|
+
*Constant*
|
|
631
|
+
|
|
632
|
+
The BigNumber constructor every function uses: a clone of bignumber.js's, so the package never changes the global bignumber.js configuration other code relies on. It never switches to exponential notation, and with STRICT off an invalid value becomes NaN instead of throwing, which the package then reports as INVALID_VALUE.
|
|
633
|
+
|
|
634
|
+
```ts
|
|
635
|
+
const BigNumber: typeof BigNumber
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
### <a id="api-ERRORS"></a>`ERRORS`
|
|
639
|
+
|
|
640
|
+
*Constant*
|
|
641
|
+
|
|
642
|
+
the codes of every error the package throws
|
|
643
|
+
|
|
644
|
+
```ts
|
|
645
|
+
const ERRORS: { INVALID_VALUE: "INVALID_VALUE"; INVALID_TYPE: "INVALID_TYPE"; INVALID_ROUNDING_MODE: "INVALID_ROUNDING_MODE"; INVALID_DECIMAL_PLACES: "INVALID_DECIMAL_PLACES"; INVALID_BIGNUMBER_FORMAT: "INVALID_BIGNUMBER_FORMAT"; INVALID_VALUES_ARRAY: "INVALID_VALUES_ARRAY"; NEGATIVE_VALUE_NOT_ALLOWED: "NEGATIVE_VALUE_NOT_ALLOWED" }
|
|
646
|
+
```
|
|
647
|
+
|
|
648
|
+
## <a id="api-types"></a>Types
|
|
649
|
+
|
|
650
|
+
### <a id="api-IBigNumber"></a>`IBigNumber`
|
|
651
|
+
|
|
652
|
+
*Type*
|
|
653
|
+
|
|
654
|
+
A BigNumber instance, created with `getBigNumber(value)` or the exported `BigNumber` constructor. Every bignumber.js method is available on it.
|
|
655
|
+
|
|
656
|
+
```ts
|
|
657
|
+
type IBigNumber = BigNumber;
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
### <a id="api-IBigNumberFormat"></a>`IBigNumberFormat`
|
|
661
|
+
|
|
662
|
+
*Type*
|
|
663
|
+
|
|
664
|
+
How prettifyValue formats a value, with every setting of bignumber.js's toFormat, defaults in parentheses: prefix (''), negativeSign ('-'), positiveSign (''), decimalSeparator ('.'), groupSeparator (','), groupSize (3), secondaryGroupSize (0), fractionGroupSeparator (' '), fractionGroupSize (0), and suffix ('').
|
|
665
|
+
|
|
666
|
+
```ts
|
|
667
|
+
type IBigNumberFormat = Required<BigNumber.Format>;
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
### <a id="api-IBigNumberRoundingMode"></a>`IBigNumberRoundingMode`
|
|
671
|
+
|
|
672
|
+
*Type*
|
|
673
|
+
|
|
674
|
+
the numeric rounding mode bignumber.js uses for each IBigNumberRoundingModeName
|
|
675
|
+
|
|
676
|
+
```ts
|
|
677
|
+
type IBigNumberRoundingMode = BigNumber.RoundingMode;
|
|
678
|
+
```
|
|
679
|
+
|
|
680
|
+
### <a id="api-IBigNumberRoundingModeName"></a>`IBigNumberRoundingModeName`
|
|
681
|
+
|
|
682
|
+
*Type*
|
|
683
|
+
|
|
684
|
+
How a value is rounded when it has more decimals than allowed: - ROUND_UP: away from zero - ROUND_DOWN: towards zero - ROUND_CEIL: towards Infinity - ROUND_FLOOR: towards -Infinity - ROUND_HALF_UP (default): to the nearest neighbour, away from zero when equidistant - ROUND_HALF_DOWN: to the nearest neighbour, towards zero when equidistant - ROUND_HALF_EVEN: to the nearest neighbour, towards the even one when equidistant - ROUND_HALF_CEIL: to the nearest neighbour, towards Infinity when equidistant - ROUND_HALF_FLOOR: to the nearest neighbour, towards -Infinity when equidistant
|
|
685
|
+
|
|
686
|
+
```ts
|
|
687
|
+
type IBigNumberRoundingModeName = 'ROUND_UP' | 'ROUND_DOWN' | 'ROUND_CEIL' | 'ROUND_FLOOR' | 'ROUND_HALF_UP' | 'ROUND_HALF_DOWN' | 'ROUND_HALF_EVEN' | 'ROUND_HALF_CEIL' | 'ROUND_HALF_FLOOR';
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
### <a id="api-IBigNumberToType"></a>`IBigNumberToType`
|
|
691
|
+
|
|
692
|
+
*Type*
|
|
693
|
+
|
|
694
|
+
the TypeScript type of a value converted to an IType
|
|
695
|
+
|
|
696
|
+
```ts
|
|
697
|
+
type IBigNumberToType<T> = T extends 'string' ? string : T extends 'number' ? number : T extends 'bignumber' ? IBigNumber : never;
|
|
698
|
+
```
|
|
699
|
+
|
|
700
|
+
### <a id="api-IBigNumberValue"></a>`IBigNumberValue`
|
|
701
|
+
|
|
702
|
+
*Type*
|
|
703
|
+
|
|
704
|
+
the values a BigNumber can be created from: numbers, numeric strings, and BigNumbers
|
|
705
|
+
|
|
706
|
+
```ts
|
|
707
|
+
type IBigNumberValue = BigNumber.Value;
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
### <a id="api-IConfig"></a>`IConfig`
|
|
711
|
+
|
|
712
|
+
*Type*
|
|
713
|
+
|
|
714
|
+
how a value is processed
|
|
715
|
+
|
|
716
|
+
```ts
|
|
717
|
+
type IConfig = {
|
|
718
|
+
decimalPlaces: number;
|
|
719
|
+
roundingMode: IBigNumberRoundingModeName;
|
|
720
|
+
type: IType;
|
|
721
|
+
};
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
**Members**
|
|
725
|
+
|
|
726
|
+
- `decimalPlaces: number` (property): the maximum number of decimals in the output. Defaults to 2
|
|
727
|
+
- `roundingMode: IBigNumberRoundingModeName` (property): how the value is rounded when it has more decimals. Defaults to 'ROUND_HALF_UP'
|
|
728
|
+
- `type: IType` (property): the output's type. Defaults to 'number'
|
|
729
|
+
|
|
730
|
+
### <a id="api-IOutput"></a>`IOutput`
|
|
731
|
+
|
|
732
|
+
*Type*
|
|
733
|
+
|
|
734
|
+
the return type of a function that processes a value, picked from its configuration's type
|
|
735
|
+
|
|
736
|
+
```ts
|
|
737
|
+
type IOutput<T> = T extends {
|
|
738
|
+
type: 'string';
|
|
739
|
+
} ? string : T extends {
|
|
740
|
+
type: 'number';
|
|
741
|
+
} ? number : T extends {
|
|
742
|
+
type: 'bignumber';
|
|
743
|
+
} ? IBigNumber : number;
|
|
744
|
+
```
|
|
745
|
+
|
|
746
|
+
### <a id="api-IType"></a>`IType`
|
|
747
|
+
|
|
748
|
+
*Type*
|
|
749
|
+
|
|
750
|
+
the type a processed value is returned as
|
|
751
|
+
|
|
752
|
+
```ts
|
|
753
|
+
type IType = 'string' | 'number' | 'bignumber';
|
|
754
|
+
```
|