@obinexusmk2/hypernum 0.1.0 → 0.1.1
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 -21
- package/README.md +355 -256
- package/dist/index.cjs +4 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +2 -2
- package/dist/index.umd.js.map +1 -1
- package/dist/types/{config → src/config}/config-loader.d.ts +0 -0
- package/dist/types/src/config/config-loader.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-parser.d.ts +0 -0
- package/dist/types/src/config/config-parser.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-resolver.d.ts +0 -0
- package/dist/types/src/config/config-resolver.d.ts.map +1 -0
- package/dist/types/{config → src/config}/config-source.d.ts +0 -0
- package/dist/types/src/config/config-source.d.ts.map +1 -0
- package/dist/types/{config → src/config}/index.d.ts +0 -0
- package/dist/types/src/config/index.d.ts.map +1 -0
- package/dist/types/{core → src/core}/common.d.ts +0 -0
- package/dist/types/src/core/common.d.ts.map +1 -0
- package/dist/types/{core → src/core}/config.d.ts +0 -0
- package/dist/types/src/core/config.d.ts.map +1 -0
- package/dist/types/{core → src/core}/constants.d.ts +0 -0
- package/dist/types/src/core/constants.d.ts.map +1 -0
- package/dist/types/{core → src/core}/errors.d.ts +0 -0
- package/dist/types/src/core/errors.d.ts.map +1 -0
- package/dist/types/{core → src/core}/hypernum.d.ts +0 -0
- package/dist/types/src/core/hypernum.d.ts.map +1 -0
- package/dist/types/{core → src/core}/index.d.ts +0 -0
- package/dist/types/src/core/index.d.ts.map +1 -0
- package/dist/types/{index.d.ts → src/index.d.ts} +1 -1
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/arithmetic.d.ts +0 -0
- package/dist/types/src/operations/arithmetic.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/bitwise.d.ts +0 -0
- package/dist/types/src/operations/bitwise.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/comparison.d.ts +0 -0
- package/dist/types/src/operations/comparison.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/conversion.d.ts +0 -0
- package/dist/types/src/operations/conversion.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/factorial.d.ts +0 -0
- package/dist/types/src/operations/factorial.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/index.d.ts +0 -0
- package/dist/types/src/operations/index.d.ts.map +1 -0
- package/dist/types/{operations → src/operations}/power.d.ts +0 -0
- package/dist/types/src/operations/power.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/Heap.d.ts +0 -0
- package/dist/types/src/storage/Heap.d.ts.map +1 -0
- package/dist/types/{storage → src/storage}/index.d.ts +0 -0
- package/dist/types/src/storage/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/ackermann.d.ts +0 -0
- package/dist/types/src/structures/ackermann.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/big-array.d.ts +0 -0
- package/dist/types/src/structures/big-array.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/index.d.ts +0 -0
- package/dist/types/src/structures/index.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/number-tree.d.ts +0 -0
- package/dist/types/src/structures/number-tree.d.ts.map +1 -0
- package/dist/types/{structures → src/structures}/power-tower.d.ts +0 -0
- package/dist/types/src/structures/power-tower.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/formatting.d.ts +0 -0
- package/dist/types/src/utils/formatting.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/index.d.ts +0 -0
- package/dist/types/src/utils/index.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/parser.d.ts +0 -0
- package/dist/types/src/utils/parser.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/precision.d.ts +0 -0
- package/dist/types/src/utils/precision.d.ts.map +1 -0
- package/dist/types/{utils → src/utils}/validation.d.ts +0 -0
- package/dist/types/src/utils/validation.d.ts.map +1 -0
- package/package.json +169 -164
- package/rollup.config.js +163 -161
- package/src/cli/hypernum.js +272 -0
- package/src/config/config-loader.ts +0 -0
- package/src/config/config-parser.ts +160 -160
- package/src/config/config-resolver.ts +0 -0
- package/src/config/config-source.ts +0 -0
- package/src/config/index.ts +0 -0
- package/src/core/common.ts +184 -184
- package/src/core/config.ts +392 -392
- package/src/core/constants.ts +101 -101
- package/src/core/errors.ts +202 -202
- package/src/core/hypernum.ts +240 -240
- package/src/core/index.ts +4 -4
- package/src/index.ts +179 -182
- package/src/operations/arithmetic.ts +332 -332
- package/src/operations/bitwise.ts +366 -366
- package/src/operations/comparison.ts +271 -271
- package/src/operations/conversion.ts +399 -399
- package/src/operations/factorial.ts +278 -278
- package/src/operations/index.ts +4 -4
- package/src/operations/power.ts +315 -315
- package/src/storage/Heap.ts +237 -237
- package/src/storage/index.ts +0 -0
- package/src/structures/ackermann.ts +232 -232
- package/src/structures/big-array.ts +305 -305
- package/src/structures/index.ts +3 -3
- package/src/structures/number-tree.ts +403 -403
- package/src/structures/power-tower.ts +277 -277
- package/src/types/common.d.ts +356 -356
- package/src/types/core.d.ts +160 -160
- package/src/types/index.d.ts +1 -1
- package/src/utils/formatting.ts +245 -245
- package/src/utils/index.ts +4 -4
- package/src/utils/parser.ts +244 -244
- package/src/utils/precision.ts +216 -216
- package/src/utils/validation.ts +182 -182
- package/tsconfig.json +83 -83
- package/dist/types/config/config-loader.d.ts.map +0 -1
- package/dist/types/config/config-parser.d.ts.map +0 -1
- package/dist/types/config/config-resolver.d.ts.map +0 -1
- package/dist/types/config/config-source.d.ts.map +0 -1
- package/dist/types/config/index.d.ts.map +0 -1
- package/dist/types/core/common.d.ts.map +0 -1
- package/dist/types/core/config.d.ts.map +0 -1
- package/dist/types/core/constants.d.ts.map +0 -1
- package/dist/types/core/errors.d.ts.map +0 -1
- package/dist/types/core/hypernum.d.ts.map +0 -1
- package/dist/types/core/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/operations/arithmetic.d.ts.map +0 -1
- package/dist/types/operations/bitwise.d.ts.map +0 -1
- package/dist/types/operations/comparison.d.ts.map +0 -1
- package/dist/types/operations/conversion.d.ts.map +0 -1
- package/dist/types/operations/factorial.d.ts.map +0 -1
- package/dist/types/operations/index.d.ts.map +0 -1
- package/dist/types/operations/power.d.ts.map +0 -1
- package/dist/types/storage/Heap.d.ts.map +0 -1
- package/dist/types/storage/index.d.ts.map +0 -1
- package/dist/types/structures/ackermann.d.ts.map +0 -1
- package/dist/types/structures/big-array.d.ts.map +0 -1
- package/dist/types/structures/index.d.ts.map +0 -1
- package/dist/types/structures/number-tree.d.ts.map +0 -1
- package/dist/types/structures/power-tower.d.ts.map +0 -1
- package/dist/types/utils/formatting.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/parser.d.ts.map +0 -1
- package/dist/types/utils/precision.d.ts.map +0 -1
- package/dist/types/utils/validation.d.ts.map +0 -1
|
@@ -1,272 +1,272 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Comparison operations module for Hypernum library
|
|
3
|
-
* Provides functions for comparing large numbers with precision support
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
toBigInt,
|
|
8
|
-
ValidationError,
|
|
9
|
-
|
|
10
|
-
} from '../utils/validation';
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
RoundingMode,
|
|
14
|
-
normalizePrecision,
|
|
15
|
-
} from '../utils/precision';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Options for comparison operations
|
|
19
|
-
*/
|
|
20
|
-
export interface ComparisonOptions {
|
|
21
|
-
precision?: number;
|
|
22
|
-
roundingMode?: RoundingMode;
|
|
23
|
-
tolerance?: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const DEFAULT_OPTIONS: Required<ComparisonOptions> = {
|
|
27
|
-
precision: 0,
|
|
28
|
-
roundingMode: RoundingMode.HALF_EVEN,
|
|
29
|
-
tolerance: 0
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Result type for comparison operations
|
|
34
|
-
* -1: first value is less than second value
|
|
35
|
-
* 0: values are equal
|
|
36
|
-
* 1: first value is greater than second value
|
|
37
|
-
*/
|
|
38
|
-
export type ComparisonResult = -1 | 0 | 1;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Compares two numbers with optional precision
|
|
42
|
-
*/
|
|
43
|
-
export function compare(
|
|
44
|
-
a: bigint | string | number,
|
|
45
|
-
b: bigint | string | number,
|
|
46
|
-
options: ComparisonOptions = {}
|
|
47
|
-
): ComparisonResult {
|
|
48
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
49
|
-
const bigA = toBigInt(a);
|
|
50
|
-
const bigB = toBigInt(b);
|
|
51
|
-
|
|
52
|
-
if (opts.precision === 0 && opts.tolerance === 0) {
|
|
53
|
-
if (bigA < bigB) return -1;
|
|
54
|
-
if (bigA > bigB) return 1;
|
|
55
|
-
return 0;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const [scaledA, scaledB] = normalizePrecision(bigA, bigB, opts.precision, opts.precision);
|
|
59
|
-
|
|
60
|
-
if (opts.tolerance > 0) {
|
|
61
|
-
const diff = scaledA - scaledB;
|
|
62
|
-
const toleranceValue = BigInt(10) ** BigInt(opts.tolerance);
|
|
63
|
-
|
|
64
|
-
if (diff < -toleranceValue) return -1;
|
|
65
|
-
if (diff > toleranceValue) return 1;
|
|
66
|
-
return 0;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (scaledA < scaledB) return -1;
|
|
70
|
-
if (scaledA > scaledB) return 1;
|
|
71
|
-
return 0;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Checks if two numbers are equal
|
|
76
|
-
*/
|
|
77
|
-
export function equals(
|
|
78
|
-
a: bigint | string | number,
|
|
79
|
-
b: bigint | string | number,
|
|
80
|
-
options: ComparisonOptions = {}
|
|
81
|
-
): boolean {
|
|
82
|
-
return compare(a, b, options) === 0;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Checks if first number is less than second
|
|
87
|
-
*/
|
|
88
|
-
export function lessThan(
|
|
89
|
-
a: bigint | string | number,
|
|
90
|
-
b: bigint | string | number,
|
|
91
|
-
options: ComparisonOptions = {}
|
|
92
|
-
): boolean {
|
|
93
|
-
return compare(a, b, options) === -1;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Checks if first number is less than or equal to second
|
|
98
|
-
*/
|
|
99
|
-
export function lessThanOrEqual(
|
|
100
|
-
a: bigint | string | number,
|
|
101
|
-
b: bigint | string | number,
|
|
102
|
-
options: ComparisonOptions = {}
|
|
103
|
-
): boolean {
|
|
104
|
-
const result = compare(a, b, options);
|
|
105
|
-
return result === -1 || result === 0;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Checks if first number is greater than second
|
|
110
|
-
*/
|
|
111
|
-
export function greaterThan(
|
|
112
|
-
a: bigint | string | number,
|
|
113
|
-
b: bigint | string | number,
|
|
114
|
-
options: ComparisonOptions = {}
|
|
115
|
-
): boolean {
|
|
116
|
-
return compare(a, b, options) === 1;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Checks if first number is greater than or equal to second
|
|
121
|
-
*/
|
|
122
|
-
export function greaterThanOrEqual(
|
|
123
|
-
a: bigint | string | number,
|
|
124
|
-
b: bigint | string | number,
|
|
125
|
-
options: ComparisonOptions = {}
|
|
126
|
-
): boolean {
|
|
127
|
-
const result = compare(a, b, options);
|
|
128
|
-
return result === 1 || result === 0;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Checks if a number is between two others (inclusive)
|
|
133
|
-
*/
|
|
134
|
-
export function between(
|
|
135
|
-
value: bigint | string | number,
|
|
136
|
-
min: bigint | string | number,
|
|
137
|
-
max: bigint | string | number,
|
|
138
|
-
options: ComparisonOptions = {}
|
|
139
|
-
): boolean {
|
|
140
|
-
return greaterThanOrEqual(value, min, options) && lessThanOrEqual(value, max, options);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Finds the maximum value in an array of numbers
|
|
145
|
-
*/
|
|
146
|
-
export function max(
|
|
147
|
-
values: Array<bigint | string | number>,
|
|
148
|
-
options: ComparisonOptions = {}
|
|
149
|
-
): bigint {
|
|
150
|
-
if (values.length === 0) {
|
|
151
|
-
throw new ValidationError('Cannot find maximum of empty array');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return values.reduce<bigint>((max, current) => {
|
|
155
|
-
const bigMax = toBigInt(max);
|
|
156
|
-
const bigCurrent = toBigInt(current);
|
|
157
|
-
return greaterThan(bigCurrent, bigMax, options) ? bigCurrent : bigMax;
|
|
158
|
-
}, toBigInt(values[0]));
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Finds the minimum value in an array of numbers
|
|
163
|
-
*/
|
|
164
|
-
export function min(
|
|
165
|
-
values: Array<bigint | string | number>,
|
|
166
|
-
options: ComparisonOptions = {}
|
|
167
|
-
): bigint {
|
|
168
|
-
if (values.length === 0) {
|
|
169
|
-
throw new ValidationError('Cannot find minimum of empty array');
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return values.reduce<bigint>((min, current) => {
|
|
173
|
-
const bigMin = toBigInt(min);
|
|
174
|
-
const bigCurrent = toBigInt(current);
|
|
175
|
-
return lessThan(bigCurrent, bigMin, options) ? bigCurrent : bigMin;
|
|
176
|
-
}, toBigInt(values[0]));
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Clamps a value between minimum and maximum bounds
|
|
181
|
-
*/
|
|
182
|
-
export function clamp(
|
|
183
|
-
value: bigint | string | number,
|
|
184
|
-
min: bigint | string | number,
|
|
185
|
-
max: bigint | string | number,
|
|
186
|
-
options: ComparisonOptions = {}
|
|
187
|
-
): bigint {
|
|
188
|
-
const bigValue = toBigInt(value);
|
|
189
|
-
const bigMin = toBigInt(min);
|
|
190
|
-
const bigMax = toBigInt(max);
|
|
191
|
-
|
|
192
|
-
if (lessThan(bigMax, bigMin, options)) {
|
|
193
|
-
throw new ValidationError('Maximum bound must be greater than or equal to minimum bound');
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (lessThan(bigValue, bigMin, options)) return bigMin;
|
|
197
|
-
if (greaterThan(bigValue, bigMax, options)) return bigMax;
|
|
198
|
-
return bigValue;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Checks if all values in array are equal within tolerance
|
|
203
|
-
*/
|
|
204
|
-
export function allEqual(
|
|
205
|
-
values: Array<bigint | string | number>,
|
|
206
|
-
options: ComparisonOptions = {}
|
|
207
|
-
): boolean {
|
|
208
|
-
if (values.length <= 1) return true;
|
|
209
|
-
|
|
210
|
-
const first = toBigInt(values[0]);
|
|
211
|
-
return values.every(value => equals(value, first, options));
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Checks if values are in ascending order
|
|
216
|
-
*/
|
|
217
|
-
export function isAscending(
|
|
218
|
-
values: Array<bigint | string | number>,
|
|
219
|
-
options: ComparisonOptions = {}
|
|
220
|
-
): boolean {
|
|
221
|
-
if (values.length <= 1) return true;
|
|
222
|
-
|
|
223
|
-
for (let i = 1; i < values.length; i++) {
|
|
224
|
-
if (values[i] === undefined || values[i - 1] === undefined || !greaterThanOrEqual(values[i]!, values[i - 1]!, options)) {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return true;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Checks if values are in descending order
|
|
233
|
-
*/
|
|
234
|
-
export function isDescending(
|
|
235
|
-
values: Array<bigint | string | number>,
|
|
236
|
-
options: ComparisonOptions = {}
|
|
237
|
-
): boolean {
|
|
238
|
-
if (values.length <= 1) return true;
|
|
239
|
-
|
|
240
|
-
for (let i = 1; i < values.length; i++) {
|
|
241
|
-
if (values[i] === undefined || values[i - 1] === undefined || !lessThanOrEqual(values[i]!, values[i - 1]!, options)) {
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
return true;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Creates a comparator function for sorting
|
|
250
|
-
*/
|
|
251
|
-
export function createComparator(
|
|
252
|
-
options: ComparisonOptions = {}
|
|
253
|
-
): (a: bigint | string | number, b: bigint | string | number) => number {
|
|
254
|
-
return (a, b) => compare(a, b, options);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export default {
|
|
258
|
-
compare,
|
|
259
|
-
equals,
|
|
260
|
-
lessThan,
|
|
261
|
-
lessThanOrEqual,
|
|
262
|
-
greaterThan,
|
|
263
|
-
greaterThanOrEqual,
|
|
264
|
-
between,
|
|
265
|
-
max,
|
|
266
|
-
min,
|
|
267
|
-
clamp,
|
|
268
|
-
allEqual,
|
|
269
|
-
isAscending,
|
|
270
|
-
isDescending,
|
|
271
|
-
createComparator
|
|
1
|
+
/**
|
|
2
|
+
* Comparison operations module for Hypernum library
|
|
3
|
+
* Provides functions for comparing large numbers with precision support
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
toBigInt,
|
|
8
|
+
ValidationError,
|
|
9
|
+
|
|
10
|
+
} from '../utils/validation';
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
RoundingMode,
|
|
14
|
+
normalizePrecision,
|
|
15
|
+
} from '../utils/precision';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Options for comparison operations
|
|
19
|
+
*/
|
|
20
|
+
export interface ComparisonOptions {
|
|
21
|
+
precision?: number;
|
|
22
|
+
roundingMode?: RoundingMode;
|
|
23
|
+
tolerance?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const DEFAULT_OPTIONS: Required<ComparisonOptions> = {
|
|
27
|
+
precision: 0,
|
|
28
|
+
roundingMode: RoundingMode.HALF_EVEN,
|
|
29
|
+
tolerance: 0
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Result type for comparison operations
|
|
34
|
+
* -1: first value is less than second value
|
|
35
|
+
* 0: values are equal
|
|
36
|
+
* 1: first value is greater than second value
|
|
37
|
+
*/
|
|
38
|
+
export type ComparisonResult = -1 | 0 | 1;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Compares two numbers with optional precision
|
|
42
|
+
*/
|
|
43
|
+
export function compare(
|
|
44
|
+
a: bigint | string | number,
|
|
45
|
+
b: bigint | string | number,
|
|
46
|
+
options: ComparisonOptions = {}
|
|
47
|
+
): ComparisonResult {
|
|
48
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
49
|
+
const bigA = toBigInt(a);
|
|
50
|
+
const bigB = toBigInt(b);
|
|
51
|
+
|
|
52
|
+
if (opts.precision === 0 && opts.tolerance === 0) {
|
|
53
|
+
if (bigA < bigB) return -1;
|
|
54
|
+
if (bigA > bigB) return 1;
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const [scaledA, scaledB] = normalizePrecision(bigA, bigB, opts.precision, opts.precision);
|
|
59
|
+
|
|
60
|
+
if (opts.tolerance > 0) {
|
|
61
|
+
const diff = scaledA - scaledB;
|
|
62
|
+
const toleranceValue = BigInt(10) ** BigInt(opts.tolerance);
|
|
63
|
+
|
|
64
|
+
if (diff < -toleranceValue) return -1;
|
|
65
|
+
if (diff > toleranceValue) return 1;
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (scaledA < scaledB) return -1;
|
|
70
|
+
if (scaledA > scaledB) return 1;
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Checks if two numbers are equal
|
|
76
|
+
*/
|
|
77
|
+
export function equals(
|
|
78
|
+
a: bigint | string | number,
|
|
79
|
+
b: bigint | string | number,
|
|
80
|
+
options: ComparisonOptions = {}
|
|
81
|
+
): boolean {
|
|
82
|
+
return compare(a, b, options) === 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Checks if first number is less than second
|
|
87
|
+
*/
|
|
88
|
+
export function lessThan(
|
|
89
|
+
a: bigint | string | number,
|
|
90
|
+
b: bigint | string | number,
|
|
91
|
+
options: ComparisonOptions = {}
|
|
92
|
+
): boolean {
|
|
93
|
+
return compare(a, b, options) === -1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Checks if first number is less than or equal to second
|
|
98
|
+
*/
|
|
99
|
+
export function lessThanOrEqual(
|
|
100
|
+
a: bigint | string | number,
|
|
101
|
+
b: bigint | string | number,
|
|
102
|
+
options: ComparisonOptions = {}
|
|
103
|
+
): boolean {
|
|
104
|
+
const result = compare(a, b, options);
|
|
105
|
+
return result === -1 || result === 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Checks if first number is greater than second
|
|
110
|
+
*/
|
|
111
|
+
export function greaterThan(
|
|
112
|
+
a: bigint | string | number,
|
|
113
|
+
b: bigint | string | number,
|
|
114
|
+
options: ComparisonOptions = {}
|
|
115
|
+
): boolean {
|
|
116
|
+
return compare(a, b, options) === 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Checks if first number is greater than or equal to second
|
|
121
|
+
*/
|
|
122
|
+
export function greaterThanOrEqual(
|
|
123
|
+
a: bigint | string | number,
|
|
124
|
+
b: bigint | string | number,
|
|
125
|
+
options: ComparisonOptions = {}
|
|
126
|
+
): boolean {
|
|
127
|
+
const result = compare(a, b, options);
|
|
128
|
+
return result === 1 || result === 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Checks if a number is between two others (inclusive)
|
|
133
|
+
*/
|
|
134
|
+
export function between(
|
|
135
|
+
value: bigint | string | number,
|
|
136
|
+
min: bigint | string | number,
|
|
137
|
+
max: bigint | string | number,
|
|
138
|
+
options: ComparisonOptions = {}
|
|
139
|
+
): boolean {
|
|
140
|
+
return greaterThanOrEqual(value, min, options) && lessThanOrEqual(value, max, options);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Finds the maximum value in an array of numbers
|
|
145
|
+
*/
|
|
146
|
+
export function max(
|
|
147
|
+
values: Array<bigint | string | number>,
|
|
148
|
+
options: ComparisonOptions = {}
|
|
149
|
+
): bigint {
|
|
150
|
+
if (values.length === 0) {
|
|
151
|
+
throw new ValidationError('Cannot find maximum of empty array');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return values.reduce<bigint>((max, current) => {
|
|
155
|
+
const bigMax = toBigInt(max);
|
|
156
|
+
const bigCurrent = toBigInt(current);
|
|
157
|
+
return greaterThan(bigCurrent, bigMax, options) ? bigCurrent : bigMax;
|
|
158
|
+
}, toBigInt(values[0]));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Finds the minimum value in an array of numbers
|
|
163
|
+
*/
|
|
164
|
+
export function min(
|
|
165
|
+
values: Array<bigint | string | number>,
|
|
166
|
+
options: ComparisonOptions = {}
|
|
167
|
+
): bigint {
|
|
168
|
+
if (values.length === 0) {
|
|
169
|
+
throw new ValidationError('Cannot find minimum of empty array');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return values.reduce<bigint>((min, current) => {
|
|
173
|
+
const bigMin = toBigInt(min);
|
|
174
|
+
const bigCurrent = toBigInt(current);
|
|
175
|
+
return lessThan(bigCurrent, bigMin, options) ? bigCurrent : bigMin;
|
|
176
|
+
}, toBigInt(values[0]));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Clamps a value between minimum and maximum bounds
|
|
181
|
+
*/
|
|
182
|
+
export function clamp(
|
|
183
|
+
value: bigint | string | number,
|
|
184
|
+
min: bigint | string | number,
|
|
185
|
+
max: bigint | string | number,
|
|
186
|
+
options: ComparisonOptions = {}
|
|
187
|
+
): bigint {
|
|
188
|
+
const bigValue = toBigInt(value);
|
|
189
|
+
const bigMin = toBigInt(min);
|
|
190
|
+
const bigMax = toBigInt(max);
|
|
191
|
+
|
|
192
|
+
if (lessThan(bigMax, bigMin, options)) {
|
|
193
|
+
throw new ValidationError('Maximum bound must be greater than or equal to minimum bound');
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (lessThan(bigValue, bigMin, options)) return bigMin;
|
|
197
|
+
if (greaterThan(bigValue, bigMax, options)) return bigMax;
|
|
198
|
+
return bigValue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Checks if all values in array are equal within tolerance
|
|
203
|
+
*/
|
|
204
|
+
export function allEqual(
|
|
205
|
+
values: Array<bigint | string | number>,
|
|
206
|
+
options: ComparisonOptions = {}
|
|
207
|
+
): boolean {
|
|
208
|
+
if (values.length <= 1) return true;
|
|
209
|
+
|
|
210
|
+
const first = toBigInt(values[0]);
|
|
211
|
+
return values.every(value => equals(value, first, options));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Checks if values are in ascending order
|
|
216
|
+
*/
|
|
217
|
+
export function isAscending(
|
|
218
|
+
values: Array<bigint | string | number>,
|
|
219
|
+
options: ComparisonOptions = {}
|
|
220
|
+
): boolean {
|
|
221
|
+
if (values.length <= 1) return true;
|
|
222
|
+
|
|
223
|
+
for (let i = 1; i < values.length; i++) {
|
|
224
|
+
if (values[i] === undefined || values[i - 1] === undefined || !greaterThanOrEqual(values[i]!, values[i - 1]!, options)) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Checks if values are in descending order
|
|
233
|
+
*/
|
|
234
|
+
export function isDescending(
|
|
235
|
+
values: Array<bigint | string | number>,
|
|
236
|
+
options: ComparisonOptions = {}
|
|
237
|
+
): boolean {
|
|
238
|
+
if (values.length <= 1) return true;
|
|
239
|
+
|
|
240
|
+
for (let i = 1; i < values.length; i++) {
|
|
241
|
+
if (values[i] === undefined || values[i - 1] === undefined || !lessThanOrEqual(values[i]!, values[i - 1]!, options)) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Creates a comparator function for sorting
|
|
250
|
+
*/
|
|
251
|
+
export function createComparator(
|
|
252
|
+
options: ComparisonOptions = {}
|
|
253
|
+
): (a: bigint | string | number, b: bigint | string | number) => number {
|
|
254
|
+
return (a, b) => compare(a, b, options);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export default {
|
|
258
|
+
compare,
|
|
259
|
+
equals,
|
|
260
|
+
lessThan,
|
|
261
|
+
lessThanOrEqual,
|
|
262
|
+
greaterThan,
|
|
263
|
+
greaterThanOrEqual,
|
|
264
|
+
between,
|
|
265
|
+
max,
|
|
266
|
+
min,
|
|
267
|
+
clamp,
|
|
268
|
+
allEqual,
|
|
269
|
+
isAscending,
|
|
270
|
+
isDescending,
|
|
271
|
+
createComparator
|
|
272
272
|
};
|