bigint-decimal 1.0.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/LICENSE +21 -0
- package/README.md +143 -0
- package/dist/common/index.d.ts +28 -0
- package/dist/common/index.js +244 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/utils.d.ts +2 -0
- package/dist/common/utils.js +24 -0
- package/dist/common/utils.js.map +1 -0
- package/dist/global/index.d.ts +28 -0
- package/dist/global/index.js +240 -0
- package/dist/global/index.js.map +1 -0
- package/dist/global/utils.d.ts +2 -0
- package/dist/global/utils.js +19 -0
- package/dist/global/utils.js.map +1 -0
- package/package.json +44 -0
- package/tsconfig.base.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 soso
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# BigintDecimal
|
|
2
|
+
|
|
3
|
+
A blazing-fast, zero-dependency, arbitrary-precision decimal math library for TypeScript and JavaScript. Built on native `BigInt` to safely handle massive numbers, exact decimals, and eliminate floating-point errors.
|
|
4
|
+
|
|
5
|
+
Perfect for financial applications, trading platforms, and anywhere precise calculation is critical.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install bigint-decimal
|
|
11
|
+
# or
|
|
12
|
+
yarn add bigint-decimal
|
|
13
|
+
# or
|
|
14
|
+
pnpm add bigint-decimal
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Why bigint-decimal?
|
|
19
|
+
|
|
20
|
+
Native JavaScript floating-point math is notoriously imprecise:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
console.log(0.1 + 0.2); // 0.30000000000000004 ❌
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`bigint-decimal` fixes this natively, without the bloat of massive external math libraries:
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { BigintDecimal } from 'bigint-decimal';
|
|
31
|
+
|
|
32
|
+
const a = new BigintDecimal("0.1");
|
|
33
|
+
const b = new BigintDecimal("0.2");
|
|
34
|
+
console.log(a.plus(b).toString()); // "0.3" ✅
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
### Basic Arithmetic
|
|
41
|
+
|
|
42
|
+
`BigintDecimal` supports all standard arithmetic operations. Always pass numbers as strings to guarantee precision before JavaScript evaluates them.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { BigintDecimal } from 'bigint-decimal';
|
|
46
|
+
|
|
47
|
+
const walletBalance = new BigintDecimal("1500.50");
|
|
48
|
+
const tradeAmount = new BigintDecimal("200.75");
|
|
49
|
+
const gasFee = new BigintDecimal("0.005");
|
|
50
|
+
|
|
51
|
+
// Addition
|
|
52
|
+
const newBalance = walletBalance.plus(tradeAmount); // "1701.25"
|
|
53
|
+
|
|
54
|
+
// Subtraction
|
|
55
|
+
const finalBalance = newBalance.minus(gasFee); // "1701.245"
|
|
56
|
+
|
|
57
|
+
// Multiplication
|
|
58
|
+
const price = new BigintDecimal("45000.50");
|
|
59
|
+
const amount = new BigintDecimal("0.5");
|
|
60
|
+
const totalValue = price.times(amount); // "22500.25"
|
|
61
|
+
|
|
62
|
+
// Division
|
|
63
|
+
const totalTokens = new BigintDecimal("1000");
|
|
64
|
+
const splits = new BigintDecimal("3");
|
|
65
|
+
const perPerson = totalTokens.div(splits); // "333.33333333333333333333"
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Chaining Operations
|
|
70
|
+
|
|
71
|
+
Every math operation returns a new `BigintDecimal` instance, allowing you to cleanly chain complex formulas.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const result = new BigintDecimal("100")
|
|
75
|
+
.plus("50.5")
|
|
76
|
+
.times("2")
|
|
77
|
+
.minus("1.1")
|
|
78
|
+
.div("2");
|
|
79
|
+
|
|
80
|
+
console.log(result.toString()); // "149.95"
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Comparisons
|
|
85
|
+
|
|
86
|
+
Easily compare values without precision loss.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const requiredMargin = new BigintDecimal("1000.00");
|
|
90
|
+
const userBalance = new BigintDecimal("999.99");
|
|
91
|
+
|
|
92
|
+
userBalance.gt(requiredMargin); // false (Greater Than)
|
|
93
|
+
userBalance.gte(requiredMargin); // false (Greater Than or Equal)
|
|
94
|
+
userBalance.lt(requiredMargin); // true (Less Than)
|
|
95
|
+
userBalance.lte(requiredMargin); // true (Less Than or Equal)
|
|
96
|
+
userBalance.eq("999.99"); // true (Equal)
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Rounding
|
|
101
|
+
|
|
102
|
+
Round to a specific number of decimal places. Standard rounding rules apply (halves round up).
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const fee = new BigintDecimal("1.555");
|
|
106
|
+
console.log(fee.round(2).toString()); // "1.56"
|
|
107
|
+
console.log(fee.round(0).toString()); // "2"
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Managing Precision (For Division)
|
|
112
|
+
|
|
113
|
+
Division operations can result in infinitely repeating decimals. `BigintDecimal` handles this by defaulting to a maximum precision of `20` decimal places. You can adjust this globally or per instance.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
// Change instance precision
|
|
117
|
+
const exactDiv = new BigintDecimal("1");
|
|
118
|
+
exactDiv.setPrecision(5);
|
|
119
|
+
console.log(exactDiv.div("3").toString()); // "0.33333"
|
|
120
|
+
|
|
121
|
+
// Change global precision for all new instances
|
|
122
|
+
BigintDecimal.setPrecisionGlobal(2);
|
|
123
|
+
console.log(new BigintDecimal("1").div("3").toString()); // "0.33"
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Supported Inputs
|
|
128
|
+
|
|
129
|
+
`BigintDecimal` safely parses a wide variety of inputs, including scientific notation and massive integers.
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
new BigintDecimal("1e-18"); // "0.000000000000000001"
|
|
133
|
+
new BigintDecimal("9999999999999999999999999999"); // Handles massive numbers natively
|
|
134
|
+
new BigintDecimal("-0.0005"); // Negative floats
|
|
135
|
+
new BigintDecimal(100n); // Native BigInt
|
|
136
|
+
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
*Note: While passing `number` primitives (like `new BigintDecimal(100)`) is supported, passing them as strings (`"100"`) is strongly recommended to avoid JS evaluating floating points before the class receives them.*
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type SafeValues = string | number | bigint;
|
|
2
|
+
export declare class BigintDecimal {
|
|
3
|
+
private value;
|
|
4
|
+
private decimals;
|
|
5
|
+
static precision: number;
|
|
6
|
+
precision: number;
|
|
7
|
+
constructor(val?: SafeValues | BigintDecimal);
|
|
8
|
+
static setPrecisionGlobal(newPrecision: number): void;
|
|
9
|
+
setPrecision(newPrecision: number): void;
|
|
10
|
+
private trim;
|
|
11
|
+
private toSafeValue;
|
|
12
|
+
private getNumberIFoForBigintDecimal;
|
|
13
|
+
toString(): string;
|
|
14
|
+
toJSON(): string;
|
|
15
|
+
isBigintDecimal(val: unknown): boolean;
|
|
16
|
+
plus(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
17
|
+
minus(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
18
|
+
times(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
19
|
+
round(toDecimals: number): BigintDecimal;
|
|
20
|
+
div(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
21
|
+
private compare;
|
|
22
|
+
gt(val: SafeValues | BigintDecimal): boolean;
|
|
23
|
+
lt(val: SafeValues | BigintDecimal): boolean;
|
|
24
|
+
gte(val: SafeValues | BigintDecimal): boolean;
|
|
25
|
+
lte(val: SafeValues | BigintDecimal): boolean;
|
|
26
|
+
eq(val: SafeValues | BigintDecimal): boolean;
|
|
27
|
+
}
|
|
28
|
+
export { BigintDecimal as default };
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = exports.BigintDecimal = void 0;
|
|
4
|
+
class BigintDecimal {
|
|
5
|
+
constructor(val = "0") {
|
|
6
|
+
this.value = 0n;
|
|
7
|
+
this.decimals = 0;
|
|
8
|
+
this.precision = BigintDecimal.precision;
|
|
9
|
+
if (val instanceof BigintDecimal) {
|
|
10
|
+
this.value = val.value;
|
|
11
|
+
this.decimals = val.decimals;
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (!this.isBigintDecimal(val)) {
|
|
15
|
+
throw new Error(`"${val}" is not safe parameter .`);
|
|
16
|
+
}
|
|
17
|
+
const numberInfo = this.getNumberIFoForBigintDecimal(val);
|
|
18
|
+
this.value = numberInfo.value;
|
|
19
|
+
this.decimals = numberInfo.decimals;
|
|
20
|
+
this.trim();
|
|
21
|
+
}
|
|
22
|
+
static setPrecisionGlobal(newPrecision) {
|
|
23
|
+
this.precision = newPrecision;
|
|
24
|
+
}
|
|
25
|
+
setPrecision(newPrecision) {
|
|
26
|
+
this.precision = newPrecision;
|
|
27
|
+
}
|
|
28
|
+
trim() {
|
|
29
|
+
if (this.value === 0n) {
|
|
30
|
+
this.value = 0n;
|
|
31
|
+
this.decimals = 0;
|
|
32
|
+
}
|
|
33
|
+
// While we have decimals AND the last digit is 0
|
|
34
|
+
while (this.decimals > 0 && this.value !== 0n && this.value % 10n === 0n) {
|
|
35
|
+
this.value /= 10n; // Shrink the number
|
|
36
|
+
this.decimals--; // Shrink the scale
|
|
37
|
+
}
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
toSafeValue(val) {
|
|
41
|
+
if (typeof val === "string") {
|
|
42
|
+
return val.trim();
|
|
43
|
+
}
|
|
44
|
+
return `${val}`;
|
|
45
|
+
}
|
|
46
|
+
getNumberIFoForBigintDecimal(val) {
|
|
47
|
+
const safeVal = this.toSafeValue(val);
|
|
48
|
+
let isDot = false, value = 0n, decimals = 0, sign = 1n;
|
|
49
|
+
for (let i = 0; i < safeVal.length; i++) {
|
|
50
|
+
const code = safeVal.charCodeAt(i);
|
|
51
|
+
if (code >= 48 && code <= 57) {
|
|
52
|
+
value = (value * 10n) + BigInt(code - 48);
|
|
53
|
+
if (isDot)
|
|
54
|
+
decimals++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (code === 46 && !isDot) {
|
|
58
|
+
isDot = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (i === 0 && (code === 45 || code === 43)) {
|
|
62
|
+
if (code === 45)
|
|
63
|
+
sign = -1n;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// Scientific Notation Handle
|
|
67
|
+
if (code === 101 || code === 69) {
|
|
68
|
+
const exp = parseInt(safeVal.slice(i + 1), 10);
|
|
69
|
+
if (isNaN(exp))
|
|
70
|
+
throw new Error("Invalid exponent");
|
|
71
|
+
decimals -= exp;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
throw new Error(`Unexpected character: ${String.fromCharCode(code)}`);
|
|
75
|
+
}
|
|
76
|
+
// Handle negative decimals (large integers)
|
|
77
|
+
if (decimals < 0) {
|
|
78
|
+
value = value * (10n ** BigInt(-decimals));
|
|
79
|
+
decimals = 0;
|
|
80
|
+
}
|
|
81
|
+
;
|
|
82
|
+
return { value: value * sign, decimals };
|
|
83
|
+
}
|
|
84
|
+
toString() {
|
|
85
|
+
// 1. Handle Integer case (Fastest exit, Zero Allocation overhead)
|
|
86
|
+
if (this.decimals === 0)
|
|
87
|
+
return this.value.toString();
|
|
88
|
+
// 2. Separate Sign and Magnitude
|
|
89
|
+
// Check sign using BigInt math (fastest)
|
|
90
|
+
const isNegative = this.value < 0n;
|
|
91
|
+
// Get the string of digits ONLY.
|
|
92
|
+
// isNegative ? -this.value : this.value -> creates a temporary BigInt
|
|
93
|
+
// .toString() -> creates the string of digits "123"
|
|
94
|
+
// This is cheaper than creating "-123" and slicing it.
|
|
95
|
+
const s = (isNegative ? -this.value : this.value).toString();
|
|
96
|
+
const len = s.length;
|
|
97
|
+
// 3. Logic for insertion vs padding
|
|
98
|
+
if (len > this.decimals) {
|
|
99
|
+
// Case A: 12345 (2 dec) -> 123.45
|
|
100
|
+
// splitIndex is purely digits before the dot
|
|
101
|
+
const splitIndex = len - this.decimals;
|
|
102
|
+
// Build the absolute decimal string
|
|
103
|
+
const result = s.substring(0, splitIndex) + "." + s.substring(splitIndex);
|
|
104
|
+
// Add sign if needed
|
|
105
|
+
return isNegative ? "-" + result : result;
|
|
106
|
+
}
|
|
107
|
+
// Case B: 5 (2 dec) -> 0.05
|
|
108
|
+
// Calculate zeros needed: 2 decimals - length 1 = 1 zero
|
|
109
|
+
const zerosNeeded = this.decimals - len;
|
|
110
|
+
// Build the result directly
|
|
111
|
+
// "0." + "0" + "5"
|
|
112
|
+
return (isNegative ? "-0." : "0.") + "0".repeat(zerosNeeded) + s;
|
|
113
|
+
}
|
|
114
|
+
toJSON() {
|
|
115
|
+
return this.toString();
|
|
116
|
+
}
|
|
117
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
118
|
+
return this.toString();
|
|
119
|
+
}
|
|
120
|
+
isBigintDecimal(val) {
|
|
121
|
+
const s = this.toSafeValue(val);
|
|
122
|
+
if (s.length === 0)
|
|
123
|
+
return false;
|
|
124
|
+
let dot = false, e = false;
|
|
125
|
+
for (let i = 0; i < s.length; i++) {
|
|
126
|
+
const c = s.charCodeAt(i);
|
|
127
|
+
if (c >= 48 && c <= 57)
|
|
128
|
+
continue; // 0-9
|
|
129
|
+
if (c === 46 && !dot && !e) {
|
|
130
|
+
dot = true;
|
|
131
|
+
continue;
|
|
132
|
+
} // .
|
|
133
|
+
if (i === 0 && (c === 45 || c === 43))
|
|
134
|
+
continue; // +/- at start
|
|
135
|
+
if ((c === 101 || c === 69) && !e) { // e/E
|
|
136
|
+
e = true;
|
|
137
|
+
const next = s.charCodeAt(i + 1);
|
|
138
|
+
if (next === 45 || next === 43)
|
|
139
|
+
i++; // Skip sign after 'e'
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
plus(val) {
|
|
147
|
+
const plusValue = new BigintDecimal(val);
|
|
148
|
+
// 1. Determine which number has more decimals
|
|
149
|
+
const d1 = this.decimals;
|
|
150
|
+
const d2 = plusValue.decimals;
|
|
151
|
+
const newDecimal = d1 > d2 ? d1 : d2;
|
|
152
|
+
// 2. Align values (Common Denominator)
|
|
153
|
+
// If 'this' has fewer decimals, scale it up.
|
|
154
|
+
const v1 = d1 < newDecimal ? this.value * (10n ** BigInt(newDecimal - d1)) : this.value;
|
|
155
|
+
// If 'plusValue' has fewer decimals, scale it up.
|
|
156
|
+
const v2 = d2 < newDecimal ? plusValue.value * (10n ** BigInt(newDecimal - d2)) : plusValue.value;
|
|
157
|
+
// 3. Now add and return
|
|
158
|
+
const result = new BigintDecimal("0");
|
|
159
|
+
result.value = v1 + v2;
|
|
160
|
+
result.decimals = newDecimal;
|
|
161
|
+
return result.trim();
|
|
162
|
+
}
|
|
163
|
+
minus(val) {
|
|
164
|
+
const other = new BigintDecimal(val);
|
|
165
|
+
const d1 = this.decimals;
|
|
166
|
+
const d2 = other.decimals;
|
|
167
|
+
const newDecimal = d1 > d2 ? d1 : d2;
|
|
168
|
+
// Scale values to the same precision
|
|
169
|
+
const v1 = d1 < newDecimal ? this.value * (10n ** BigInt(newDecimal - d1)) : this.value;
|
|
170
|
+
const v2 = d2 < newDecimal ? other.value * (10n ** BigInt(newDecimal - d2)) : other.value;
|
|
171
|
+
const result = new BigintDecimal("0"); // Fastest instantiation
|
|
172
|
+
result.value = v1 - v2;
|
|
173
|
+
result.decimals = newDecimal;
|
|
174
|
+
return result.trim();
|
|
175
|
+
}
|
|
176
|
+
times(val) {
|
|
177
|
+
const other = new BigintDecimal(val);
|
|
178
|
+
const result = new BigintDecimal("0");
|
|
179
|
+
// Multiplication: (A * 10^d1) * (B * 10^d2) = (A * B) * 10^(d1 + d2)
|
|
180
|
+
result.value = this.value * other.value;
|
|
181
|
+
result.decimals = this.decimals + other.decimals;
|
|
182
|
+
return result.trim();
|
|
183
|
+
}
|
|
184
|
+
round(toDecimals) {
|
|
185
|
+
const result = new BigintDecimal("0");
|
|
186
|
+
const diff = this.decimals - toDecimals;
|
|
187
|
+
if (diff <= 0)
|
|
188
|
+
return this; // Already more precise than requested
|
|
189
|
+
const power = 10n ** BigInt(diff);
|
|
190
|
+
const half = power / 2n;
|
|
191
|
+
// Standard rounding: add half of the divisor before dividing
|
|
192
|
+
// This turns 0.999... into 1.0
|
|
193
|
+
const roundedValue = (this.value >= 0n) ? (this.value + half) / power : (this.value - half) / power;
|
|
194
|
+
result.value = roundedValue;
|
|
195
|
+
result.decimals = toDecimals;
|
|
196
|
+
return result.trim();
|
|
197
|
+
}
|
|
198
|
+
div(val) {
|
|
199
|
+
const other = val instanceof BigintDecimal ? val : new BigintDecimal(val);
|
|
200
|
+
if (other.value === 0n)
|
|
201
|
+
throw new Error("Division by zero");
|
|
202
|
+
let v1 = this.value;
|
|
203
|
+
const v2 = other.value;
|
|
204
|
+
// 1. Scale up once to your "Max Allowed" precision
|
|
205
|
+
const shift = this.precision + other.decimals - this.decimals;
|
|
206
|
+
if (shift > 0)
|
|
207
|
+
v1 *= 10n ** BigInt(shift);
|
|
208
|
+
else if (shift < 0)
|
|
209
|
+
v1 /= 10n ** BigInt(-shift);
|
|
210
|
+
const result = new BigintDecimal("0");
|
|
211
|
+
result.value = v1 / v2;
|
|
212
|
+
result.decimals = this.precision;
|
|
213
|
+
// 2. Immediately trim it!
|
|
214
|
+
// This is where 0.500000... becomes 0.5 automatically.
|
|
215
|
+
return result.trim();
|
|
216
|
+
}
|
|
217
|
+
compare(val) {
|
|
218
|
+
const other = val instanceof BigintDecimal ? val : new BigintDecimal(val);
|
|
219
|
+
const maxD = Math.max(this.decimals, other.decimals);
|
|
220
|
+
const v1 = this.decimals < maxD ? this.value * (10n ** BigInt(maxD - this.decimals)) : this.value;
|
|
221
|
+
const v2 = other.decimals < maxD ? other.value * (10n ** BigInt(maxD - other.decimals)) : other.value;
|
|
222
|
+
return v1 > v2 ? 1 : v1 < v2 ? -1 : 0;
|
|
223
|
+
}
|
|
224
|
+
gt(val) {
|
|
225
|
+
return this.compare(val) === 1;
|
|
226
|
+
}
|
|
227
|
+
lt(val) {
|
|
228
|
+
return this.compare(val) === -1;
|
|
229
|
+
}
|
|
230
|
+
gte(val) {
|
|
231
|
+
return this.compare(val) >= 0;
|
|
232
|
+
}
|
|
233
|
+
lte(val) {
|
|
234
|
+
return this.compare(val) <= 0;
|
|
235
|
+
}
|
|
236
|
+
eq(val) {
|
|
237
|
+
return this.compare(val) === 0;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.BigintDecimal = BigintDecimal;
|
|
241
|
+
exports.default = BigintDecimal;
|
|
242
|
+
BigintDecimal.precision = 20;
|
|
243
|
+
;
|
|
244
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":";;;AAGA,MAAa,aAAa;IAOtB,YAAY,MAAkC,GAAG;QALzC,UAAK,GAAW,EAAE,CAAC;QACnB,aAAQ,GAAW,CAAC,CAAC;QAE7B,cAAS,GAAW,aAAa,CAAC,SAAS,CAAC;QAGxC,IAAI,GAAG,YAAY,aAAa,EAAE,CAAC;YAC/B,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAA;YACtB,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAA;YAC5B,OAAM;QACV,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CAAC,IAAI,GAAG,2BAA2B,CAAC,CAAA;QACvD,CAAC;QACD,MAAM,UAAU,GAAG,IAAI,CAAC,4BAA4B,CAAC,GAAG,CAAC,CAAC;QAC1D,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;QAEp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C,CAAC,CAAC;IACpC,CAAC;IAED,GAAG,CAAC,GAA+B;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,GAAG,CAAC,GAA+B;QAC/B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAClC,CAAC;IAED,EAAE,CAAC,GAA+B;QAC9B,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;;AAtRL,sCAuRC;AAEyB,gCAAO;AArRtB,uBAAS,GAAW,EAAE,AAAb,CAAa;AAmRhC,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createNumberPermutations = exports.createBigintPermutations = void 0;
|
|
4
|
+
const factorial = (n) => {
|
|
5
|
+
let result = 1n;
|
|
6
|
+
for (let i = 1n; i <= n; i++) {
|
|
7
|
+
result *= i;
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
const permutationsBigint = (n, r) => factorial(n) / factorial(n - r);
|
|
12
|
+
const createBigintPermutations = (size, momentumSize) => {
|
|
13
|
+
const permutations = [];
|
|
14
|
+
for (let i = 0n; i < momentumSize; i++) {
|
|
15
|
+
permutations.push(permutationsBigint(size - i - 1n, momentumSize - i - 1n));
|
|
16
|
+
}
|
|
17
|
+
return permutations;
|
|
18
|
+
};
|
|
19
|
+
exports.createBigintPermutations = createBigintPermutations;
|
|
20
|
+
const createNumberPermutations = (size, momentumSize) => {
|
|
21
|
+
return (0, exports.createBigintPermutations)(BigInt(size), BigInt(momentumSize)).map(Number);
|
|
22
|
+
};
|
|
23
|
+
exports.createNumberPermutations = createNumberPermutations;
|
|
24
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../utils.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;IACpC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,MAAM,IAAI,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE,CAChD,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAG7B,MAAM,wBAAwB,GAAG,CAAC,IAAY,EAAE,YAAoB,EAAE,EAAE;IAC3E,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,YAAY,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,EAAE,YAAY,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;IAC/E,CAAC;IACD,OAAO,YAAY,CAAA;AACvB,CAAC,CAAA;AANY,QAAA,wBAAwB,4BAMpC;AAEM,MAAM,wBAAwB,GAAG,CAAC,IAAY,EAAE,YAAoB,EAAE,EAAE;IAC3E,OAAO,IAAA,gCAAwB,EAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnF,CAAC,CAAA;AAFY,QAAA,wBAAwB,4BAEpC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type SafeValues = string | number | bigint;
|
|
2
|
+
export declare class BigintDecimal {
|
|
3
|
+
private value;
|
|
4
|
+
private decimals;
|
|
5
|
+
static precision: number;
|
|
6
|
+
precision: number;
|
|
7
|
+
constructor(val?: SafeValues | BigintDecimal);
|
|
8
|
+
static setPrecisionGlobal(newPrecision: number): void;
|
|
9
|
+
setPrecision(newPrecision: number): void;
|
|
10
|
+
private trim;
|
|
11
|
+
private toSafeValue;
|
|
12
|
+
private getNumberIFoForBigintDecimal;
|
|
13
|
+
toString(): string;
|
|
14
|
+
toJSON(): string;
|
|
15
|
+
isBigintDecimal(val: unknown): boolean;
|
|
16
|
+
plus(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
17
|
+
minus(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
18
|
+
times(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
19
|
+
round(toDecimals: number): BigintDecimal;
|
|
20
|
+
div(val: SafeValues | BigintDecimal): BigintDecimal;
|
|
21
|
+
private compare;
|
|
22
|
+
gt(val: SafeValues | BigintDecimal): boolean;
|
|
23
|
+
lt(val: SafeValues | BigintDecimal): boolean;
|
|
24
|
+
gte(val: SafeValues | BigintDecimal): boolean;
|
|
25
|
+
lte(val: SafeValues | BigintDecimal): boolean;
|
|
26
|
+
eq(val: SafeValues | BigintDecimal): boolean;
|
|
27
|
+
}
|
|
28
|
+
export { BigintDecimal as default };
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
export class BigintDecimal {
|
|
2
|
+
constructor(val = "0") {
|
|
3
|
+
this.value = 0n;
|
|
4
|
+
this.decimals = 0;
|
|
5
|
+
this.precision = BigintDecimal.precision;
|
|
6
|
+
if (val instanceof BigintDecimal) {
|
|
7
|
+
this.value = val.value;
|
|
8
|
+
this.decimals = val.decimals;
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (!this.isBigintDecimal(val)) {
|
|
12
|
+
throw new Error(`"${val}" is not safe parameter .`);
|
|
13
|
+
}
|
|
14
|
+
const numberInfo = this.getNumberIFoForBigintDecimal(val);
|
|
15
|
+
this.value = numberInfo.value;
|
|
16
|
+
this.decimals = numberInfo.decimals;
|
|
17
|
+
this.trim();
|
|
18
|
+
}
|
|
19
|
+
static setPrecisionGlobal(newPrecision) {
|
|
20
|
+
this.precision = newPrecision;
|
|
21
|
+
}
|
|
22
|
+
setPrecision(newPrecision) {
|
|
23
|
+
this.precision = newPrecision;
|
|
24
|
+
}
|
|
25
|
+
trim() {
|
|
26
|
+
if (this.value === 0n) {
|
|
27
|
+
this.value = 0n;
|
|
28
|
+
this.decimals = 0;
|
|
29
|
+
}
|
|
30
|
+
// While we have decimals AND the last digit is 0
|
|
31
|
+
while (this.decimals > 0 && this.value !== 0n && this.value % 10n === 0n) {
|
|
32
|
+
this.value /= 10n; // Shrink the number
|
|
33
|
+
this.decimals--; // Shrink the scale
|
|
34
|
+
}
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
toSafeValue(val) {
|
|
38
|
+
if (typeof val === "string") {
|
|
39
|
+
return val.trim();
|
|
40
|
+
}
|
|
41
|
+
return `${val}`;
|
|
42
|
+
}
|
|
43
|
+
getNumberIFoForBigintDecimal(val) {
|
|
44
|
+
const safeVal = this.toSafeValue(val);
|
|
45
|
+
let isDot = false, value = 0n, decimals = 0, sign = 1n;
|
|
46
|
+
for (let i = 0; i < safeVal.length; i++) {
|
|
47
|
+
const code = safeVal.charCodeAt(i);
|
|
48
|
+
if (code >= 48 && code <= 57) {
|
|
49
|
+
value = (value * 10n) + BigInt(code - 48);
|
|
50
|
+
if (isDot)
|
|
51
|
+
decimals++;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (code === 46 && !isDot) {
|
|
55
|
+
isDot = true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (i === 0 && (code === 45 || code === 43)) {
|
|
59
|
+
if (code === 45)
|
|
60
|
+
sign = -1n;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
// Scientific Notation Handle
|
|
64
|
+
if (code === 101 || code === 69) {
|
|
65
|
+
const exp = parseInt(safeVal.slice(i + 1), 10);
|
|
66
|
+
if (isNaN(exp))
|
|
67
|
+
throw new Error("Invalid exponent");
|
|
68
|
+
decimals -= exp;
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`Unexpected character: ${String.fromCharCode(code)}`);
|
|
72
|
+
}
|
|
73
|
+
// Handle negative decimals (large integers)
|
|
74
|
+
if (decimals < 0) {
|
|
75
|
+
value = value * (10n ** BigInt(-decimals));
|
|
76
|
+
decimals = 0;
|
|
77
|
+
}
|
|
78
|
+
;
|
|
79
|
+
return { value: value * sign, decimals };
|
|
80
|
+
}
|
|
81
|
+
toString() {
|
|
82
|
+
// 1. Handle Integer case (Fastest exit, Zero Allocation overhead)
|
|
83
|
+
if (this.decimals === 0)
|
|
84
|
+
return this.value.toString();
|
|
85
|
+
// 2. Separate Sign and Magnitude
|
|
86
|
+
// Check sign using BigInt math (fastest)
|
|
87
|
+
const isNegative = this.value < 0n;
|
|
88
|
+
// Get the string of digits ONLY.
|
|
89
|
+
// isNegative ? -this.value : this.value -> creates a temporary BigInt
|
|
90
|
+
// .toString() -> creates the string of digits "123"
|
|
91
|
+
// This is cheaper than creating "-123" and slicing it.
|
|
92
|
+
const s = (isNegative ? -this.value : this.value).toString();
|
|
93
|
+
const len = s.length;
|
|
94
|
+
// 3. Logic for insertion vs padding
|
|
95
|
+
if (len > this.decimals) {
|
|
96
|
+
// Case A: 12345 (2 dec) -> 123.45
|
|
97
|
+
// splitIndex is purely digits before the dot
|
|
98
|
+
const splitIndex = len - this.decimals;
|
|
99
|
+
// Build the absolute decimal string
|
|
100
|
+
const result = s.substring(0, splitIndex) + "." + s.substring(splitIndex);
|
|
101
|
+
// Add sign if needed
|
|
102
|
+
return isNegative ? "-" + result : result;
|
|
103
|
+
}
|
|
104
|
+
// Case B: 5 (2 dec) -> 0.05
|
|
105
|
+
// Calculate zeros needed: 2 decimals - length 1 = 1 zero
|
|
106
|
+
const zerosNeeded = this.decimals - len;
|
|
107
|
+
// Build the result directly
|
|
108
|
+
// "0." + "0" + "5"
|
|
109
|
+
return (isNegative ? "-0." : "0.") + "0".repeat(zerosNeeded) + s;
|
|
110
|
+
}
|
|
111
|
+
toJSON() {
|
|
112
|
+
return this.toString();
|
|
113
|
+
}
|
|
114
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
115
|
+
return this.toString();
|
|
116
|
+
}
|
|
117
|
+
isBigintDecimal(val) {
|
|
118
|
+
const s = this.toSafeValue(val);
|
|
119
|
+
if (s.length === 0)
|
|
120
|
+
return false;
|
|
121
|
+
let dot = false, e = false;
|
|
122
|
+
for (let i = 0; i < s.length; i++) {
|
|
123
|
+
const c = s.charCodeAt(i);
|
|
124
|
+
if (c >= 48 && c <= 57)
|
|
125
|
+
continue; // 0-9
|
|
126
|
+
if (c === 46 && !dot && !e) {
|
|
127
|
+
dot = true;
|
|
128
|
+
continue;
|
|
129
|
+
} // .
|
|
130
|
+
if (i === 0 && (c === 45 || c === 43))
|
|
131
|
+
continue; // +/- at start
|
|
132
|
+
if ((c === 101 || c === 69) && !e) { // e/E
|
|
133
|
+
e = true;
|
|
134
|
+
const next = s.charCodeAt(i + 1);
|
|
135
|
+
if (next === 45 || next === 43)
|
|
136
|
+
i++; // Skip sign after 'e'
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
plus(val) {
|
|
144
|
+
const plusValue = new BigintDecimal(val);
|
|
145
|
+
// 1. Determine which number has more decimals
|
|
146
|
+
const d1 = this.decimals;
|
|
147
|
+
const d2 = plusValue.decimals;
|
|
148
|
+
const newDecimal = d1 > d2 ? d1 : d2;
|
|
149
|
+
// 2. Align values (Common Denominator)
|
|
150
|
+
// If 'this' has fewer decimals, scale it up.
|
|
151
|
+
const v1 = d1 < newDecimal ? this.value * (10n ** BigInt(newDecimal - d1)) : this.value;
|
|
152
|
+
// If 'plusValue' has fewer decimals, scale it up.
|
|
153
|
+
const v2 = d2 < newDecimal ? plusValue.value * (10n ** BigInt(newDecimal - d2)) : plusValue.value;
|
|
154
|
+
// 3. Now add and return
|
|
155
|
+
const result = new BigintDecimal("0");
|
|
156
|
+
result.value = v1 + v2;
|
|
157
|
+
result.decimals = newDecimal;
|
|
158
|
+
return result.trim();
|
|
159
|
+
}
|
|
160
|
+
minus(val) {
|
|
161
|
+
const other = new BigintDecimal(val);
|
|
162
|
+
const d1 = this.decimals;
|
|
163
|
+
const d2 = other.decimals;
|
|
164
|
+
const newDecimal = d1 > d2 ? d1 : d2;
|
|
165
|
+
// Scale values to the same precision
|
|
166
|
+
const v1 = d1 < newDecimal ? this.value * (10n ** BigInt(newDecimal - d1)) : this.value;
|
|
167
|
+
const v2 = d2 < newDecimal ? other.value * (10n ** BigInt(newDecimal - d2)) : other.value;
|
|
168
|
+
const result = new BigintDecimal("0"); // Fastest instantiation
|
|
169
|
+
result.value = v1 - v2;
|
|
170
|
+
result.decimals = newDecimal;
|
|
171
|
+
return result.trim();
|
|
172
|
+
}
|
|
173
|
+
times(val) {
|
|
174
|
+
const other = new BigintDecimal(val);
|
|
175
|
+
const result = new BigintDecimal("0");
|
|
176
|
+
// Multiplication: (A * 10^d1) * (B * 10^d2) = (A * B) * 10^(d1 + d2)
|
|
177
|
+
result.value = this.value * other.value;
|
|
178
|
+
result.decimals = this.decimals + other.decimals;
|
|
179
|
+
return result.trim();
|
|
180
|
+
}
|
|
181
|
+
round(toDecimals) {
|
|
182
|
+
const result = new BigintDecimal("0");
|
|
183
|
+
const diff = this.decimals - toDecimals;
|
|
184
|
+
if (diff <= 0)
|
|
185
|
+
return this; // Already more precise than requested
|
|
186
|
+
const power = 10n ** BigInt(diff);
|
|
187
|
+
const half = power / 2n;
|
|
188
|
+
// Standard rounding: add half of the divisor before dividing
|
|
189
|
+
// This turns 0.999... into 1.0
|
|
190
|
+
const roundedValue = (this.value >= 0n) ? (this.value + half) / power : (this.value - half) / power;
|
|
191
|
+
result.value = roundedValue;
|
|
192
|
+
result.decimals = toDecimals;
|
|
193
|
+
return result.trim();
|
|
194
|
+
}
|
|
195
|
+
div(val) {
|
|
196
|
+
const other = val instanceof BigintDecimal ? val : new BigintDecimal(val);
|
|
197
|
+
if (other.value === 0n)
|
|
198
|
+
throw new Error("Division by zero");
|
|
199
|
+
let v1 = this.value;
|
|
200
|
+
const v2 = other.value;
|
|
201
|
+
// 1. Scale up once to your "Max Allowed" precision
|
|
202
|
+
const shift = this.precision + other.decimals - this.decimals;
|
|
203
|
+
if (shift > 0)
|
|
204
|
+
v1 *= 10n ** BigInt(shift);
|
|
205
|
+
else if (shift < 0)
|
|
206
|
+
v1 /= 10n ** BigInt(-shift);
|
|
207
|
+
const result = new BigintDecimal("0");
|
|
208
|
+
result.value = v1 / v2;
|
|
209
|
+
result.decimals = this.precision;
|
|
210
|
+
// 2. Immediately trim it!
|
|
211
|
+
// This is where 0.500000... becomes 0.5 automatically.
|
|
212
|
+
return result.trim();
|
|
213
|
+
}
|
|
214
|
+
compare(val) {
|
|
215
|
+
const other = val instanceof BigintDecimal ? val : new BigintDecimal(val);
|
|
216
|
+
const maxD = Math.max(this.decimals, other.decimals);
|
|
217
|
+
const v1 = this.decimals < maxD ? this.value * (10n ** BigInt(maxD - this.decimals)) : this.value;
|
|
218
|
+
const v2 = other.decimals < maxD ? other.value * (10n ** BigInt(maxD - other.decimals)) : other.value;
|
|
219
|
+
return v1 > v2 ? 1 : v1 < v2 ? -1 : 0;
|
|
220
|
+
}
|
|
221
|
+
gt(val) {
|
|
222
|
+
return this.compare(val) === 1;
|
|
223
|
+
}
|
|
224
|
+
lt(val) {
|
|
225
|
+
return this.compare(val) === -1;
|
|
226
|
+
}
|
|
227
|
+
gte(val) {
|
|
228
|
+
return this.compare(val) >= 0;
|
|
229
|
+
}
|
|
230
|
+
lte(val) {
|
|
231
|
+
return this.compare(val) <= 0;
|
|
232
|
+
}
|
|
233
|
+
eq(val) {
|
|
234
|
+
return this.compare(val) === 0;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
BigintDecimal.precision = 20;
|
|
238
|
+
;
|
|
239
|
+
export { BigintDecimal as default };
|
|
240
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../index.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,aAAa;IAOtB,YAAY,MAAkC,GAAG;QALzC,UAAK,GAAW,EAAE,CAAC;QACnB,aAAQ,GAAW,CAAC,CAAC;QAE7B,cAAS,GAAW,aAAa,CAAC,SAAS,CAAC;QAGxC,IAAI,GAAG,YAAY,aAAa,EAAE,CAAC;YAC/B,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,CAAA;YACtB,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAA;YAC5B,OAAM;QACV,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CAAC,IAAI,GAAG,2BAA2B,CAAC,CAAA;QACvD,CAAC;QACD,MAAM,UAAU,GAAG,IAAI,CAAC,4BAA4B,CAAC,GAAG,CAAC,CAAC;QAC1D,IAAI,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC;QAC9B,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;QAEpC,IAAI,CAAC,IAAI,EAAE,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,kBAAkB,CAAC,YAAoB;QAC1C,IAAI,CAAC,SAAS,GAAG,YAAY,CAAC;IAClC,CAAC;IAED,YAAY,CAAC,YAAoB;QAC7B,IAAI,CAAC,SAAS,GAAG,YAAY,CAAC;IAClC,CAAC;IAGO,IAAI;QACR,IAAI,IAAI,CAAC,KAAK,KAAK,EAAE,EAAE,CAAC;YACpB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;YACf,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QACt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CAAa;AAmRhC,CAAC;AAEF,OAAO,EAAE,aAAa,IAAI,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const factorial = (n) => {
|
|
2
|
+
let result = 1n;
|
|
3
|
+
for (let i = 1n; i <= n; i++) {
|
|
4
|
+
result *= i;
|
|
5
|
+
}
|
|
6
|
+
return result;
|
|
7
|
+
};
|
|
8
|
+
const permutationsBigint = (n, r) => factorial(n) / factorial(n - r);
|
|
9
|
+
export const createBigintPermutations = (size, momentumSize) => {
|
|
10
|
+
const permutations = [];
|
|
11
|
+
for (let i = 0n; i < momentumSize; i++) {
|
|
12
|
+
permutations.push(permutationsBigint(size - i - 1n, momentumSize - i - 1n));
|
|
13
|
+
}
|
|
14
|
+
return permutations;
|
|
15
|
+
};
|
|
16
|
+
export const createNumberPermutations = (size, momentumSize) => {
|
|
17
|
+
return createBigintPermutations(BigInt(size), BigInt(momentumSize)).map(Number);
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../utils.ts"],"names":[],"mappings":"AAAA,MAAM,SAAS,GAAG,CAAC,CAAS,EAAU,EAAE;IACpC,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,MAAM,IAAI,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE,CAChD,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;AAGpC,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,IAAY,EAAE,YAAoB,EAAE,EAAE;IAC3E,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,KAAK,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,YAAY,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,YAAY,CAAC,IAAI,CAAC,kBAAkB,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,EAAE,YAAY,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;IAC/E,CAAC;IACD,OAAO,YAAY,CAAA;AACvB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,IAAY,EAAE,YAAoB,EAAE,EAAE;IAC3E,OAAO,wBAAwB,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnF,CAAC,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bigint-decimal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-dependency, arbitrary-precision decimal math library using native BigInt for safe financial and crypto calculations.",
|
|
5
|
+
"main": "./dist/global/index.js",
|
|
6
|
+
"author": "soso tsertsvadze",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"dev": "tsc -w --project tsconfig.common.json & tsc -w --project tsconfig.global.json & node --watch --watch-preserve-output ./dist/common/index.js & wait",
|
|
11
|
+
"build": "tsc --project tsconfig.node.json && tsc --project tsconfig.global.json"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public",
|
|
15
|
+
"registry": "https://registry.npmjs.org/"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Generalsimus/bigint-decimal.git"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/global/index.js",
|
|
24
|
+
"require": "./dist/common/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"bigint",
|
|
29
|
+
"decimal",
|
|
30
|
+
"math",
|
|
31
|
+
"number",
|
|
32
|
+
"finance",
|
|
33
|
+
"crypto",
|
|
34
|
+
"precision",
|
|
35
|
+
"arbitrary-precision",
|
|
36
|
+
"typescript",
|
|
37
|
+
"bignumber"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/jest": "^30.0.0",
|
|
41
|
+
"jest": "^30.3.0",
|
|
42
|
+
"ts-jest": "^29.4.6"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// --- Strong Type Safety Flags ---
|
|
4
|
+
"strict": true,
|
|
5
|
+
"noUncheckedIndexedAccess": true,
|
|
6
|
+
"exactOptionalPropertyTypes": true,
|
|
7
|
+
"noImplicitOverride": true,
|
|
8
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
9
|
+
// --- Additional Code Quality Checks ---
|
|
10
|
+
"noUnusedLocals": true,
|
|
11
|
+
"noUnusedParameters": true,
|
|
12
|
+
"noImplicitReturns": true,
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
// --- Original Configuration (Updated) ---
|
|
16
|
+
"moduleResolution": "node",
|
|
17
|
+
"removeComments": false,
|
|
18
|
+
"preserveConstEnums": true,
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"target": "ES2020",
|
|
22
|
+
"isolatedModules": true,
|
|
23
|
+
"skipLibCheck": true,
|
|
24
|
+
"rootDir": "./",
|
|
25
|
+
"declarationDir": "./dist",
|
|
26
|
+
"outDir": "./dist"
|
|
27
|
+
},
|
|
28
|
+
"exclude": [
|
|
29
|
+
"./dist/",
|
|
30
|
+
"node_modules",
|
|
31
|
+
"./test"
|
|
32
|
+
]
|
|
33
|
+
}
|