@agrozyme/numeric 1.0.20 → 1.0.21
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/lib/config.d.ts +3 -0
- package/lib/config.js +33 -0
- package/lib/config.js.map +1 -0
- package/lib/config.mjs +28 -0
- package/lib/config.mjs.map +1 -0
- package/lib/constants.d.ts +9 -0
- package/lib/constants.js +15 -0
- package/lib/constants.js.map +1 -0
- package/lib/constants.mjs +11 -0
- package/lib/constants.mjs.map +1 -0
- package/lib/convert.d.ts +11 -0
- package/lib/convert.js +23 -0
- package/lib/convert.js.map +1 -0
- package/lib/convert.mjs +12 -0
- package/lib/convert.mjs.map +1 -0
- package/lib/format.d.ts +18 -0
- package/lib/format.js +13 -0
- package/lib/format.js.map +1 -0
- package/lib/format.mjs +8 -0
- package/lib/format.mjs.map +1 -0
- package/lib/helper.d.ts +2 -0
- package/lib/helper.js +9 -0
- package/lib/helper.js.map +1 -0
- package/lib/helper.mjs +5 -0
- package/lib/helper.mjs.map +1 -0
- package/lib/index.d.ts +8 -158
- package/lib/index.js +27 -230
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +6 -217
- package/lib/index.mjs.map +1 -1
- package/lib/numeric.d.ts +126 -0
- package/lib/numeric.js +397 -0
- package/lib/numeric.js.map +1 -0
- package/lib/numeric.mjs +392 -0
- package/lib/numeric.mjs.map +1 -0
- package/package.json +1 -1
- package/src/config.ts +32 -0
- package/src/constants.ts +11 -0
- package/src/convert.ts +12 -0
- package/src/format.ts +25 -0
- package/src/helper.ts +6 -0
- package/src/index.ts +8 -378
- package/src/numeric.ts +517 -0
package/src/index.ts
CHANGED
|
@@ -1,383 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
export { Numeric } from './numeric';
|
|
2
|
+
export type { NumericValue } from './numeric';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
import { hexlify, isBytesLike } from '@ethersproject/bytes';
|
|
5
|
-
import * as constants from '@ethersproject/constants';
|
|
6
|
-
import bigInt from 'big-integer';
|
|
7
|
-
import BN from 'bn.js';
|
|
8
|
-
import { isNode } from 'browser-or-node';
|
|
9
|
-
import { Decimal } from 'decimal.js';
|
|
10
|
-
import formatter from 'format-number';
|
|
11
|
-
import { InspectOptionsStylized } from 'node:util';
|
|
4
|
+
export { getNumericConfig, setupNumericConfig } from './config';
|
|
12
5
|
|
|
13
|
-
|
|
6
|
+
export { MaxInt256, MaxUint256, MinInt256, NegativeOne, One, Two, WeiPerEther, Zero } from './constants';
|
|
14
7
|
|
|
15
|
-
export
|
|
8
|
+
export { toBigInt, toBigInteger, toBigNumber, toBN, toIntegerString, toNumeric, toNumerics } from './convert';
|
|
16
9
|
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
negativeLeftSymbol?: string;
|
|
20
|
-
negativeRightSymbol?: string;
|
|
21
|
-
negativeLeftOut?: boolean;
|
|
22
|
-
negativeRightOut?: boolean;
|
|
23
|
-
prefix?: string;
|
|
24
|
-
suffix?: string;
|
|
25
|
-
integerSeparator?: string;
|
|
26
|
-
decimalsSeparator?: string;
|
|
27
|
-
decimal?: string;
|
|
28
|
-
padLeft?: number;
|
|
29
|
-
padRight?: number;
|
|
30
|
-
round?: number;
|
|
31
|
-
truncate?: number;
|
|
32
|
-
}
|
|
10
|
+
export { formatValue } from './format';
|
|
11
|
+
export type { IFormatNumberOptions } from './format';
|
|
33
12
|
|
|
34
|
-
|
|
35
|
-
export class Numeric extends Decimal {
|
|
36
|
-
// private readonly toStringTag: string = 'Decimal';
|
|
37
|
-
|
|
38
|
-
constructor(value: NumericValue) {
|
|
39
|
-
if (value instanceof Decimal) {
|
|
40
|
-
super(value);
|
|
41
|
-
} else {
|
|
42
|
-
switch (typeof value) {
|
|
43
|
-
case 'string':
|
|
44
|
-
case 'number':
|
|
45
|
-
super(value);
|
|
46
|
-
break;
|
|
47
|
-
case 'bigint':
|
|
48
|
-
super(value.toString());
|
|
49
|
-
break;
|
|
50
|
-
default:
|
|
51
|
-
if (bigInt.isInstance(value) || BigNumber.isBigNumber(value) || BN.isBN(value)) {
|
|
52
|
-
super(value.toString());
|
|
53
|
-
} else if (isBytesLike(value)) {
|
|
54
|
-
super(hexlify(value));
|
|
55
|
-
} else {
|
|
56
|
-
super(value);
|
|
57
|
-
}
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get [Symbol.toStringTag]() {
|
|
64
|
-
return 'Numeric';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
static abs = (value: NumericValue) => new Numeric(value).abs();
|
|
68
|
-
|
|
69
|
-
static acos = (value: NumericValue) => new Numeric(value).acos();
|
|
70
|
-
|
|
71
|
-
static acosh = (value: NumericValue) => new Numeric(value).acosh();
|
|
72
|
-
|
|
73
|
-
static add = (x: NumericValue, y: NumericValue) => new Numeric(x).add(y);
|
|
74
|
-
|
|
75
|
-
static asin = (value: NumericValue) => new Numeric(value).asin();
|
|
76
|
-
|
|
77
|
-
static asinh = (value: NumericValue) => new Numeric(value).asinh();
|
|
78
|
-
|
|
79
|
-
static atan = (value: NumericValue) => new Numeric(value).atan();
|
|
80
|
-
|
|
81
|
-
static atan2 = (x: NumericValue, y: NumericValue) => new Numeric(Decimal.atan2(new Numeric(x), new Numeric(y)));
|
|
82
|
-
|
|
83
|
-
static atanh = (value: NumericValue) => new Numeric(value).atanh();
|
|
84
|
-
|
|
85
|
-
static cbrt = (value: NumericValue) => new Numeric(value).cbrt();
|
|
86
|
-
|
|
87
|
-
static ceil = (value: NumericValue) => new Numeric(value).ceil();
|
|
88
|
-
|
|
89
|
-
static cos = (value: NumericValue) => new Numeric(value).cos();
|
|
90
|
-
|
|
91
|
-
static cosh = (value: NumericValue) => new Numeric(value).cosh();
|
|
92
|
-
|
|
93
|
-
static div = (x: NumericValue, y: NumericValue) => new Numeric(x).div(y);
|
|
94
|
-
|
|
95
|
-
static exp = (value: NumericValue) => new Numeric(value).exp();
|
|
96
|
-
|
|
97
|
-
static floor = (value: NumericValue) => new Numeric(value).floor();
|
|
98
|
-
|
|
99
|
-
static hypot = (...values: NumericValue[]) => new Numeric(Decimal.hypot(...toNumerics(values)));
|
|
100
|
-
|
|
101
|
-
static ln = (value: NumericValue) => new Numeric(value).ln();
|
|
102
|
-
|
|
103
|
-
static log = (value: NumericValue, base?: NumericValue) => new Numeric(value).log(base);
|
|
104
|
-
|
|
105
|
-
static log10 = (value: NumericValue) => new Numeric(value).log(10);
|
|
106
|
-
|
|
107
|
-
static log2 = (value: NumericValue) => new Numeric(value).log(2);
|
|
108
|
-
|
|
109
|
-
static max = (...values: NumericValue[]) => new Numeric(Decimal.max(...toNumerics(values)));
|
|
110
|
-
|
|
111
|
-
static min = (...values: NumericValue[]) => new Numeric(Decimal.min(...toNumerics(values)));
|
|
112
|
-
|
|
113
|
-
static mod = (x: NumericValue, y: NumericValue) => new Numeric(x).mod(y);
|
|
114
|
-
|
|
115
|
-
static mul = (x: NumericValue, y: NumericValue) => new Numeric(x).mul(y);
|
|
116
|
-
|
|
117
|
-
static pow = (base: NumericValue, exponent: NumericValue) => new Numeric(base).pow(exponent);
|
|
118
|
-
|
|
119
|
-
static random = (significantDigits?: number) => new Numeric(Decimal.random(significantDigits));
|
|
120
|
-
|
|
121
|
-
static round = (value: NumericValue) => new Numeric(value).round();
|
|
122
|
-
|
|
123
|
-
static set = (object: Decimal.Config) => Decimal.set(object);
|
|
124
|
-
|
|
125
|
-
static sign = (value: NumericValue) => Decimal.sign(new Numeric(value));
|
|
126
|
-
|
|
127
|
-
static sin = (value: NumericValue) => new Numeric(value).sin();
|
|
128
|
-
|
|
129
|
-
static sinh = (value: NumericValue) => new Numeric(value).sinh();
|
|
130
|
-
|
|
131
|
-
static sqrt = (value: NumericValue) => new Numeric(value).sqrt();
|
|
132
|
-
|
|
133
|
-
static sub = (x: NumericValue, y: NumericValue) => new Numeric(x).sub(y);
|
|
134
|
-
|
|
135
|
-
static tan = (value: NumericValue) => new Numeric(value).tan();
|
|
136
|
-
|
|
137
|
-
static tanh = (value: NumericValue) => new Numeric(value).tanh();
|
|
138
|
-
|
|
139
|
-
static trunc = (value: NumericValue) => new Numeric(value).trunc();
|
|
140
|
-
|
|
141
|
-
abs = () => new Numeric(super.abs());
|
|
142
|
-
|
|
143
|
-
absoluteValue = () => new Numeric(super.absoluteValue());
|
|
144
|
-
|
|
145
|
-
acos = () => new Numeric(super.acos());
|
|
146
|
-
|
|
147
|
-
acosh = () => new Numeric(super.acosh());
|
|
148
|
-
|
|
149
|
-
add = (value: NumericValue) => new Numeric(super.add(new Numeric(value)));
|
|
150
|
-
|
|
151
|
-
asin = () => new Numeric(super.asin());
|
|
152
|
-
|
|
153
|
-
asinh = () => new Numeric(super.asinh());
|
|
154
|
-
|
|
155
|
-
atan = () => new Numeric(super.atan());
|
|
156
|
-
|
|
157
|
-
atanh = () => new Numeric(super.atanh());
|
|
158
|
-
|
|
159
|
-
cbrt = () => new Numeric(super.cbrt());
|
|
160
|
-
|
|
161
|
-
ceil = () => new Numeric(super.ceil());
|
|
162
|
-
|
|
163
|
-
cmp = (value: NumericValue) => super.cmp(new Numeric(value));
|
|
164
|
-
|
|
165
|
-
comparedTo = (value: NumericValue) => super.comparedTo(new Numeric(value));
|
|
166
|
-
|
|
167
|
-
cos = () => new Numeric(super.cos());
|
|
168
|
-
|
|
169
|
-
cosh = () => new Numeric(super.cosh());
|
|
170
|
-
|
|
171
|
-
cosine = () => new Numeric(super.cosine());
|
|
172
|
-
|
|
173
|
-
cubeRoot = () => new Numeric(super.cubeRoot());
|
|
174
|
-
|
|
175
|
-
div = (value: NumericValue) => new Numeric(super.div(new Numeric(value)));
|
|
176
|
-
|
|
177
|
-
divToInt = (value: NumericValue) => new Numeric(super.divToInt(new Numeric(value)));
|
|
178
|
-
|
|
179
|
-
dividedBy = (value: NumericValue) => new Numeric(super.dividedBy(new Numeric(value)));
|
|
180
|
-
|
|
181
|
-
dividedToIntegerBy = (value: NumericValue) => new Numeric(super.dividedToIntegerBy(new Numeric(value)));
|
|
182
|
-
|
|
183
|
-
eq = (value: NumericValue) => super.eq(new Numeric(value));
|
|
184
|
-
|
|
185
|
-
equals = (value: NumericValue) => super.equals(new Numeric(value));
|
|
186
|
-
|
|
187
|
-
exp = () => new Numeric(super.exp());
|
|
188
|
-
|
|
189
|
-
floor = () => new Numeric(super.floor());
|
|
190
|
-
|
|
191
|
-
greaterThan = (value: NumericValue) => super.greaterThan(new Numeric(value));
|
|
192
|
-
|
|
193
|
-
greaterThanOrEqualTo = (value: NumericValue) => super.greaterThanOrEqualTo(new Numeric(value));
|
|
194
|
-
|
|
195
|
-
gt = (value: NumericValue) => super.gt(new Numeric(value));
|
|
196
|
-
|
|
197
|
-
gte = (value: NumericValue) => super.gte(new Numeric(value));
|
|
198
|
-
|
|
199
|
-
hyperbolicCosine = () => new Numeric(super.hyperbolicCosine());
|
|
200
|
-
|
|
201
|
-
hyperbolicSine = () => new Numeric(super.hyperbolicSine());
|
|
202
|
-
|
|
203
|
-
hyperbolicTangent = () => new Numeric(super.hyperbolicTangent());
|
|
204
|
-
|
|
205
|
-
inverseCosine = () => new Numeric(super.inverseCosine());
|
|
206
|
-
|
|
207
|
-
inverseHyperbolicCosine = () => new Numeric(super.inverseHyperbolicCosine());
|
|
208
|
-
|
|
209
|
-
inverseHyperbolicSine = () => new Numeric(super.inverseHyperbolicSine());
|
|
210
|
-
|
|
211
|
-
inverseHyperbolicTangent = () => new Numeric(super.inverseHyperbolicTangent());
|
|
212
|
-
|
|
213
|
-
inverseSine = () => new Numeric(super.inverseSine());
|
|
214
|
-
|
|
215
|
-
inverseTangent = () => new Numeric(super.inverseTangent());
|
|
216
|
-
|
|
217
|
-
lessThan = (value: NumericValue) => super.lessThan(new Numeric(value));
|
|
218
|
-
|
|
219
|
-
lessThanOrEqualTo = (value: NumericValue) => super.lessThanOrEqualTo(new Numeric(value));
|
|
220
|
-
|
|
221
|
-
ln = () => new Numeric(super.ln());
|
|
222
|
-
|
|
223
|
-
log = (value?: NumericValue) => new Numeric(super.log(undefined === value ? undefined : new Numeric(value)));
|
|
224
|
-
|
|
225
|
-
logarithm = (value?: NumericValue) =>
|
|
226
|
-
new Numeric(super.logarithm(undefined === value ? undefined : new Numeric(value)));
|
|
227
|
-
|
|
228
|
-
lt = (value: NumericValue) => super.lt(new Numeric(value));
|
|
229
|
-
|
|
230
|
-
lte = (value: NumericValue) => super.lte(new Numeric(value));
|
|
231
|
-
|
|
232
|
-
minus = (value: NumericValue) => new Numeric(super.minus(new Numeric(value)));
|
|
233
|
-
|
|
234
|
-
mod = (value: NumericValue) => new Numeric(super.mod(new Numeric(value)));
|
|
235
|
-
|
|
236
|
-
modulo = (value: NumericValue) => new Numeric(super.modulo(new Numeric(value)));
|
|
237
|
-
|
|
238
|
-
mul = (value: NumericValue) => new Numeric(super.mul(new Numeric(value)));
|
|
239
|
-
|
|
240
|
-
naturalExponential = () => new Numeric(super.naturalExponential());
|
|
241
|
-
|
|
242
|
-
naturalLogarithm = () => new Numeric(super.naturalLogarithm());
|
|
243
|
-
|
|
244
|
-
neg = () => new Numeric(super.neg());
|
|
245
|
-
|
|
246
|
-
negated = () => new Numeric(super.negated());
|
|
247
|
-
|
|
248
|
-
plus = (value: NumericValue) => new Numeric(super.plus(new Numeric(value)));
|
|
249
|
-
|
|
250
|
-
pow = (value: NumericValue) => new Numeric(super.pow(new Numeric(value)));
|
|
251
|
-
|
|
252
|
-
round = () => new Numeric(super.round());
|
|
253
|
-
|
|
254
|
-
sin = () => new Numeric(super.sin());
|
|
255
|
-
|
|
256
|
-
sine = () => new Numeric(super.sine());
|
|
257
|
-
|
|
258
|
-
sinh = () => new Numeric(super.sinh());
|
|
259
|
-
|
|
260
|
-
sqrt = () => new Numeric(super.sqrt());
|
|
261
|
-
|
|
262
|
-
squareRoot = () => new Numeric(super.squareRoot());
|
|
263
|
-
|
|
264
|
-
sub = (value: NumericValue) => new Numeric(super.sub(new Numeric(value)));
|
|
265
|
-
|
|
266
|
-
tan = () => new Numeric(super.tan());
|
|
267
|
-
|
|
268
|
-
tangent = () => new Numeric(super.tangent());
|
|
269
|
-
|
|
270
|
-
tanh = () => new Numeric(super.tanh());
|
|
271
|
-
|
|
272
|
-
times = (value: NumericValue) => new Numeric(super.times(new Numeric(value)));
|
|
273
|
-
|
|
274
|
-
toDP = (decimalPlaces?: number, rounding?: Decimal.Rounding) =>
|
|
275
|
-
new Numeric(
|
|
276
|
-
undefined !== rounding && undefined !== decimalPlaces
|
|
277
|
-
? super.toDP(decimalPlaces, rounding)
|
|
278
|
-
: super.toDP(decimalPlaces)
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
toDecimalPlaces = (decimalPlaces?: number, rounding?: Decimal.Rounding) =>
|
|
282
|
-
new Numeric(
|
|
283
|
-
undefined !== rounding && undefined !== decimalPlaces
|
|
284
|
-
? super.toDecimalPlaces(decimalPlaces, rounding)
|
|
285
|
-
: super.toDecimalPlaces(decimalPlaces)
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
toFraction = (maxDenominator?: NumericValue) =>
|
|
289
|
-
toNumerics(super.toFraction(undefined === maxDenominator ? undefined : new Numeric(maxDenominator)));
|
|
290
|
-
|
|
291
|
-
toNearest = (value: NumericValue, rounding?: Decimal.Rounding) =>
|
|
292
|
-
new Numeric(super.toNearest(new Numeric(value), rounding));
|
|
293
|
-
|
|
294
|
-
toPower = (value: NumericValue) => new Numeric(super.toPower(new Numeric(value)));
|
|
295
|
-
|
|
296
|
-
toSD = (decimalPlaces?: number, rounding?: Decimal.Rounding) =>
|
|
297
|
-
new Numeric(
|
|
298
|
-
undefined !== rounding && undefined !== decimalPlaces
|
|
299
|
-
? super.toSD(decimalPlaces, rounding)
|
|
300
|
-
: super.toSD(decimalPlaces)
|
|
301
|
-
);
|
|
302
|
-
|
|
303
|
-
toSignificantDigits = (decimalPlaces?: number, rounding?: Decimal.Rounding) =>
|
|
304
|
-
new Numeric(
|
|
305
|
-
undefined !== rounding && undefined !== decimalPlaces
|
|
306
|
-
? super.toSignificantDigits(decimalPlaces, rounding)
|
|
307
|
-
: super.toSignificantDigits(decimalPlaces)
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
trunc = () => new Numeric(super.trunc());
|
|
311
|
-
|
|
312
|
-
truncated = () => new Numeric(super.truncated());
|
|
313
|
-
|
|
314
|
-
inspect = (depth: number, options: InspectOptionsStylized) => {
|
|
315
|
-
const value = this.toFixed();
|
|
316
|
-
return isNode ? options.stylize(value, 'bigint') : value;
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
export const toNumeric = (value: NumericValue) => new Numeric(value);
|
|
321
|
-
|
|
322
|
-
export const toNumerics = (values: NumericValue[]) => values.map(toNumeric);
|
|
323
|
-
|
|
324
|
-
export const toIntegerString = (value: NumericValue) => toNumeric(value).toFixed(0);
|
|
325
|
-
|
|
326
|
-
export const toBigInt = (value: NumericValue) => BigInt(toIntegerString(value));
|
|
327
|
-
|
|
328
|
-
export const toBN = (value: NumericValue) => new BN(toIntegerString(value));
|
|
329
|
-
|
|
330
|
-
export const toBigInteger = (value: NumericValue) => bigInt(toIntegerString(value));
|
|
331
|
-
|
|
332
|
-
export const toBigNumber = (value: NumericValue) => BigNumber.from(toIntegerString(value));
|
|
333
|
-
|
|
334
|
-
export const createArrayCompareFunction = (ascending: boolean = true) =>
|
|
335
|
-
ascending
|
|
336
|
-
? (left: NumericValue, right: NumericValue) => Numeric.sub(left, right).toNumber()
|
|
337
|
-
: (left: NumericValue, right: NumericValue) => Numeric.sub(right, left).toNumber();
|
|
338
|
-
|
|
339
|
-
export const formatValue = (value: NumericValue, options?: IFormatNumberOptions) => {
|
|
340
|
-
const format = formatter(options);
|
|
341
|
-
const text = new Numeric(value).toFixed();
|
|
342
|
-
return format(<any>text);
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
export const getNumericConfig = () => {
|
|
346
|
-
const { precision, rounding, minE, maxE, toExpNeg, toExpPos, crypto, modulo } = Numeric;
|
|
347
|
-
const data: Required<Decimal.Config> = {
|
|
348
|
-
precision,
|
|
349
|
-
rounding,
|
|
350
|
-
minE,
|
|
351
|
-
maxE,
|
|
352
|
-
toExpNeg,
|
|
353
|
-
toExpPos,
|
|
354
|
-
crypto,
|
|
355
|
-
modulo,
|
|
356
|
-
defaults: false,
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
return data;
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
export const setupNumericConfig = () => {
|
|
363
|
-
const config: Decimal.Config = {
|
|
364
|
-
precision: 256,
|
|
365
|
-
//rounding: Numeric.ROUND_FLOOR,
|
|
366
|
-
toExpNeg: Numeric.minE,
|
|
367
|
-
toExpPos: Numeric.maxE,
|
|
368
|
-
crypto: false,
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
Numeric.set({ defaults: true });
|
|
372
|
-
Numeric.set(config);
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
export const NegativeOne = new Numeric(-1);
|
|
376
|
-
export const Zero = new Numeric(0);
|
|
377
|
-
export const One = new Numeric(1);
|
|
378
|
-
export const Two = new Numeric(2);
|
|
379
|
-
|
|
380
|
-
export const WeiPerEther = new Numeric(constants.WeiPerEther);
|
|
381
|
-
export const MaxUint256 = new Numeric(constants.MaxUint256);
|
|
382
|
-
export const MaxInt256 = new Numeric(constants.MaxInt256);
|
|
383
|
-
export const MinInt256 = new Numeric(constants.MinInt256);
|
|
13
|
+
export { createArrayCompareFunction } from './helper';
|