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