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