@pioneer-platform/helpers 4.0.13 → 4.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/CHANGELOG.md +12 -0
- package/lib/helpers/asset.d.ts +1 -1
- package/lib/helpers/asset.js +83 -145
- package/lib/helpers/liquidity.js +52 -73
- package/lib/helpers/memo.js +19 -22
- package/lib/helpers/others.js +6 -7
- package/lib/helpers/request.d.ts +1 -1
- package/lib/helpers/request.js +30 -68
- package/lib/helpers/validators.js +7 -8
- package/lib/modules/assetValue.js +171 -258
- package/lib/modules/bigIntArithmetics.js +180 -225
- package/lib/modules/swapKitError.js +6 -25
- package/lib/modules/swapKitNumber.js +10 -30
- package/package.json +18 -15
- package/src/helpers/asset.ts +0 -5
- package/tsconfig.json +1 -1
- package/LICENSE +0 -674
- package/lib/modules/__tests__/assetValue.test.d.ts +0 -1
- package/lib/modules/__tests__/assetValue.test.js +0 -399
- package/lib/modules/__tests__/swapKitNumber.test.d.ts +0 -1
- package/lib/modules/__tests__/swapKitNumber.test.js +0 -425
@@ -1,22 +1,14 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
3
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
4
|
-
if (ar || !(i in from)) {
|
5
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
6
|
-
ar[i] = from[i];
|
7
|
-
}
|
8
|
-
}
|
9
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
10
|
-
};
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.BigIntArithmetics = exports.
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
3
|
+
exports.BigIntArithmetics = exports.toMultiplier = void 0;
|
4
|
+
exports.formatBigIntToSafeValue = formatBigIntToSafeValue;
|
5
|
+
const types_1 = require("@coinmasters/types");
|
6
|
+
const TAG = " | BigIntArithmetics | ";
|
7
|
+
const DEFAULT_DECIMAL = 8;
|
8
|
+
const bigintPow = (base, exponent) => {
|
9
|
+
let tag = TAG + " | bigintPow | ";
|
18
10
|
try {
|
19
|
-
|
11
|
+
let result = BigInt(1);
|
20
12
|
while (exponent > 0) {
|
21
13
|
if (exponent % 2 === 1) {
|
22
14
|
result *= base;
|
@@ -31,15 +23,15 @@ var bigintPow = function (base, exponent) {
|
|
31
23
|
return BigInt(1);
|
32
24
|
}
|
33
25
|
};
|
34
|
-
|
35
|
-
|
26
|
+
const toMultiplier = (decimal) => {
|
27
|
+
let tag = TAG + " | toMultiplier | ";
|
36
28
|
//console.log(tag + 'input decimal:', decimal);
|
37
29
|
try {
|
38
30
|
if (decimal < 0) {
|
39
31
|
throw new Error("Decimal must be non-negative");
|
40
32
|
}
|
41
|
-
|
42
|
-
for (
|
33
|
+
let result = BigInt(1);
|
34
|
+
for (let i = 0; i < decimal; i++) {
|
43
35
|
result *= BigInt(10);
|
44
36
|
}
|
45
37
|
//console.log(tag + 'result:', result);
|
@@ -51,8 +43,8 @@ var toMultiplier = function (decimal) {
|
|
51
43
|
}
|
52
44
|
};
|
53
45
|
exports.toMultiplier = toMultiplier;
|
54
|
-
|
55
|
-
|
46
|
+
const decimalFromMultiplier = (multiplier) => {
|
47
|
+
let tag = TAG + " | decimalFromMultiplier | ";
|
56
48
|
try {
|
57
49
|
return Math.log10(Number(multiplier));
|
58
50
|
}
|
@@ -61,9 +53,8 @@ var decimalFromMultiplier = function (multiplier) {
|
|
61
53
|
return 0;
|
62
54
|
}
|
63
55
|
};
|
64
|
-
function formatBigIntToSafeValue(
|
65
|
-
|
66
|
-
var tag = TAG + " | BigIntArithmetics | ";
|
56
|
+
function formatBigIntToSafeValue({ value, bigIntDecimal, decimal, }) {
|
57
|
+
const tag = TAG + " | BigIntArithmetics | ";
|
67
58
|
try {
|
68
59
|
//console.log(tag,'value:',value,'bigIntDecimal:',bigIntDecimal,'decimal:',decimal);
|
69
60
|
if (!decimal)
|
@@ -75,7 +66,7 @@ function formatBigIntToSafeValue(_a) {
|
|
75
66
|
throw new Error(TAG + 'Negative value is not allowed');
|
76
67
|
}
|
77
68
|
// Convert bigint to string and then to number
|
78
|
-
|
69
|
+
let valueString = value.toString();
|
79
70
|
//console.log(tag,'valueString:',valueString);
|
80
71
|
//console.log(tag,'valueString:',valueString.length);
|
81
72
|
//console.log(tag,'decimal:',decimal);
|
@@ -84,10 +75,10 @@ function formatBigIntToSafeValue(_a) {
|
|
84
75
|
valueString = '0'.repeat(decimal - valueString.length + 1) + valueString;
|
85
76
|
}
|
86
77
|
// Determine the integer and decimal parts
|
87
|
-
|
88
|
-
|
78
|
+
const integerPart = valueString.slice(0, valueString.length - decimal) || '0';
|
79
|
+
const decimalPart = valueString.slice(valueString.length - decimal).padEnd(decimal, '0');
|
89
80
|
// Construct the final formatted value
|
90
|
-
|
81
|
+
const formattedValue = `${integerPart}.${decimalPart}`;
|
91
82
|
//console.log(tag,"formattedValue:", formattedValue);
|
92
83
|
return formattedValue;
|
93
84
|
}
|
@@ -96,8 +87,21 @@ function formatBigIntToSafeValue(_a) {
|
|
96
87
|
return '';
|
97
88
|
}
|
98
89
|
}
|
99
|
-
|
100
|
-
|
90
|
+
class BigIntArithmetics {
|
91
|
+
static fromBigInt(value, decimal) {
|
92
|
+
let tag = TAG + " | fromBigInt | ";
|
93
|
+
try {
|
94
|
+
//console.log(tag,"Decimal: ", decimal);
|
95
|
+
return new BigIntArithmetics({
|
96
|
+
decimal,
|
97
|
+
value: formatBigIntToSafeValue({ value, bigIntDecimal: (0, exports.toMultiplier)(decimal || DEFAULT_DECIMAL), decimal }),
|
98
|
+
});
|
99
|
+
}
|
100
|
+
catch (error) {
|
101
|
+
console.error(tag + 'Error in fromBigInt:', error);
|
102
|
+
return new BigIntArithmetics(0);
|
103
|
+
}
|
104
|
+
}
|
101
105
|
// static shiftDecimals({
|
102
106
|
// value,
|
103
107
|
// from,
|
@@ -135,13 +139,13 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
135
139
|
caip: 'eip155:8453/slip44:60'
|
136
140
|
}
|
137
141
|
*/
|
138
|
-
|
139
|
-
|
142
|
+
constructor(params) {
|
143
|
+
let tag = TAG + " | constructor | ";
|
140
144
|
try {
|
141
145
|
////console.log(tag + 'Constructor Params:', params);
|
142
|
-
|
146
|
+
const value = getStringValue(params);
|
143
147
|
////console.log(tag + 'String Value:', value);
|
144
|
-
|
148
|
+
const isComplex = typeof params === 'object' && params !== null;
|
145
149
|
this.decimal = isComplex ? params.decimal : undefined;
|
146
150
|
////console.log(tag , 'Decimal:', this.decimal);
|
147
151
|
////console.log(tag , 'isComplex:', isComplex);
|
@@ -149,7 +153,7 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
149
153
|
this.decimalMultiplier = (0, exports.toMultiplier)(this.decimal || DEFAULT_DECIMAL);
|
150
154
|
}
|
151
155
|
else {
|
152
|
-
|
156
|
+
const maxDecimals = Math.max(getFloatDecimals(toSafeValue(value)), this.decimal || 0);
|
153
157
|
this.decimalMultiplier = (0, exports.toMultiplier)(maxDecimals);
|
154
158
|
}
|
155
159
|
////console.log(tag + 'Decimal Multiplier:', this.decimalMultiplier);
|
@@ -163,20 +167,6 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
163
167
|
this.bigIntValue = BigInt(0);
|
164
168
|
}
|
165
169
|
}
|
166
|
-
BigIntArithmetics.fromBigInt = function (value, decimal) {
|
167
|
-
var tag = TAG + " | fromBigInt | ";
|
168
|
-
try {
|
169
|
-
//console.log(tag,"Decimal: ", decimal);
|
170
|
-
return new BigIntArithmetics({
|
171
|
-
decimal: decimal,
|
172
|
-
value: formatBigIntToSafeValue({ value: value, bigIntDecimal: (0, exports.toMultiplier)(decimal || DEFAULT_DECIMAL), decimal: decimal }),
|
173
|
-
});
|
174
|
-
}
|
175
|
-
catch (error) {
|
176
|
-
console.error(tag + 'Error in fromBigInt:', error);
|
177
|
-
return new BigIntArithmetics(0);
|
178
|
-
}
|
179
|
-
};
|
180
170
|
// constructor(params: SKBigIntParams) {
|
181
171
|
// let tag = TAG + " | constructor | ";
|
182
172
|
// try {
|
@@ -204,75 +194,59 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
204
194
|
// this.bigIntValue = BigInt(0);
|
205
195
|
// }
|
206
196
|
// }
|
207
|
-
|
208
|
-
|
197
|
+
set(value) {
|
198
|
+
let tag = TAG + " | set | ";
|
209
199
|
try {
|
210
200
|
// @ts-ignore
|
211
|
-
return new BigIntArithmetics({ decimal: this.decimal, value
|
201
|
+
return new BigIntArithmetics({ decimal: this.decimal, value });
|
212
202
|
}
|
213
203
|
catch (error) {
|
214
204
|
console.error(tag + 'Error in set:', error);
|
215
205
|
return this;
|
216
206
|
}
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
221
|
-
args[_i] = arguments[_i];
|
222
|
-
}
|
223
|
-
var tag = TAG + " | add | ";
|
207
|
+
}
|
208
|
+
add(...args) {
|
209
|
+
let tag = TAG + " | add | ";
|
224
210
|
try {
|
225
|
-
return this.arithmetics
|
211
|
+
return this.arithmetics('add', ...args);
|
226
212
|
}
|
227
213
|
catch (error) {
|
228
214
|
console.error(tag + 'Error in add:', error);
|
229
215
|
return this;
|
230
216
|
}
|
231
|
-
}
|
232
|
-
|
233
|
-
|
234
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
235
|
-
args[_i] = arguments[_i];
|
236
|
-
}
|
237
|
-
var tag = TAG + " | sub | ";
|
217
|
+
}
|
218
|
+
sub(...args) {
|
219
|
+
let tag = TAG + " | sub | ";
|
238
220
|
try {
|
239
|
-
return this.arithmetics
|
221
|
+
return this.arithmetics('sub', ...args);
|
240
222
|
}
|
241
223
|
catch (error) {
|
242
224
|
console.error(tag + 'Error in sub:', error);
|
243
225
|
return this;
|
244
226
|
}
|
245
|
-
}
|
246
|
-
|
247
|
-
|
248
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
249
|
-
args[_i] = arguments[_i];
|
250
|
-
}
|
251
|
-
var tag = TAG + " | mul | ";
|
227
|
+
}
|
228
|
+
mul(...args) {
|
229
|
+
let tag = TAG + " | mul | ";
|
252
230
|
try {
|
253
|
-
return this.arithmetics
|
231
|
+
return this.arithmetics('mul', ...args);
|
254
232
|
}
|
255
233
|
catch (error) {
|
256
234
|
console.error(tag + 'Error in mul:', error);
|
257
235
|
return this;
|
258
236
|
}
|
259
|
-
}
|
260
|
-
|
261
|
-
|
262
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
263
|
-
args[_i] = arguments[_i];
|
264
|
-
}
|
265
|
-
var tag = TAG + " | div | ";
|
237
|
+
}
|
238
|
+
div(...args) {
|
239
|
+
let tag = TAG + " | div | ";
|
266
240
|
try {
|
267
|
-
return this.arithmetics
|
241
|
+
return this.arithmetics('div', ...args);
|
268
242
|
}
|
269
243
|
catch (error) {
|
270
244
|
console.error(tag + 'Error in div:', error);
|
271
245
|
return this;
|
272
246
|
}
|
273
|
-
}
|
274
|
-
|
275
|
-
|
247
|
+
}
|
248
|
+
gt(value) {
|
249
|
+
let tag = TAG + " | gt | ";
|
276
250
|
try {
|
277
251
|
return this.comparison('gt', value);
|
278
252
|
}
|
@@ -280,9 +254,9 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
280
254
|
console.error(tag + 'Error in gt:', error);
|
281
255
|
return false;
|
282
256
|
}
|
283
|
-
}
|
284
|
-
|
285
|
-
|
257
|
+
}
|
258
|
+
gte(value) {
|
259
|
+
let tag = TAG + " | gte | ";
|
286
260
|
try {
|
287
261
|
return this.comparison('gte', value);
|
288
262
|
}
|
@@ -290,9 +264,9 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
290
264
|
console.error(tag + 'Error in gte:', error);
|
291
265
|
return false;
|
292
266
|
}
|
293
|
-
}
|
294
|
-
|
295
|
-
|
267
|
+
}
|
268
|
+
lt(value) {
|
269
|
+
let tag = TAG + " | lt | ";
|
296
270
|
try {
|
297
271
|
return this.comparison('lt', value);
|
298
272
|
}
|
@@ -300,9 +274,9 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
300
274
|
console.error(tag + 'Error in lt:', error);
|
301
275
|
return false;
|
302
276
|
}
|
303
|
-
}
|
304
|
-
|
305
|
-
|
277
|
+
}
|
278
|
+
lte(value) {
|
279
|
+
let tag = TAG + " | lte | ";
|
306
280
|
try {
|
307
281
|
return this.comparison('lte', value);
|
308
282
|
}
|
@@ -310,9 +284,9 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
310
284
|
console.error(tag + 'Error in lte:', error);
|
311
285
|
return false;
|
312
286
|
}
|
313
|
-
}
|
314
|
-
|
315
|
-
|
287
|
+
}
|
288
|
+
eqValue(value) {
|
289
|
+
let tag = TAG + " | eqValue | ";
|
316
290
|
try {
|
317
291
|
return this.comparison('eqValue', value);
|
318
292
|
}
|
@@ -320,14 +294,14 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
320
294
|
console.error(tag + 'Error in eqValue:', error);
|
321
295
|
return false;
|
322
296
|
}
|
323
|
-
}
|
297
|
+
}
|
324
298
|
//@ts-ignore
|
325
|
-
|
326
|
-
|
299
|
+
getValue(type) {
|
300
|
+
let tag = TAG + " | getValue | ";
|
327
301
|
try {
|
328
302
|
//console.log(tag,'getValue type:', type);
|
329
303
|
//console.log(tag,'decimalMultiplier: ', this.decimalMultiplier);
|
330
|
-
|
304
|
+
const value = formatBigIntToSafeValue({
|
331
305
|
value: this.bigIntValue,
|
332
306
|
bigIntDecimal: (0, exports.toMultiplier)(this.decimal || DEFAULT_DECIMAL),
|
333
307
|
decimal: this.decimal || DEFAULT_DECIMAL,
|
@@ -347,13 +321,13 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
347
321
|
console.error(tag + 'Error in getValue:', error);
|
348
322
|
return null;
|
349
323
|
}
|
350
|
-
}
|
324
|
+
}
|
351
325
|
// @ts-ignore
|
352
|
-
|
353
|
-
|
326
|
+
getBaseValue(type) {
|
327
|
+
let tag = TAG + " | getBaseValue | ";
|
354
328
|
try {
|
355
|
-
|
356
|
-
|
329
|
+
const divisor = this.decimalMultiplier / (0, exports.toMultiplier)(this.decimal || types_1.BaseDecimal.THOR);
|
330
|
+
const baseValue = this.bigIntValue / divisor;
|
357
331
|
switch (type) {
|
358
332
|
case 'number':
|
359
333
|
return Number(baseValue);
|
@@ -367,15 +341,15 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
367
341
|
console.error(tag + 'Error in getBaseValue:', error);
|
368
342
|
return null;
|
369
343
|
}
|
370
|
-
}
|
371
|
-
|
372
|
-
|
344
|
+
}
|
345
|
+
getBigIntValue(value, decimal) {
|
346
|
+
let tag = TAG + " | getBigIntValue | ";
|
373
347
|
try {
|
374
348
|
if (!decimal && typeof value === 'object' && value !== null) {
|
375
349
|
return value.bigIntValue;
|
376
350
|
}
|
377
|
-
|
378
|
-
|
351
|
+
const stringValue = getStringValue(value);
|
352
|
+
const safeValue = toSafeValue(stringValue);
|
379
353
|
if (safeValue === '0' || safeValue === 'undefined')
|
380
354
|
return BigInt(0);
|
381
355
|
return this.toBigInt(safeValue, decimal);
|
@@ -384,15 +358,14 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
384
358
|
console.error(tag + 'Error in getBigIntValue:', error);
|
385
359
|
return BigInt(0);
|
386
360
|
}
|
387
|
-
}
|
388
|
-
|
389
|
-
|
390
|
-
var tag = TAG + " | toSignificant | ";
|
361
|
+
}
|
362
|
+
toSignificant(significantDigits = 6) {
|
363
|
+
let tag = TAG + " | toSignificant | ";
|
391
364
|
try {
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
365
|
+
const [int, dec] = this.getValue('string').split('.');
|
366
|
+
const integer = int || '';
|
367
|
+
const decimal = dec || '';
|
368
|
+
const valueLength = parseInt(integer) ? integer.length + decimal.length : decimal.length;
|
396
369
|
if (valueLength <= significantDigits) {
|
397
370
|
return this.getValue('string');
|
398
371
|
}
|
@@ -400,111 +373,102 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
400
373
|
return integer.slice(0, significantDigits).padEnd(integer.length, '0');
|
401
374
|
}
|
402
375
|
if (parseInt(integer)) {
|
403
|
-
return
|
376
|
+
return `${integer}.${decimal.slice(0, significantDigits - integer.length)}`.padEnd(significantDigits - integer.length, '0');
|
404
377
|
}
|
405
|
-
|
406
|
-
|
407
|
-
return
|
378
|
+
const trimmedDecimal = parseInt(decimal);
|
379
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, significantDigits);
|
380
|
+
return `0.${slicedDecimal.padStart(decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length, '0')}`;
|
408
381
|
}
|
409
382
|
catch (error) {
|
410
383
|
console.error(tag + 'Error in toSignificant:', error);
|
411
384
|
return '';
|
412
385
|
}
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
var tag = TAG + " | toFixed | ";
|
386
|
+
}
|
387
|
+
toFixed(fixedDigits = 6) {
|
388
|
+
let tag = TAG + " | toFixed | ";
|
417
389
|
try {
|
418
|
-
|
419
|
-
|
420
|
-
|
390
|
+
const [int, dec] = this.getValue('string').split('.');
|
391
|
+
const integer = int || '';
|
392
|
+
const decimal = dec || '';
|
421
393
|
if (parseInt(integer)) {
|
422
|
-
return
|
394
|
+
return `${integer}.${decimal.slice(0, fixedDigits)}`.padEnd(fixedDigits, '0');
|
423
395
|
}
|
424
|
-
|
425
|
-
|
426
|
-
return
|
396
|
+
const trimmedDecimal = parseInt(decimal);
|
397
|
+
const slicedDecimal = `${trimmedDecimal}`.slice(0, fixedDigits);
|
398
|
+
return `0.${slicedDecimal.padStart(decimal.length - `${trimmedDecimal}`.length + slicedDecimal.length, '0')}`;
|
427
399
|
}
|
428
400
|
catch (error) {
|
429
401
|
console.error(tag + 'Error in toFixed:', error);
|
430
402
|
return '';
|
431
403
|
}
|
432
|
-
}
|
433
|
-
|
434
|
-
|
435
|
-
var tag = TAG + " | toAbbreviation | ";
|
404
|
+
}
|
405
|
+
toAbbreviation(digits = 2) {
|
406
|
+
let tag = TAG + " | toAbbreviation | ";
|
436
407
|
try {
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
408
|
+
const value = this.getValue('number');
|
409
|
+
const abbreviations = ['', 'K', 'M', 'B', 'T', 'Q', 'Qi', 'S'];
|
410
|
+
const tier = Math.floor(Math.log10(Math.abs(value)) / 3);
|
411
|
+
const suffix = abbreviations[tier];
|
441
412
|
if (!suffix)
|
442
413
|
return this.getValue('string');
|
443
|
-
|
444
|
-
|
445
|
-
return
|
414
|
+
const scale = Math.pow(10, (tier * 3));
|
415
|
+
const scaled = value / scale;
|
416
|
+
return `${scaled.toFixed(digits)}${suffix}`;
|
446
417
|
}
|
447
418
|
catch (error) {
|
448
419
|
console.error(tag + 'Error in toAbbreviation:', error);
|
449
420
|
return '';
|
450
421
|
}
|
451
|
-
}
|
452
|
-
|
453
|
-
|
454
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.currencyPosition, currencyPosition = _c === void 0 ? 'start' : _c, _d = _b.decimal, decimal = _d === void 0 ? 2 : _d, _e = _b.decimalSeparator, decimalSeparator = _e === void 0 ? '.' : _e, _f = _b.thousandSeparator, thousandSeparator = _f === void 0 ? ',' : _f;
|
455
|
-
var tag = TAG + " | toCurrency | ";
|
422
|
+
}
|
423
|
+
toCurrency(currency = '$', { currencyPosition = 'start', decimal = 2, decimalSeparator = '.', thousandSeparator = ',', } = {}) {
|
424
|
+
let tag = TAG + " | toCurrency | ";
|
456
425
|
try {
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
426
|
+
const value = this.getValue('number');
|
427
|
+
const [int, dec = ''] = value.toFixed(6).split('.');
|
428
|
+
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator);
|
429
|
+
const parsedValue = !int && !dec
|
461
430
|
? '0.00'
|
462
431
|
: int === '0'
|
463
|
-
?
|
464
|
-
:
|
465
|
-
return
|
432
|
+
? `${parseFloat(`0.${dec}`)}`.replace('.', decimalSeparator)
|
433
|
+
: `${integer}${parseInt(dec) ? `${decimalSeparator}${dec.slice(0, decimal)}` : ''}`;
|
434
|
+
return `${currencyPosition === 'start' ? currency : ''}${parsedValue}${currencyPosition === 'end' ? currency : ''}`;
|
466
435
|
}
|
467
436
|
catch (error) {
|
468
437
|
console.error(tag + 'Error in toCurrency:', error);
|
469
438
|
return '';
|
470
439
|
}
|
471
|
-
}
|
472
|
-
|
473
|
-
|
474
|
-
var args = [];
|
475
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
476
|
-
args[_i - 1] = arguments[_i];
|
477
|
-
}
|
478
|
-
var tag = TAG + " | arithmetics | ";
|
440
|
+
}
|
441
|
+
arithmetics(method, ...args) {
|
442
|
+
let tag = TAG + " | arithmetics | ";
|
479
443
|
try {
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
444
|
+
const precisionDecimal = this.retrievePrecisionDecimal(this, ...args);
|
445
|
+
const decimal = Math.max(precisionDecimal, decimalFromMultiplier(this.decimalMultiplier));
|
446
|
+
const precisionDecimalMultiplier = (0, exports.toMultiplier)(decimal);
|
447
|
+
const result = args.reduce((acc, arg) => {
|
448
|
+
const value = this.getBigIntValue(arg, decimal);
|
485
449
|
switch (method) {
|
486
450
|
case 'add':
|
487
451
|
return acc + value;
|
488
452
|
case 'sub':
|
489
453
|
return acc - value;
|
490
454
|
case 'mul':
|
491
|
-
return (acc * value) /
|
455
|
+
return (acc * value) / precisionDecimalMultiplier;
|
492
456
|
case 'div': {
|
493
457
|
if (value === BigInt(0))
|
494
458
|
throw new RangeError('Division by zero');
|
495
|
-
return (acc *
|
459
|
+
return (acc * precisionDecimalMultiplier) / value;
|
496
460
|
}
|
497
461
|
default:
|
498
462
|
return acc;
|
499
463
|
}
|
500
|
-
}, (this.bigIntValue *
|
501
|
-
|
502
|
-
bigIntDecimal: (0, exports.toMultiplier)(
|
503
|
-
decimal
|
464
|
+
}, (this.bigIntValue * precisionDecimalMultiplier) / this.decimalMultiplier);
|
465
|
+
const formattedValue = formatBigIntToSafeValue({
|
466
|
+
bigIntDecimal: (0, exports.toMultiplier)(decimal),
|
467
|
+
decimal,
|
504
468
|
value: result,
|
505
469
|
});
|
506
470
|
return new BigIntArithmetics({
|
507
|
-
decimalMultiplier: (0, exports.toMultiplier)(
|
471
|
+
decimalMultiplier: (0, exports.toMultiplier)(decimal),
|
508
472
|
decimal: this.decimal,
|
509
473
|
value: formattedValue,
|
510
474
|
});
|
@@ -513,17 +477,13 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
513
477
|
console.error(tag + 'Error in arithmetics:', error);
|
514
478
|
return this;
|
515
479
|
}
|
516
|
-
}
|
517
|
-
|
518
|
-
|
519
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
520
|
-
args[_i - 1] = arguments[_i];
|
521
|
-
}
|
522
|
-
var tag = TAG + " | comparison | ";
|
480
|
+
}
|
481
|
+
comparison(method, ...args) {
|
482
|
+
let tag = TAG + " | comparison | ";
|
523
483
|
try {
|
524
|
-
|
525
|
-
|
526
|
-
|
484
|
+
const decimal = this.retrievePrecisionDecimal(this, ...args);
|
485
|
+
const value = this.getBigIntValue(args[0], decimal);
|
486
|
+
const compareToValue = this.getBigIntValue(this, decimal);
|
527
487
|
switch (method) {
|
528
488
|
case 'gt':
|
529
489
|
return compareToValue > value;
|
@@ -541,70 +501,65 @@ var BigIntArithmetics = /** @class */ (function () {
|
|
541
501
|
console.error(tag + 'Error in comparison:', error);
|
542
502
|
return false;
|
543
503
|
}
|
544
|
-
}
|
545
|
-
|
546
|
-
|
504
|
+
}
|
505
|
+
setValue(value) {
|
506
|
+
let tag = TAG + " | setValue | ";
|
547
507
|
try {
|
548
508
|
//console.log(tag, 'value:', value);
|
549
509
|
//console.log(tag, ' this.decimal:', this.decimal);
|
550
|
-
|
510
|
+
const safeValue = formatBigIntToSafeValue({ value: value, decimal: this.decimal });
|
551
511
|
//console.log(tag, 'safeValue:', safeValue);
|
552
512
|
this.bigIntValue = this.toBigInt(safeValue);
|
553
513
|
}
|
554
514
|
catch (error) {
|
555
515
|
console.error(tag + 'Error in setValue:', error);
|
556
516
|
}
|
557
|
-
}
|
558
|
-
|
559
|
-
|
560
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
561
|
-
args[_i] = arguments[_i];
|
562
|
-
}
|
563
|
-
var tag = TAG + " | retrievePrecisionDecimal | ";
|
517
|
+
}
|
518
|
+
retrievePrecisionDecimal(...args) {
|
519
|
+
let tag = TAG + " | retrievePrecisionDecimal | ";
|
564
520
|
try {
|
565
|
-
|
566
|
-
.map(
|
567
|
-
|
568
|
-
|
521
|
+
const decimals = args
|
522
|
+
.map((arg) => {
|
523
|
+
const isObject = typeof arg === 'object' && arg !== null;
|
524
|
+
const value = isObject
|
569
525
|
? arg.decimal || decimalFromMultiplier(arg.decimalMultiplier)
|
570
526
|
: getFloatDecimals(toSafeValue(arg));
|
571
527
|
return value;
|
572
528
|
})
|
573
529
|
.filter(Boolean);
|
574
|
-
return Math.max
|
530
|
+
return Math.max(...decimals, DEFAULT_DECIMAL);
|
575
531
|
}
|
576
532
|
catch (error) {
|
577
533
|
console.error(tag + 'Error in retrievePrecisionDecimal:', error);
|
578
534
|
return DEFAULT_DECIMAL;
|
579
535
|
}
|
580
|
-
}
|
581
|
-
|
582
|
-
|
536
|
+
}
|
537
|
+
toBigInt(value, decimal) {
|
538
|
+
let tag = TAG + " | toBigInt | ";
|
583
539
|
try {
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
return BigInt(
|
540
|
+
const multiplier = decimal ? (0, exports.toMultiplier)(decimal) : this.decimalMultiplier;
|
541
|
+
const padDecimal = decimalFromMultiplier(multiplier);
|
542
|
+
const [integerPart = '', decimalPart = ''] = value.split('.');
|
543
|
+
return BigInt(`${integerPart}${decimalPart.padEnd(padDecimal, '0')}`);
|
588
544
|
}
|
589
545
|
catch (error) {
|
590
546
|
console.error(tag + 'Error in toBigInt:', error);
|
591
547
|
return BigInt(0);
|
592
548
|
}
|
593
|
-
}
|
594
|
-
|
595
|
-
}());
|
549
|
+
}
|
550
|
+
}
|
596
551
|
exports.BigIntArithmetics = BigIntArithmetics;
|
597
|
-
|
552
|
+
const numberFormatter = Intl.NumberFormat('fullwide', {
|
598
553
|
useGrouping: false,
|
599
554
|
maximumFractionDigits: 20,
|
600
555
|
});
|
601
556
|
function toSafeValue(value) {
|
602
|
-
|
557
|
+
let tag = TAG + " | toSafeValue | ";
|
603
558
|
try {
|
604
|
-
|
605
|
-
|
559
|
+
const parsedValue = typeof value === 'number' ? numberFormatter.format(value) : getStringValue(value);
|
560
|
+
const splitValue = `${parsedValue}`.replace(/,/g, '.').split('.');
|
606
561
|
return splitValue.length > 1
|
607
|
-
?
|
562
|
+
? `${splitValue.slice(0, -1).join('')}.${splitValue.at(-1)}`
|
608
563
|
: splitValue[0];
|
609
564
|
}
|
610
565
|
catch (error) {
|
@@ -613,10 +568,10 @@ function toSafeValue(value) {
|
|
613
568
|
}
|
614
569
|
}
|
615
570
|
function getFloatDecimals(value) {
|
616
|
-
|
571
|
+
let tag = TAG + " | getFloatDecimals | ";
|
617
572
|
try {
|
618
|
-
|
619
|
-
|
573
|
+
let decimals = 0;
|
574
|
+
const parts = value.split('.');
|
620
575
|
if (parts.length > 1 && parts[1].length > 0) {
|
621
576
|
decimals = parts[1].length;
|
622
577
|
}
|
@@ -628,7 +583,7 @@ function getFloatDecimals(value) {
|
|
628
583
|
}
|
629
584
|
}
|
630
585
|
function getStringValue(param) {
|
631
|
-
|
586
|
+
let tag = TAG + " | getStringValue | ";
|
632
587
|
try {
|
633
588
|
return typeof param === 'object' && param !== null
|
634
589
|
? 'getValue' in param
|