es-toolkit 1.50.0-dev.1991 → 1.50.0-dev.1993
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
|
@@ -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
|
+
export { median };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/medianBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the median of an array by mapping each element to a bigint.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the median of those values. Because
|
|
6
|
+
* bigints have no fractional part, the average taken for even-length arrays is truncated toward zero.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to calculate the median of.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to use.
|
|
11
|
+
* @returns The median of the mapped bigints.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = medianBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be 20n
|
|
18
|
+
*/
|
|
19
|
+
declare function medianBy<T>(items: readonly T[], getValue: (element: T) => bigint): bigint;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { medianBy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/medianBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the median of an array by mapping each element to a bigint.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the median of those values. Because
|
|
6
|
+
* bigints have no fractional part, the average taken for even-length arrays is truncated toward zero.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The type of elements in the array.
|
|
9
|
+
* @param items - An array of elements to calculate the median of.
|
|
10
|
+
* @param getValue - A function that maps an element to the bigint to use.
|
|
11
|
+
* @returns The median of the mapped bigints.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
16
|
+
* const result = medianBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be 20n
|
|
18
|
+
*/
|
|
19
|
+
declare function medianBy<T>(items: readonly T[], getValue: (element: T) => bigint): bigint;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { medianBy };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const require_median = require("./median.js");
|
|
2
|
+
//#region src/bigint/medianBy.ts
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the median of an array by mapping each element to a bigint.
|
|
5
|
+
*
|
|
6
|
+
* This function maps every element to a bigint and returns the median of those values. Because
|
|
7
|
+
* bigints have no fractional part, the average taken for even-length arrays is truncated toward zero.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements in the array.
|
|
10
|
+
* @param items - An array of elements to calculate the median of.
|
|
11
|
+
* @param getValue - A function that maps an element to the bigint to use.
|
|
12
|
+
* @returns The median of the mapped bigints.
|
|
13
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
17
|
+
* const result = medianBy(accounts, account => account.balance);
|
|
18
|
+
* // result will be 20n
|
|
19
|
+
*/
|
|
20
|
+
function medianBy(items, getValue) {
|
|
21
|
+
return require_median.median(items.map((item) => getValue(item)));
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
exports.medianBy = medianBy;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { median } from "./median.mjs";
|
|
2
|
+
//#region src/bigint/medianBy.ts
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the median of an array by mapping each element to a bigint.
|
|
5
|
+
*
|
|
6
|
+
* This function maps every element to a bigint and returns the median of those values. Because
|
|
7
|
+
* bigints have no fractional part, the average taken for even-length arrays is truncated toward zero.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The type of elements in the array.
|
|
10
|
+
* @param items - An array of elements to calculate the median of.
|
|
11
|
+
* @param getValue - A function that maps an element to the bigint to use.
|
|
12
|
+
* @returns The median of the mapped bigints.
|
|
13
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no median".
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const accounts = [{ balance: 10n }, { balance: 30n }, { balance: 20n }];
|
|
17
|
+
* const result = medianBy(accounts, account => account.balance);
|
|
18
|
+
* // result will be 20n
|
|
19
|
+
*/
|
|
20
|
+
function medianBy(items, getValue) {
|
|
21
|
+
return median(items.map((item) => getValue(item)));
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { medianBy };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/bigint/min.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the smallest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the smallest element. Unlike `Math.min`,
|
|
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 smallest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no minimum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = min([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 1n
|
|
15
|
+
*/
|
|
16
|
+
declare function min(nums: readonly bigint[]): bigint;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { min };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/bigint/min.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the smallest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the smallest element. Unlike `Math.min`,
|
|
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 smallest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no minimum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = min([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 1n
|
|
15
|
+
*/
|
|
16
|
+
declare function min(nums: readonly bigint[]): bigint;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { min };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/bigint/min.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the smallest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the smallest element. Unlike `Math.min`,
|
|
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 smallest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no minimum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = min([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 1n
|
|
15
|
+
*/
|
|
16
|
+
function min(nums) {
|
|
17
|
+
if (nums.length === 0) throw new RangeError("Cannot find the minimum of an empty array.");
|
|
18
|
+
let result = nums[0];
|
|
19
|
+
for (let i = 1; i < nums.length; i++) if (nums[i] < result) result = nums[i];
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.min = min;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/bigint/min.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the smallest bigint in an array.
|
|
4
|
+
*
|
|
5
|
+
* This function iterates through the array and returns the smallest element. Unlike `Math.min`,
|
|
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 smallest bigint in the array.
|
|
10
|
+
* @throws {RangeError} If the array is empty, since there is no bigint that represents "no minimum".
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const result = min([1n, 5n, 3n]);
|
|
14
|
+
* // result will be 1n
|
|
15
|
+
*/
|
|
16
|
+
function min(nums) {
|
|
17
|
+
if (nums.length === 0) throw new RangeError("Cannot find the minimum of an empty array.");
|
|
18
|
+
let result = nums[0];
|
|
19
|
+
for (let i = 1; i < nums.length; i++) if (nums[i] < result) result = nums[i];
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { min };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/minBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the smallest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the smallest 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 smallest 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 = minBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 10n }
|
|
18
|
+
*/
|
|
19
|
+
declare function minBy<T>(items: readonly T[], getValue: (element: T, index: number, array: readonly T[]) => bigint): T;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { minBy };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/bigint/minBy.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the smallest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the smallest 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 smallest 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 = minBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 10n }
|
|
18
|
+
*/
|
|
19
|
+
declare function minBy<T>(items: readonly T[], getValue: (element: T, index: number, array: readonly T[]) => bigint): T;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { minBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/bigint/minBy.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the smallest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the smallest 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 smallest 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 = minBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 10n }
|
|
18
|
+
*/
|
|
19
|
+
function minBy(items, getValue) {
|
|
20
|
+
if (items.length === 0) throw new RangeError("Cannot find the minimum of an empty array.");
|
|
21
|
+
let minElement = items[0];
|
|
22
|
+
let min = 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 < min) {
|
|
27
|
+
min = value;
|
|
28
|
+
minElement = element;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return minElement;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.minBy = minBy;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/bigint/minBy.ts
|
|
2
|
+
/**
|
|
3
|
+
* Finds the element in an array that has the smallest bigint value returned by `getValue`.
|
|
4
|
+
*
|
|
5
|
+
* This function maps every element to a bigint and returns the element with the smallest 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 smallest 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 = minBy(accounts, account => account.balance);
|
|
17
|
+
* // result will be { balance: 10n }
|
|
18
|
+
*/
|
|
19
|
+
function minBy(items, getValue) {
|
|
20
|
+
if (items.length === 0) throw new RangeError("Cannot find the minimum of an empty array.");
|
|
21
|
+
let minElement = items[0];
|
|
22
|
+
let min = 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 < min) {
|
|
27
|
+
min = value;
|
|
28
|
+
minElement = element;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return minElement;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { minBy };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/bigint/percentile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the value at a given percentile in an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* This function uses the [nearest-rank method](https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method),
|
|
6
|
+
* so it always returns one of the values that is already in the array and never interpolates between them.
|
|
7
|
+
*
|
|
8
|
+
* @param arr - An array of bigints to calculate the percentile of.
|
|
9
|
+
* @param percentile - The percentile to look up, as a number between `0` and `100`.
|
|
10
|
+
* @returns The bigint at the given percentile.
|
|
11
|
+
* @throws {Error} If `percentile` is not a number between `0` and `100`.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const result = percentile([1n, 2n, 3n, 4n, 5n], 50);
|
|
16
|
+
* // result will be 3n
|
|
17
|
+
*
|
|
18
|
+
* const p90 = percentile([1n, 2n, 3n, 4n, 5n], 90);
|
|
19
|
+
* // p90 will be 5n
|
|
20
|
+
*/
|
|
21
|
+
declare function percentile(arr: readonly bigint[], percentile: number): bigint;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { percentile };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/bigint/percentile.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the value at a given percentile in an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* This function uses the [nearest-rank method](https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method),
|
|
6
|
+
* so it always returns one of the values that is already in the array and never interpolates between them.
|
|
7
|
+
*
|
|
8
|
+
* @param arr - An array of bigints to calculate the percentile of.
|
|
9
|
+
* @param percentile - The percentile to look up, as a number between `0` and `100`.
|
|
10
|
+
* @returns The bigint at the given percentile.
|
|
11
|
+
* @throws {Error} If `percentile` is not a number between `0` and `100`.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const result = percentile([1n, 2n, 3n, 4n, 5n], 50);
|
|
16
|
+
* // result will be 3n
|
|
17
|
+
*
|
|
18
|
+
* const p90 = percentile([1n, 2n, 3n, 4n, 5n], 90);
|
|
19
|
+
* // p90 will be 5n
|
|
20
|
+
*/
|
|
21
|
+
declare function percentile(arr: readonly bigint[], percentile: number): bigint;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { percentile };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/bigint/percentile.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the value at a given percentile in an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* This function uses the [nearest-rank method](https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method),
|
|
6
|
+
* so it always returns one of the values that is already in the array and never interpolates between them.
|
|
7
|
+
*
|
|
8
|
+
* @param arr - An array of bigints to calculate the percentile of.
|
|
9
|
+
* @param percentile - The percentile to look up, as a number between `0` and `100`.
|
|
10
|
+
* @returns The bigint at the given percentile.
|
|
11
|
+
* @throws {Error} If `percentile` is not a number between `0` and `100`.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const result = percentile([1n, 2n, 3n, 4n, 5n], 50);
|
|
16
|
+
* // result will be 3n
|
|
17
|
+
*
|
|
18
|
+
* const p90 = percentile([1n, 2n, 3n, 4n, 5n], 90);
|
|
19
|
+
* // p90 will be 5n
|
|
20
|
+
*/
|
|
21
|
+
function percentile(arr, percentile) {
|
|
22
|
+
if (Number.isNaN(Number(percentile))) throw new Error(`Expected percentile to be a number but got "${percentile}".`);
|
|
23
|
+
if (percentile < 0) throw new Error(`Expected percentile to be >= 0 but got "${percentile}".`);
|
|
24
|
+
if (percentile > 100) throw new Error(`Expected percentile to be <= 100 but got "${percentile}".`);
|
|
25
|
+
if (arr.length === 0) throw new RangeError("Cannot compute the percentile of an empty array.");
|
|
26
|
+
const sorted = arr.slice().sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
27
|
+
if (percentile === 0) return sorted[0];
|
|
28
|
+
return sorted[Math.ceil(sorted.length * (percentile / 100)) - 1];
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
exports.percentile = percentile;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/bigint/percentile.ts
|
|
2
|
+
/**
|
|
3
|
+
* Calculates the value at a given percentile in an array of bigints.
|
|
4
|
+
*
|
|
5
|
+
* This function uses the [nearest-rank method](https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method),
|
|
6
|
+
* so it always returns one of the values that is already in the array and never interpolates between them.
|
|
7
|
+
*
|
|
8
|
+
* @param arr - An array of bigints to calculate the percentile of.
|
|
9
|
+
* @param percentile - The percentile to look up, as a number between `0` and `100`.
|
|
10
|
+
* @returns The bigint at the given percentile.
|
|
11
|
+
* @throws {Error} If `percentile` is not a number between `0` and `100`.
|
|
12
|
+
* @throws {RangeError} If the array is empty, since there is no bigint to return.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const result = percentile([1n, 2n, 3n, 4n, 5n], 50);
|
|
16
|
+
* // result will be 3n
|
|
17
|
+
*
|
|
18
|
+
* const p90 = percentile([1n, 2n, 3n, 4n, 5n], 90);
|
|
19
|
+
* // p90 will be 5n
|
|
20
|
+
*/
|
|
21
|
+
function percentile(arr, percentile) {
|
|
22
|
+
if (Number.isNaN(Number(percentile))) throw new Error(`Expected percentile to be a number but got "${percentile}".`);
|
|
23
|
+
if (percentile < 0) throw new Error(`Expected percentile to be >= 0 but got "${percentile}".`);
|
|
24
|
+
if (percentile > 100) throw new Error(`Expected percentile to be <= 100 but got "${percentile}".`);
|
|
25
|
+
if (arr.length === 0) throw new RangeError("Cannot compute the percentile of an empty array.");
|
|
26
|
+
const sorted = arr.slice().sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
27
|
+
if (percentile === 0) return sorted[0];
|
|
28
|
+
return sorted[Math.ceil(sorted.length * (percentile / 100)) - 1];
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { percentile };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/bigint/range.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns an array of bigints from `0n` up to, but not including, `end`.
|
|
4
|
+
*
|
|
5
|
+
* @param end - The exclusive end of the range.
|
|
6
|
+
* @returns An array of bigints.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = range(4n);
|
|
10
|
+
* // result will be [0n, 1n, 2n, 3n]
|
|
11
|
+
*/
|
|
12
|
+
declare function range(end: bigint): bigint[];
|
|
13
|
+
/**
|
|
14
|
+
* Returns an array of bigints from `start` up to, but not including, `end`.
|
|
15
|
+
*
|
|
16
|
+
* @param start - The inclusive start of the range.
|
|
17
|
+
* @param end - The exclusive end of the range.
|
|
18
|
+
* @returns An array of bigints.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const result = range(2n, 5n);
|
|
22
|
+
* // result will be [2n, 3n, 4n]
|
|
23
|
+
*/
|
|
24
|
+
declare function range(start: bigint, end: bigint): bigint[];
|
|
25
|
+
/**
|
|
26
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, incrementing by `step`.
|
|
27
|
+
*
|
|
28
|
+
* A negative `step` counts down. If `step` points away from `end`, an empty array is returned.
|
|
29
|
+
*
|
|
30
|
+
* @param start - The inclusive start of the range.
|
|
31
|
+
* @param end - The exclusive end of the range.
|
|
32
|
+
* @param step - The amount to increment by. Must not be `0n`.
|
|
33
|
+
* @returns An array of bigints.
|
|
34
|
+
* @throws {Error} If `step` is `0n`.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const result = range(0n, 10n, 2n);
|
|
38
|
+
* // result will be [0n, 2n, 4n, 6n, 8n]
|
|
39
|
+
*
|
|
40
|
+
* const countdown = range(5n, 0n, -1n);
|
|
41
|
+
* // countdown will be [5n, 4n, 3n, 2n, 1n]
|
|
42
|
+
*/
|
|
43
|
+
declare function range(start: bigint, end: bigint, step: bigint): bigint[];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { range };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/bigint/range.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns an array of bigints from `0n` up to, but not including, `end`.
|
|
4
|
+
*
|
|
5
|
+
* @param end - The exclusive end of the range.
|
|
6
|
+
* @returns An array of bigints.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = range(4n);
|
|
10
|
+
* // result will be [0n, 1n, 2n, 3n]
|
|
11
|
+
*/
|
|
12
|
+
declare function range(end: bigint): bigint[];
|
|
13
|
+
/**
|
|
14
|
+
* Returns an array of bigints from `start` up to, but not including, `end`.
|
|
15
|
+
*
|
|
16
|
+
* @param start - The inclusive start of the range.
|
|
17
|
+
* @param end - The exclusive end of the range.
|
|
18
|
+
* @returns An array of bigints.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const result = range(2n, 5n);
|
|
22
|
+
* // result will be [2n, 3n, 4n]
|
|
23
|
+
*/
|
|
24
|
+
declare function range(start: bigint, end: bigint): bigint[];
|
|
25
|
+
/**
|
|
26
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, incrementing by `step`.
|
|
27
|
+
*
|
|
28
|
+
* A negative `step` counts down. If `step` points away from `end`, an empty array is returned.
|
|
29
|
+
*
|
|
30
|
+
* @param start - The inclusive start of the range.
|
|
31
|
+
* @param end - The exclusive end of the range.
|
|
32
|
+
* @param step - The amount to increment by. Must not be `0n`.
|
|
33
|
+
* @returns An array of bigints.
|
|
34
|
+
* @throws {Error} If `step` is `0n`.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const result = range(0n, 10n, 2n);
|
|
38
|
+
* // result will be [0n, 2n, 4n, 6n, 8n]
|
|
39
|
+
*
|
|
40
|
+
* const countdown = range(5n, 0n, -1n);
|
|
41
|
+
* // countdown will be [5n, 4n, 3n, 2n, 1n]
|
|
42
|
+
*/
|
|
43
|
+
declare function range(start: bigint, end: bigint, step: bigint): bigint[];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { range };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const require_bigIntRangeLength = require("../_internal/bigIntRangeLength.js");
|
|
2
|
+
//#region src/bigint/range.ts
|
|
3
|
+
/**
|
|
4
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, incrementing by `step`.
|
|
5
|
+
*
|
|
6
|
+
* @param start - The exclusive end of the range when called with one argument, otherwise the inclusive start.
|
|
7
|
+
* @param end - The exclusive end of the range.
|
|
8
|
+
* @param step - The amount to increment by. Must not be `0n`.
|
|
9
|
+
* @returns An array of bigints.
|
|
10
|
+
* @throws {Error} If `step` is `0n`.
|
|
11
|
+
*/
|
|
12
|
+
function range(start, end, step = 1n) {
|
|
13
|
+
if (end == null) {
|
|
14
|
+
end = start;
|
|
15
|
+
start = 0n;
|
|
16
|
+
}
|
|
17
|
+
if (step === 0n) throw new Error("The step value must be a non-zero bigint.");
|
|
18
|
+
const length = require_bigIntRangeLength.bigIntRangeLength(start, end, step);
|
|
19
|
+
const result = new Array(length);
|
|
20
|
+
for (let i = 0; i < length; i++) result[i] = start + BigInt(i) * step;
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
exports.range = range;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { bigIntRangeLength } from "../_internal/bigIntRangeLength.mjs";
|
|
2
|
+
//#region src/bigint/range.ts
|
|
3
|
+
/**
|
|
4
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, incrementing by `step`.
|
|
5
|
+
*
|
|
6
|
+
* @param start - The exclusive end of the range when called with one argument, otherwise the inclusive start.
|
|
7
|
+
* @param end - The exclusive end of the range.
|
|
8
|
+
* @param step - The amount to increment by. Must not be `0n`.
|
|
9
|
+
* @returns An array of bigints.
|
|
10
|
+
* @throws {Error} If `step` is `0n`.
|
|
11
|
+
*/
|
|
12
|
+
function range(start, end, step = 1n) {
|
|
13
|
+
if (end == null) {
|
|
14
|
+
end = start;
|
|
15
|
+
start = 0n;
|
|
16
|
+
}
|
|
17
|
+
if (step === 0n) throw new Error("The step value must be a non-zero bigint.");
|
|
18
|
+
const length = bigIntRangeLength(start, end, step);
|
|
19
|
+
const result = new Array(length);
|
|
20
|
+
for (let i = 0; i < length; i++) result[i] = start + BigInt(i) * step;
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { range };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region src/bigint/rangeRight.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns an array of bigints from `0n` up to, but not including, `end`, in descending order.
|
|
4
|
+
*
|
|
5
|
+
* @param end - The exclusive end of the range.
|
|
6
|
+
* @returns An array of bigints.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const result = rangeRight(4n);
|
|
10
|
+
* // result will be [3n, 2n, 1n, 0n]
|
|
11
|
+
*/
|
|
12
|
+
declare function rangeRight(end: bigint): bigint[];
|
|
13
|
+
/**
|
|
14
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, in descending order.
|
|
15
|
+
*
|
|
16
|
+
* @param start - The inclusive start of the range.
|
|
17
|
+
* @param end - The exclusive end of the range.
|
|
18
|
+
* @returns An array of bigints.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const result = rangeRight(2n, 5n);
|
|
22
|
+
* // result will be [4n, 3n, 2n]
|
|
23
|
+
*/
|
|
24
|
+
declare function rangeRight(start: bigint, end: bigint): bigint[];
|
|
25
|
+
/**
|
|
26
|
+
* Returns an array of bigints from `start` up to, but not including, `end`, incrementing by `step`,
|
|
27
|
+
* in descending order.
|
|
28
|
+
*
|
|
29
|
+
* A negative `step` counts down. If `step` points away from `end`, an empty array is returned.
|
|
30
|
+
*
|
|
31
|
+
* @param start - The inclusive start of the range.
|
|
32
|
+
* @param end - The exclusive end of the range.
|
|
33
|
+
* @param step - The amount to increment by. Must not be `0n`.
|
|
34
|
+
* @returns An array of bigints.
|
|
35
|
+
* @throws {Error} If `step` is `0n`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const result = rangeRight(0n, 10n, 2n);
|
|
39
|
+
* // result will be [8n, 6n, 4n, 2n, 0n]
|
|
40
|
+
*/
|
|
41
|
+
declare function rangeRight(start: bigint, end: bigint, step: bigint): bigint[];
|
|
42
|
+
//#endregion
|
|
43
|
+
export { rangeRight };
|