es-toolkit 1.50.0-dev.1991 → 1.50.0-dev.1992
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/bigint.d.ts +1 -0
- package/bigint.js +1 -0
- package/dist/_internal/bigIntRangeLength.js +19 -0
- package/dist/_internal/bigIntRangeLength.mjs +19 -0
- package/dist/bigint/clamp.d.mts +37 -0
- package/dist/bigint/clamp.d.ts +37 -0
- package/dist/bigint/clamp.js +16 -0
- package/dist/bigint/clamp.mjs +16 -0
- package/dist/bigint/inRange.d.mts +37 -0
- package/dist/bigint/inRange.d.ts +37 -0
- package/dist/bigint/inRange.js +20 -0
- package/dist/bigint/inRange.mjs +20 -0
- package/dist/bigint/index.d.mts +14 -0
- package/dist/bigint/index.d.ts +14 -0
- package/dist/bigint/index.js +27 -0
- package/dist/bigint/index.mjs +14 -0
- package/dist/bigint/max.d.mts +21 -0
- package/dist/bigint/max.d.ts +21 -0
- package/dist/bigint/max.js +26 -0
- package/dist/bigint/max.mjs +26 -0
- package/dist/bigint/maxBy.d.mts +21 -0
- package/dist/bigint/maxBy.d.ts +21 -0
- package/dist/bigint/maxBy.js +34 -0
- package/dist/bigint/maxBy.mjs +34 -0
- package/dist/bigint/median.d.mts +22 -0
- package/dist/bigint/median.d.ts +22 -0
- package/dist/bigint/median.js +28 -0
- package/dist/bigint/median.mjs +28 -0
- package/dist/bigint/medianBy.d.mts +21 -0
- package/dist/bigint/medianBy.d.ts +21 -0
- package/dist/bigint/medianBy.js +24 -0
- package/dist/bigint/medianBy.mjs +24 -0
- package/dist/bigint/min.d.mts +18 -0
- package/dist/bigint/min.d.ts +18 -0
- package/dist/bigint/min.js +23 -0
- package/dist/bigint/min.mjs +23 -0
- package/dist/bigint/minBy.d.mts +21 -0
- package/dist/bigint/minBy.d.ts +21 -0
- package/dist/bigint/minBy.js +34 -0
- package/dist/bigint/minBy.mjs +34 -0
- package/dist/bigint/percentile.d.mts +23 -0
- package/dist/bigint/percentile.d.ts +23 -0
- package/dist/bigint/percentile.js +31 -0
- package/dist/bigint/percentile.mjs +31 -0
- package/dist/bigint/range.d.mts +45 -0
- package/dist/bigint/range.d.ts +45 -0
- package/dist/bigint/range.js +24 -0
- package/dist/bigint/range.mjs +24 -0
- package/dist/bigint/rangeRight.d.mts +43 -0
- package/dist/bigint/rangeRight.d.ts +43 -0
- package/dist/bigint/rangeRight.js +25 -0
- package/dist/bigint/rangeRight.mjs +25 -0
- package/dist/bigint/sum.d.mts +18 -0
- package/dist/bigint/sum.d.ts +18 -0
- package/dist/bigint/sum.js +22 -0
- package/dist/bigint/sum.mjs +22 -0
- package/dist/bigint/sumBy.d.mts +20 -0
- package/dist/bigint/sumBy.d.ts +20 -0
- package/dist/bigint/sumBy.js +24 -0
- package/dist/bigint/sumBy.mjs +24 -0
- package/package.json +22 -1
package/bigint.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/bigint';
|
package/bigint.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/bigint');
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/_internal/bigIntRangeLength.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates how many elements a bigint range from `start` (inclusive) to `end` (exclusive) contains.
|
|
4
|
+
*
|
|
5
|
+
* This mirrors `Math.ceil((end - start) / step)` from the `number` implementations, but performs the
|
|
6
|
+
* ceiling division with bigint arithmetic. Returns `0` when `step` points away from `end`.
|
|
7
|
+
*
|
|
8
|
+
* @param start - The start of the range.
|
|
9
|
+
* @param end - The end of the range.
|
|
10
|
+
* @param step - A non-zero step value.
|
|
11
|
+
* @returns The number of elements in the range.
|
|
12
|
+
*/
|
|
13
|
+
function bigIntRangeLength(start, end, step) {
|
|
14
|
+
const difference = end - start;
|
|
15
|
+
if (step > 0n) return difference <= 0n ? 0 : Number((difference + step - 1n) / step);
|
|
16
|
+
return difference >= 0n ? 0 : Number((difference + step + 1n) / step);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
exports.bigIntRangeLength = bigIntRangeLength;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/_internal/bigIntRangeLength.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates how many elements a bigint range from `start` (inclusive) to `end` (exclusive) contains.
|
|
4
|
+
*
|
|
5
|
+
* This mirrors `Math.ceil((end - start) / step)` from the `number` implementations, but performs the
|
|
6
|
+
* ceiling division with bigint arithmetic. Returns `0` when `step` points away from `end`.
|
|
7
|
+
*
|
|
8
|
+
* @param start - The start of the range.
|
|
9
|
+
* @param end - The end of the range.
|
|
10
|
+
* @param step - A non-zero step value.
|
|
11
|
+
* @returns The number of elements in the range.
|
|
12
|
+
*/
|
|
13
|
+
function bigIntRangeLength(start, end, step) {
|
|
14
|
+
const difference = end - start;
|
|
15
|
+
if (step > 0n) return difference <= 0n ? 0 : Number((difference + step - 1n) / step);
|
|
16
|
+
return difference >= 0n ? 0 : Number((difference + step + 1n) / step);
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { bigIntRangeLength };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/bigint/clamp.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
4
|
+
*
|
|
5
|
+
* This function takes a bigint and returns it constrained to the given range. Unlike `Math.min` and
|
|
6
|
+
* `Math.max`, which cannot accept bigints, it compares values directly so large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The bigint to clamp.
|
|
9
|
+
* @param maximum - The upper bound to clamp to.
|
|
10
|
+
* @returns The clamped bigint.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = clamp(10n, 5n);
|
|
14
|
+
* // result will be 5n, because 10n is greater than the maximum
|
|
15
|
+
*/
|
|
16
|
+
declare function clamp(value: bigint, maximum: bigint): bigint;
|
|
17
|
+
/**
|
|
18
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
19
|
+
*
|
|
20
|
+
* This function takes a bigint and returns it constrained to the given range. Unlike `Math.min` and
|
|
21
|
+
* `Math.max`, which cannot accept bigints, it compares values directly so large integers stay exact.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The bigint to clamp.
|
|
24
|
+
* @param minimum - The lower bound to clamp to.
|
|
25
|
+
* @param maximum - The upper bound to clamp to.
|
|
26
|
+
* @returns The clamped bigint.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const result = clamp(10n, 0n, 5n);
|
|
30
|
+
* // result will be 5n, because 10n is greater than the maximum
|
|
31
|
+
*
|
|
32
|
+
* const result2 = clamp(-10n, 0n, 5n);
|
|
33
|
+
* // result2 will be 0n, because -10n is less than the minimum
|
|
34
|
+
*/
|
|
35
|
+
declare function clamp(value: bigint, minimum: bigint, maximum: bigint): bigint;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { clamp };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/bigint/clamp.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
4
|
+
*
|
|
5
|
+
* This function takes a bigint and returns it constrained to the given range. Unlike `Math.min` and
|
|
6
|
+
* `Math.max`, which cannot accept bigints, it compares values directly so large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param value - The bigint to clamp.
|
|
9
|
+
* @param maximum - The upper bound to clamp to.
|
|
10
|
+
* @returns The clamped bigint.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = clamp(10n, 5n);
|
|
14
|
+
* // result will be 5n, because 10n is greater than the maximum
|
|
15
|
+
*/
|
|
16
|
+
declare function clamp(value: bigint, maximum: bigint): bigint;
|
|
17
|
+
/**
|
|
18
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
19
|
+
*
|
|
20
|
+
* This function takes a bigint and returns it constrained to the given range. Unlike `Math.min` and
|
|
21
|
+
* `Math.max`, which cannot accept bigints, it compares values directly so large integers stay exact.
|
|
22
|
+
*
|
|
23
|
+
* @param value - The bigint to clamp.
|
|
24
|
+
* @param minimum - The lower bound to clamp to.
|
|
25
|
+
* @param maximum - The upper bound to clamp to.
|
|
26
|
+
* @returns The clamped bigint.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const result = clamp(10n, 0n, 5n);
|
|
30
|
+
* // result will be 5n, because 10n is greater than the maximum
|
|
31
|
+
*
|
|
32
|
+
* const result2 = clamp(-10n, 0n, 5n);
|
|
33
|
+
* // result2 will be 0n, because -10n is less than the minimum
|
|
34
|
+
*/
|
|
35
|
+
declare function clamp(value: bigint, minimum: bigint, maximum: bigint): bigint;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { clamp };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/bigint/clamp.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The bigint to clamp.
|
|
6
|
+
* @param bound1 - The upper bound when called with two arguments, otherwise the lower bound.
|
|
7
|
+
* @param bound2 - The upper bound when called with three arguments.
|
|
8
|
+
* @returns The clamped bigint.
|
|
9
|
+
*/
|
|
10
|
+
function clamp(value, bound1, bound2) {
|
|
11
|
+
if (bound2 == null) return value < bound1 ? value : bound1;
|
|
12
|
+
const lowerClamped = value > bound1 ? value : bound1;
|
|
13
|
+
return lowerClamped < bound2 ? lowerClamped : bound2;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.clamp = clamp;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/bigint/clamp.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clamps a bigint within the inclusive lower and upper bounds.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The bigint to clamp.
|
|
6
|
+
* @param bound1 - The upper bound when called with two arguments, otherwise the lower bound.
|
|
7
|
+
* @param bound2 - The upper bound when called with three arguments.
|
|
8
|
+
* @returns The clamped bigint.
|
|
9
|
+
*/
|
|
10
|
+
function clamp(value, bound1, bound2) {
|
|
11
|
+
if (bound2 == null) return value < bound1 ? value : bound1;
|
|
12
|
+
const lowerClamped = value > bound1 ? value : bound1;
|
|
13
|
+
return lowerClamped < bound2 ? lowerClamped : bound2;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { clamp };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/bigint/inRange.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a bigint is within a range.
|
|
4
|
+
*
|
|
5
|
+
* The lower bound is inclusive and the upper bound is exclusive, so `inRange(5n, 0n, 5n)` is `false`.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The bigint to check.
|
|
8
|
+
* @param maximum - The exclusive upper bound. The lower bound defaults to `0n`.
|
|
9
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
10
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = inRange(3n, 5n);
|
|
14
|
+
* // result will be true, because 3n is within [0n, 5n)
|
|
15
|
+
*/
|
|
16
|
+
declare function inRange(value: bigint, maximum: bigint): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Checks whether a bigint is within a range.
|
|
19
|
+
*
|
|
20
|
+
* The lower bound is inclusive and the upper bound is exclusive, so `inRange(5n, 0n, 5n)` is `false`.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The bigint to check.
|
|
23
|
+
* @param minimum - The inclusive lower bound.
|
|
24
|
+
* @param maximum - The exclusive upper bound.
|
|
25
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
26
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const result = inRange(5n, 0n, 10n);
|
|
30
|
+
* // result will be true
|
|
31
|
+
*
|
|
32
|
+
* const result2 = inRange(10n, 0n, 10n);
|
|
33
|
+
* // result2 will be false, because the upper bound is exclusive
|
|
34
|
+
*/
|
|
35
|
+
declare function inRange(value: bigint, minimum: bigint, maximum: bigint): boolean;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { inRange };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/bigint/inRange.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a bigint is within a range.
|
|
4
|
+
*
|
|
5
|
+
* The lower bound is inclusive and the upper bound is exclusive, so `inRange(5n, 0n, 5n)` is `false`.
|
|
6
|
+
*
|
|
7
|
+
* @param value - The bigint to check.
|
|
8
|
+
* @param maximum - The exclusive upper bound. The lower bound defaults to `0n`.
|
|
9
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
10
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = inRange(3n, 5n);
|
|
14
|
+
* // result will be true, because 3n is within [0n, 5n)
|
|
15
|
+
*/
|
|
16
|
+
declare function inRange(value: bigint, maximum: bigint): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Checks whether a bigint is within a range.
|
|
19
|
+
*
|
|
20
|
+
* The lower bound is inclusive and the upper bound is exclusive, so `inRange(5n, 0n, 5n)` is `false`.
|
|
21
|
+
*
|
|
22
|
+
* @param value - The bigint to check.
|
|
23
|
+
* @param minimum - The inclusive lower bound.
|
|
24
|
+
* @param maximum - The exclusive upper bound.
|
|
25
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
26
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const result = inRange(5n, 0n, 10n);
|
|
30
|
+
* // result will be true
|
|
31
|
+
*
|
|
32
|
+
* const result2 = inRange(10n, 0n, 10n);
|
|
33
|
+
* // result2 will be false, because the upper bound is exclusive
|
|
34
|
+
*/
|
|
35
|
+
declare function inRange(value: bigint, minimum: bigint, maximum: bigint): boolean;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { inRange };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/bigint/inRange.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a bigint is within a range.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The bigint to check.
|
|
6
|
+
* @param minimum - The exclusive upper bound when called with two arguments, otherwise the inclusive lower bound.
|
|
7
|
+
* @param maximum - The exclusive upper bound when called with three arguments.
|
|
8
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
9
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
10
|
+
*/
|
|
11
|
+
function inRange(value, minimum, maximum) {
|
|
12
|
+
if (maximum == null) {
|
|
13
|
+
maximum = minimum;
|
|
14
|
+
minimum = 0n;
|
|
15
|
+
}
|
|
16
|
+
if (minimum >= maximum) throw new Error("The maximum value must be greater than the minimum value.");
|
|
17
|
+
return minimum <= value && value < maximum;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
exports.inRange = inRange;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/bigint/inRange.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks whether a bigint is within a range.
|
|
4
|
+
*
|
|
5
|
+
* @param value - The bigint to check.
|
|
6
|
+
* @param minimum - The exclusive upper bound when called with two arguments, otherwise the inclusive lower bound.
|
|
7
|
+
* @param maximum - The exclusive upper bound when called with three arguments.
|
|
8
|
+
* @returns `true` if the bigint is within the range, `false` otherwise.
|
|
9
|
+
* @throws {Error} If the minimum is greater than or equal to the maximum.
|
|
10
|
+
*/
|
|
11
|
+
function inRange(value, minimum, maximum) {
|
|
12
|
+
if (maximum == null) {
|
|
13
|
+
maximum = minimum;
|
|
14
|
+
minimum = 0n;
|
|
15
|
+
}
|
|
16
|
+
if (minimum >= maximum) throw new Error("The maximum value must be greater than the minimum value.");
|
|
17
|
+
return minimum <= value && value < maximum;
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { inRange };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { clamp } from "./clamp.mjs";
|
|
2
|
+
import { inRange } from "./inRange.mjs";
|
|
3
|
+
import { max } from "./max.mjs";
|
|
4
|
+
import { maxBy } from "./maxBy.mjs";
|
|
5
|
+
import { median } from "./median.mjs";
|
|
6
|
+
import { medianBy } from "./medianBy.mjs";
|
|
7
|
+
import { min } from "./min.mjs";
|
|
8
|
+
import { minBy } from "./minBy.mjs";
|
|
9
|
+
import { percentile } from "./percentile.mjs";
|
|
10
|
+
import { range } from "./range.mjs";
|
|
11
|
+
import { rangeRight } from "./rangeRight.mjs";
|
|
12
|
+
import { sum } from "./sum.mjs";
|
|
13
|
+
import { sumBy } from "./sumBy.mjs";
|
|
14
|
+
export { clamp, inRange, max, maxBy, median, medianBy, min, minBy, percentile, range, rangeRight, sum, sumBy };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { clamp } from "./clamp.js";
|
|
2
|
+
import { inRange } from "./inRange.js";
|
|
3
|
+
import { max } from "./max.js";
|
|
4
|
+
import { maxBy } from "./maxBy.js";
|
|
5
|
+
import { median } from "./median.js";
|
|
6
|
+
import { medianBy } from "./medianBy.js";
|
|
7
|
+
import { min } from "./min.js";
|
|
8
|
+
import { minBy } from "./minBy.js";
|
|
9
|
+
import { percentile } from "./percentile.js";
|
|
10
|
+
import { range } from "./range.js";
|
|
11
|
+
import { rangeRight } from "./rangeRight.js";
|
|
12
|
+
import { sum } from "./sum.js";
|
|
13
|
+
import { sumBy } from "./sumBy.js";
|
|
14
|
+
export { clamp, inRange, max, maxBy, median, medianBy, min, minBy, percentile, range, rangeRight, sum, sumBy };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_clamp = require("./clamp.js");
|
|
3
|
+
const require_inRange = require("./inRange.js");
|
|
4
|
+
const require_max = require("./max.js");
|
|
5
|
+
const require_maxBy = require("./maxBy.js");
|
|
6
|
+
const require_median = require("./median.js");
|
|
7
|
+
const require_medianBy = require("./medianBy.js");
|
|
8
|
+
const require_min = require("./min.js");
|
|
9
|
+
const require_minBy = require("./minBy.js");
|
|
10
|
+
const require_percentile = require("./percentile.js");
|
|
11
|
+
const require_range = require("./range.js");
|
|
12
|
+
const require_rangeRight = require("./rangeRight.js");
|
|
13
|
+
const require_sum = require("./sum.js");
|
|
14
|
+
const require_sumBy = require("./sumBy.js");
|
|
15
|
+
exports.clamp = require_clamp.clamp;
|
|
16
|
+
exports.inRange = require_inRange.inRange;
|
|
17
|
+
exports.max = require_max.max;
|
|
18
|
+
exports.maxBy = require_maxBy.maxBy;
|
|
19
|
+
exports.median = require_median.median;
|
|
20
|
+
exports.medianBy = require_medianBy.medianBy;
|
|
21
|
+
exports.min = require_min.min;
|
|
22
|
+
exports.minBy = require_minBy.minBy;
|
|
23
|
+
exports.percentile = require_percentile.percentile;
|
|
24
|
+
exports.range = require_range.range;
|
|
25
|
+
exports.rangeRight = require_rangeRight.rangeRight;
|
|
26
|
+
exports.sum = require_sum.sum;
|
|
27
|
+
exports.sumBy = require_sumBy.sumBy;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { clamp } from "./clamp.mjs";
|
|
2
|
+
import { inRange } from "./inRange.mjs";
|
|
3
|
+
import { max } from "./max.mjs";
|
|
4
|
+
import { maxBy } from "./maxBy.mjs";
|
|
5
|
+
import { median } from "./median.mjs";
|
|
6
|
+
import { medianBy } from "./medianBy.mjs";
|
|
7
|
+
import { min } from "./min.mjs";
|
|
8
|
+
import { minBy } from "./minBy.mjs";
|
|
9
|
+
import { percentile } from "./percentile.mjs";
|
|
10
|
+
import { range } from "./range.mjs";
|
|
11
|
+
import { rangeRight } from "./rangeRight.mjs";
|
|
12
|
+
import { sum } from "./sum.mjs";
|
|
13
|
+
import { sumBy } from "./sumBy.mjs";
|
|
14
|
+
export { clamp, inRange, max, maxBy, median, medianBy, min, minBy, percentile, range, rangeRight, sum, sumBy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/max.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the largest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the largest element. Unlike `Math.max`,
|
|
6
|
+
* which cannot accept bigints, it compares values directly so arbitrarily large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param nums - An array of bigints to search.
|
|
9
|
+
* @returns The largest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no maximum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = max([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 5n
|
|
15
|
+
*
|
|
16
|
+
* const huge = max([9007199254740993n, 9007199254740992n]);
|
|
17
|
+
* // huge will be 9007199254740993n, a value `Math.max` cannot distinguish
|
|
18
|
+
*/
|
|
19
|
+
declare function max(nums: readonly bigint[]): bigint;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { max };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/max.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the largest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the largest element. Unlike `Math.max`,
|
|
6
|
+
* which cannot accept bigints, it compares values directly so arbitrarily large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param nums - An array of bigints to search.
|
|
9
|
+
* @returns The largest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no maximum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = max([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 5n
|
|
15
|
+
*
|
|
16
|
+
* const huge = max([9007199254740993n, 9007199254740992n]);
|
|
17
|
+
* // huge will be 9007199254740993n, a value `Math.max` cannot distinguish
|
|
18
|
+
*/
|
|
19
|
+
declare function max(nums: readonly bigint[]): bigint;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { max };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/bigint/max.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the largest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the largest element. Unlike `Math.max`,
|
|
6
|
+
* which cannot accept bigints, it compares values directly so arbitrarily large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param nums - An array of bigints to search.
|
|
9
|
+
* @returns The largest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no maximum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = max([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 5n
|
|
15
|
+
*
|
|
16
|
+
* const huge = max([9007199254740993n, 9007199254740992n]);
|
|
17
|
+
* // huge will be 9007199254740993n, a value `Math.max` cannot distinguish
|
|
18
|
+
*/
|
|
19
|
+
function max(nums) {
|
|
20
|
+
if (nums.length === 0) throw new RangeError("Cannot find the maximum of an empty array.");
|
|
21
|
+
let result = nums[0];
|
|
22
|
+
for (let i = 1; i < nums.length; i++) if (nums[i] > result) result = nums[i];
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
exports.max = max;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/bigint/max.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the largest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the largest element. Unlike `Math.max`,
|
|
6
|
+
* which cannot accept bigints, it compares values directly so arbitrarily large integers stay exact.
|
|
7
|
+
*
|
|
8
|
+
* @param nums - An array of bigints to search.
|
|
9
|
+
* @returns The largest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no maximum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = max([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 5n
|
|
15
|
+
*
|
|
16
|
+
* const huge = max([9007199254740993n, 9007199254740992n]);
|
|
17
|
+
* // huge will be 9007199254740993n, a value `Math.max` cannot distinguish
|
|
18
|
+
*/
|
|
19
|
+
function max(nums) {
|
|
20
|
+
if (nums.length === 0) throw new RangeError("Cannot find the maximum of an empty array.");
|
|
21
|
+
let result = nums[0];
|
|
22
|
+
for (let i = 1; i < nums.length; i++) if (nums[i] > result) result = nums[i];
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { max };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/maxBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the largest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the largest value.
|
|
6
|
+
* If several elements tie, the first one is returned.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to search.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to compare by.
|
|
11
|
+
* @returns The element with the largest mapped bigint.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no element to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = maxBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 30n }
|
|
18
|
+
*/
|
|
19
|
+
declare function maxBy<T>(items: readonly T[], getValue: (element: T, index: number, array: readonly T[]) => bigint): T;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { maxBy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/maxBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the largest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the largest value.
|
|
6
|
+
* If several elements tie, the first one is returned.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to search.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to compare by.
|
|
11
|
+
* @returns The element with the largest mapped bigint.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no element to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = maxBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 30n }
|
|
18
|
+
*/
|
|
19
|
+
declare function maxBy<T>(items: readonly T[], getValue: (element: T, index: number, array: readonly T[]) => bigint): T;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { maxBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/bigint/maxBy.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the largest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the largest value.
|
|
6
|
+
* If several elements tie, the first one is returned.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to search.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to compare by.
|
|
11
|
+
* @returns The element with the largest mapped bigint.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no element to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = maxBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 30n }
|
|
18
|
+
*/
|
|
19
|
+
function maxBy(items, getValue) {
|
|
20
|
+
if (items.length === 0) throw new RangeError("Cannot find the maximum of an empty array.");
|
|
21
|
+
let maxElement = items[0];
|
|
22
|
+
let max = getValue(items[0], 0, items);
|
|
23
|
+
for (let i = 1; i < items.length; i++) {
|
|
24
|
+
const element = items[i];
|
|
25
|
+
const value = getValue(element, i, items);
|
|
26
|
+
if (value > max) {
|
|
27
|
+
max = value;
|
|
28
|
+
maxElement = element;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return maxElement;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.maxBy = maxBy;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/bigint/maxBy.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the largest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the largest value.
|
|
6
|
+
* If several elements tie, the first one is returned.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to search.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to compare by.
|
|
11
|
+
* @returns The element with the largest mapped bigint.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no element to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = maxBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 30n }
|
|
18
|
+
*/
|
|
19
|
+
function maxBy(items, getValue) {
|
|
20
|
+
if (items.length === 0) throw new RangeError("Cannot find the maximum of an empty array.");
|
|
21
|
+
let maxElement = items[0];
|
|
22
|
+
let max = getValue(items[0], 0, items);
|
|
23
|
+
for (let i = 1; i < items.length; i++) {
|
|
24
|
+
const element = items[i];
|
|
25
|
+
const value = getValue(element, i, items);
|
|
26
|
+
if (value > max) {
|
|
27
|
+
max = value;
|
|
28
|
+
maxElement = element;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return maxElement;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { maxBy };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/bigint/median.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the median of an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* The median is the middle value of a sorted array. When the array has an even number of elements,
|
|
6
|
+
* the two middle values are averaged. Because bigints have no fractional part, that average is
|
|
7
|
+
* truncated toward zero: `median([1n, 2n])` is `1n` and `median([-3n, -2n])` is `-2n`.
|
|
8
|
+
*
|
|
9
|
+
* @param nums - An array of bigints to calculate the median of.
|
|
10
|
+
* @returns The median of the array.
|
|
11
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const result = median([1n, 2n, 3n, 4n, 5n]);
|
|
15
|
+
* // result will be 3n
|
|
16
|
+
*
|
|
17
|
+
* const truncated = median([1n, 2n, 3n, 4n]);
|
|
18
|
+
* // truncated will be 2n, because (2n + 3n) / 2n truncates toward zero
|
|
19
|
+
*/
|
|
20
|
+
declare function median(nums: readonly bigint[]): bigint;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { median };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/bigint/median.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the median of an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* The median is the middle value of a sorted array. When the array has an even number of elements,
|
|
6
|
+
* the two middle values are averaged. Because bigints have no fractional part, that average is
|
|
7
|
+
* truncated toward zero: `median([1n, 2n])` is `1n` and `median([-3n, -2n])` is `-2n`.
|
|
8
|
+
*
|
|
9
|
+
* @param nums - An array of bigints to calculate the median of.
|
|
10
|
+
* @returns The median of the array.
|
|
11
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const result = median([1n, 2n, 3n, 4n, 5n]);
|
|
15
|
+
* // result will be 3n
|
|
16
|
+
*
|
|
17
|
+
* const truncated = median([1n, 2n, 3n, 4n]);
|
|
18
|
+
* // truncated will be 2n, because (2n + 3n) / 2n truncates toward zero
|
|
19
|
+
*/
|
|
20
|
+
declare function median(nums: readonly bigint[]): bigint;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { median };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region src/bigint/median.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the median of an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* The median is the middle value of a sorted array. When the array has an even number of elements,
|
|
6
|
+
* the two middle values are averaged. Because bigints have no fractional part, that average is
|
|
7
|
+
* truncated toward zero: `median([1n, 2n])` is `1n` and `median([-3n, -2n])` is `-2n`.
|
|
8
|
+
*
|
|
9
|
+
* @param nums - An array of bigints to calculate the median of.
|
|
10
|
+
* @returns The median of the array.
|
|
11
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const result = median([1n, 2n, 3n, 4n, 5n]);
|
|
15
|
+
* // result will be 3n
|
|
16
|
+
*
|
|
17
|
+
* const truncated = median([1n, 2n, 3n, 4n]);
|
|
18
|
+
* // truncated will be 2n, because (2n + 3n) / 2n truncates toward zero
|
|
19
|
+
*/
|
|
20
|
+
function median(nums) {
|
|
21
|
+
if (nums.length === 0) throw new RangeError("Cannot compute the median of an empty array.");
|
|
22
|
+
const sorted = nums.slice().sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
23
|
+
const middleIndex = Math.floor(sorted.length / 2);
|
|
24
|
+
if (sorted.length % 2 === 0) return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2n;
|
|
25
|
+
return sorted[middleIndex];
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
exports.median = median;
|