es-toolkit 1.25.2-dev.807 → 1.25.2-dev.809
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/dist/_chunk/{sumBy-BkErWJ.js → sumBy-RVppiV.js} +21 -0
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/orderBy.d.mts +3 -2
- package/dist/compat/array/orderBy.d.ts +3 -2
- package/dist/compat/array/orderBy.mjs +7 -3
- package/dist/compat/array/sortBy.d.mts +3 -3
- package/dist/compat/array/sortBy.d.ts +3 -3
- package/dist/compat/array/sortBy.mjs +11 -2
- package/dist/compat/index.d.mts +2 -0
- package/dist/compat/index.d.ts +2 -0
- package/dist/compat/index.js +19 -6
- package/dist/compat/index.mjs +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/index.mjs +2 -0
- package/dist/math/index.d.mts +2 -0
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.js +3 -1
- package/dist/math/index.mjs +2 -0
- package/dist/math/median.d.mts +25 -0
- package/dist/math/median.d.ts +25 -0
- package/dist/math/median.mjs +15 -0
- package/dist/math/medianBy.d.mts +23 -0
- package/dist/math/medianBy.d.ts +23 -0
- package/dist/math/medianBy.mjs +8 -0
- package/package.json +1 -1
|
@@ -7,9 +7,10 @@ type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null
|
|
|
7
7
|
* If values for a key are equal, it moves to the next key to determine the order.
|
|
8
8
|
*
|
|
9
9
|
* @template T - The type of elements in the array.
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {ArrayLike<T> | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
11
|
* @param {Criterion<T> | Array<Criterion<T>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
12
12
|
* @param {unknown | unknown[]} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
|
|
13
|
+
* @param {unknown} [guard] Enables use as an iteratee for methods like `_.reduce`.
|
|
13
14
|
* @returns {T[]} - The sorted array.
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
@@ -29,6 +30,6 @@ type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null
|
|
|
29
30
|
* // { user: 'fred', age: 40 },
|
|
30
31
|
* // ]
|
|
31
32
|
*/
|
|
32
|
-
declare function orderBy<T>(collection:
|
|
33
|
+
declare function orderBy<T = any>(collection: ArrayLike<T> | object | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>, orders?: unknown | unknown[], guard?: unknown): T[];
|
|
33
34
|
|
|
34
35
|
export { type Criterion, orderBy };
|
|
@@ -7,9 +7,10 @@ type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null
|
|
|
7
7
|
* If values for a key are equal, it moves to the next key to determine the order.
|
|
8
8
|
*
|
|
9
9
|
* @template T - The type of elements in the array.
|
|
10
|
-
* @param {
|
|
10
|
+
* @param {ArrayLike<T> | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
11
|
* @param {Criterion<T> | Array<Criterion<T>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
12
12
|
* @param {unknown | unknown[]} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
|
|
13
|
+
* @param {unknown} [guard] Enables use as an iteratee for methods like `_.reduce`.
|
|
13
14
|
* @returns {T[]} - The sorted array.
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
@@ -29,6 +30,6 @@ type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null
|
|
|
29
30
|
* // { user: 'fred', age: 40 },
|
|
30
31
|
* // ]
|
|
31
32
|
*/
|
|
32
|
-
declare function orderBy<T>(collection:
|
|
33
|
+
declare function orderBy<T = any>(collection: ArrayLike<T> | object | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>, orders?: unknown | unknown[], guard?: unknown): T[];
|
|
33
34
|
|
|
34
35
|
export { type Criterion, orderBy };
|
|
@@ -2,16 +2,20 @@ import { compareValues } from '../_internal/compareValues.mjs';
|
|
|
2
2
|
import { isKey } from '../_internal/isKey.mjs';
|
|
3
3
|
import { toPath } from '../util/toPath.mjs';
|
|
4
4
|
|
|
5
|
-
function orderBy(collection, criteria, orders) {
|
|
6
|
-
if (collection == null
|
|
5
|
+
function orderBy(collection, criteria, orders, guard) {
|
|
6
|
+
if (collection == null) {
|
|
7
7
|
return [];
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
orders = guard ? undefined : orders;
|
|
10
|
+
if (!Array.isArray(collection)) {
|
|
10
11
|
collection = Object.values(collection);
|
|
11
12
|
}
|
|
12
13
|
if (!Array.isArray(criteria)) {
|
|
13
14
|
criteria = criteria == null ? [null] : [criteria];
|
|
14
15
|
}
|
|
16
|
+
if (criteria.length === 0) {
|
|
17
|
+
criteria = [null];
|
|
18
|
+
}
|
|
15
19
|
if (!Array.isArray(orders)) {
|
|
16
20
|
orders = orders == null ? [] : [orders];
|
|
17
21
|
}
|
|
@@ -8,8 +8,8 @@ import { Criterion } from './orderBy.mjs';
|
|
|
8
8
|
* If values for a key are equal, it moves to the next key to determine the order.
|
|
9
9
|
*
|
|
10
10
|
* @template T - The type of elements in the array.
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {Criterion<T> |
|
|
11
|
+
* @param {ArrayLike<T> | object | null | undefined} collection - The array of objects to be sorted.
|
|
12
|
+
* @param {Array<Array<Criterion<T> | Criterion<T>>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
13
13
|
* @returns {T[]} - The ascending sorted array.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -29,6 +29,6 @@ import { Criterion } from './orderBy.mjs';
|
|
|
29
29
|
* // { user: 'fred', age: 48 },
|
|
30
30
|
* // ]
|
|
31
31
|
*/
|
|
32
|
-
declare function sortBy<T>(collection:
|
|
32
|
+
declare function sortBy<T = any>(collection: ArrayLike<T> | object | null | undefined, ...criteria: Array<Criterion<T> | Array<Criterion<T>>>): T[];
|
|
33
33
|
|
|
34
34
|
export { sortBy };
|
|
@@ -8,8 +8,8 @@ import { Criterion } from './orderBy.js';
|
|
|
8
8
|
* If values for a key are equal, it moves to the next key to determine the order.
|
|
9
9
|
*
|
|
10
10
|
* @template T - The type of elements in the array.
|
|
11
|
-
* @param {
|
|
12
|
-
* @param {Criterion<T> |
|
|
11
|
+
* @param {ArrayLike<T> | object | null | undefined} collection - The array of objects to be sorted.
|
|
12
|
+
* @param {Array<Array<Criterion<T> | Criterion<T>>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
13
13
|
* @returns {T[]} - The ascending sorted array.
|
|
14
14
|
*
|
|
15
15
|
* @example
|
|
@@ -29,6 +29,6 @@ import { Criterion } from './orderBy.js';
|
|
|
29
29
|
* // { user: 'fred', age: 48 },
|
|
30
30
|
* // ]
|
|
31
31
|
*/
|
|
32
|
-
declare function sortBy<T>(collection:
|
|
32
|
+
declare function sortBy<T = any>(collection: ArrayLike<T> | object | null | undefined, ...criteria: Array<Criterion<T> | Array<Criterion<T>>>): T[];
|
|
33
33
|
|
|
34
34
|
export { sortBy };
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { orderBy } from './orderBy.mjs';
|
|
2
|
+
import { flatten } from '../../array/flatten.mjs';
|
|
3
|
+
import { isIterateeCall } from '../_internal/isIterateeCall.mjs';
|
|
2
4
|
|
|
3
|
-
function sortBy(collection, criteria) {
|
|
4
|
-
|
|
5
|
+
function sortBy(collection, ...criteria) {
|
|
6
|
+
const length = criteria.length;
|
|
7
|
+
if (length > 1 && isIterateeCall(collection, criteria[0], criteria[1])) {
|
|
8
|
+
criteria = [];
|
|
9
|
+
}
|
|
10
|
+
else if (length > 2 && isIterateeCall(criteria[0], criteria[1], criteria[2])) {
|
|
11
|
+
criteria = [criteria[0]];
|
|
12
|
+
}
|
|
13
|
+
return orderBy(collection, flatten(criteria), ['asc']);
|
|
5
14
|
}
|
|
6
15
|
|
|
7
16
|
export { sortBy };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -45,6 +45,8 @@ export { partialRight } from '../function/partialRight.mjs';
|
|
|
45
45
|
export { unary } from '../function/unary.mjs';
|
|
46
46
|
export { mean } from '../math/mean.mjs';
|
|
47
47
|
export { meanBy } from '../math/meanBy.mjs';
|
|
48
|
+
export { median } from '../math/median.mjs';
|
|
49
|
+
export { medianBy } from '../math/medianBy.mjs';
|
|
48
50
|
export { randomInt } from '../math/randomInt.mjs';
|
|
49
51
|
export { range } from '../math/range.mjs';
|
|
50
52
|
export { rangeRight } from '../math/rangeRight.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ export { partialRight } from '../function/partialRight.js';
|
|
|
45
45
|
export { unary } from '../function/unary.js';
|
|
46
46
|
export { mean } from '../math/mean.js';
|
|
47
47
|
export { meanBy } from '../math/meanBy.js';
|
|
48
|
+
export { median } from '../math/median.js';
|
|
49
|
+
export { medianBy } from '../math/medianBy.js';
|
|
48
50
|
export { randomInt } from '../math/randomInt.js';
|
|
49
51
|
export { range } from '../math/range.js';
|
|
50
52
|
export { rangeRight } from '../math/rangeRight.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -6,7 +6,7 @@ const zipWith = require('../_chunk/zipWith-Dkv3D1.js');
|
|
|
6
6
|
const promise_index = require('../_chunk/index-BGZDR9.js');
|
|
7
7
|
const unary = require('../_chunk/unary-CMvKXy.js');
|
|
8
8
|
const noop = require('../_chunk/noop-2IwLUk.js');
|
|
9
|
-
const sumBy = require('../_chunk/sumBy-
|
|
9
|
+
const sumBy = require('../_chunk/sumBy-RVppiV.js');
|
|
10
10
|
const randomInt = require('../_chunk/randomInt-CF7bZK.js');
|
|
11
11
|
const toMerged = require('../_chunk/toMerged-wNz52b.js');
|
|
12
12
|
const isPlainObject$1 = require('../_chunk/isPlainObject-octpoD.js');
|
|
@@ -886,16 +886,20 @@ function isKey(value, object) {
|
|
|
886
886
|
(object != null));
|
|
887
887
|
}
|
|
888
888
|
|
|
889
|
-
function orderBy(collection, criteria, orders) {
|
|
890
|
-
if (collection == null
|
|
889
|
+
function orderBy(collection, criteria, orders, guard) {
|
|
890
|
+
if (collection == null) {
|
|
891
891
|
return [];
|
|
892
892
|
}
|
|
893
|
-
|
|
893
|
+
orders = guard ? undefined : orders;
|
|
894
|
+
if (!Array.isArray(collection)) {
|
|
894
895
|
collection = Object.values(collection);
|
|
895
896
|
}
|
|
896
897
|
if (!Array.isArray(criteria)) {
|
|
897
898
|
criteria = criteria == null ? [null] : [criteria];
|
|
898
899
|
}
|
|
900
|
+
if (criteria.length === 0) {
|
|
901
|
+
criteria = [null];
|
|
902
|
+
}
|
|
899
903
|
if (!Array.isArray(orders)) {
|
|
900
904
|
orders = orders == null ? [] : [orders];
|
|
901
905
|
}
|
|
@@ -1051,8 +1055,15 @@ function some(source, predicate, guard) {
|
|
|
1051
1055
|
}
|
|
1052
1056
|
}
|
|
1053
1057
|
|
|
1054
|
-
function sortBy(collection, criteria) {
|
|
1055
|
-
|
|
1058
|
+
function sortBy(collection, ...criteria) {
|
|
1059
|
+
const length = criteria.length;
|
|
1060
|
+
if (length > 1 && isIterateeCall(collection, criteria[0], criteria[1])) {
|
|
1061
|
+
criteria = [];
|
|
1062
|
+
}
|
|
1063
|
+
else if (length > 2 && isIterateeCall(criteria[0], criteria[1], criteria[2])) {
|
|
1064
|
+
criteria = [criteria[0]];
|
|
1065
|
+
}
|
|
1066
|
+
return orderBy(collection, zipWith.flatten(criteria), ['asc']);
|
|
1056
1067
|
}
|
|
1057
1068
|
|
|
1058
1069
|
function tail(arr) {
|
|
@@ -2287,6 +2298,8 @@ exports.unary = unary.unary;
|
|
|
2287
2298
|
exports.noop = noop.noop;
|
|
2288
2299
|
exports.mean = sumBy.mean;
|
|
2289
2300
|
exports.meanBy = sumBy.meanBy;
|
|
2301
|
+
exports.median = sumBy.median;
|
|
2302
|
+
exports.medianBy = sumBy.medianBy;
|
|
2290
2303
|
exports.range = sumBy.range;
|
|
2291
2304
|
exports.rangeRight = sumBy.rangeRight;
|
|
2292
2305
|
exports.sum = sumBy.sum;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -45,6 +45,8 @@ export { partialRight } from '../function/partialRight.mjs';
|
|
|
45
45
|
export { unary } from '../function/unary.mjs';
|
|
46
46
|
export { mean } from '../math/mean.mjs';
|
|
47
47
|
export { meanBy } from '../math/meanBy.mjs';
|
|
48
|
+
export { median } from '../math/median.mjs';
|
|
49
|
+
export { medianBy } from '../math/medianBy.mjs';
|
|
48
50
|
export { randomInt } from '../math/randomInt.mjs';
|
|
49
51
|
export { range } from '../math/range.mjs';
|
|
50
52
|
export { rangeRight } from '../math/rangeRight.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -78,6 +78,8 @@ export { clamp } from './math/clamp.mjs';
|
|
|
78
78
|
export { inRange } from './math/inRange.mjs';
|
|
79
79
|
export { mean } from './math/mean.mjs';
|
|
80
80
|
export { meanBy } from './math/meanBy.mjs';
|
|
81
|
+
export { median } from './math/median.mjs';
|
|
82
|
+
export { medianBy } from './math/medianBy.mjs';
|
|
81
83
|
export { random } from './math/random.mjs';
|
|
82
84
|
export { randomInt } from './math/randomInt.mjs';
|
|
83
85
|
export { range } from './math/range.mjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -78,6 +78,8 @@ export { clamp } from './math/clamp.js';
|
|
|
78
78
|
export { inRange } from './math/inRange.js';
|
|
79
79
|
export { mean } from './math/mean.js';
|
|
80
80
|
export { meanBy } from './math/meanBy.js';
|
|
81
|
+
export { median } from './math/median.js';
|
|
82
|
+
export { medianBy } from './math/medianBy.js';
|
|
81
83
|
export { random } from './math/random.js';
|
|
82
84
|
export { randomInt } from './math/randomInt.js';
|
|
83
85
|
export { range } from './math/range.js';
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const promise_index = require('./_chunk/index-BGZDR9.js');
|
|
|
8
8
|
const unary = require('./_chunk/unary-CMvKXy.js');
|
|
9
9
|
const function_index = require('./function/index.js');
|
|
10
10
|
const noop = require('./_chunk/noop-2IwLUk.js');
|
|
11
|
-
const sumBy = require('./_chunk/sumBy-
|
|
11
|
+
const sumBy = require('./_chunk/sumBy-RVppiV.js');
|
|
12
12
|
const randomInt = require('./_chunk/randomInt-CF7bZK.js');
|
|
13
13
|
const math_index = require('./math/index.js');
|
|
14
14
|
const toMerged = require('./_chunk/toMerged-wNz52b.js');
|
|
@@ -104,6 +104,8 @@ exports.clamp = sumBy.clamp;
|
|
|
104
104
|
exports.inRange = sumBy.inRange;
|
|
105
105
|
exports.mean = sumBy.mean;
|
|
106
106
|
exports.meanBy = sumBy.meanBy;
|
|
107
|
+
exports.median = sumBy.median;
|
|
108
|
+
exports.medianBy = sumBy.medianBy;
|
|
107
109
|
exports.range = sumBy.range;
|
|
108
110
|
exports.rangeRight = sumBy.rangeRight;
|
|
109
111
|
exports.sum = sumBy.sum;
|
package/dist/index.mjs
CHANGED
|
@@ -78,6 +78,8 @@ export { clamp } from './math/clamp.mjs';
|
|
|
78
78
|
export { inRange } from './math/inRange.mjs';
|
|
79
79
|
export { mean } from './math/mean.mjs';
|
|
80
80
|
export { meanBy } from './math/meanBy.mjs';
|
|
81
|
+
export { median } from './math/median.mjs';
|
|
82
|
+
export { medianBy } from './math/medianBy.mjs';
|
|
81
83
|
export { random } from './math/random.mjs';
|
|
82
84
|
export { randomInt } from './math/randomInt.mjs';
|
|
83
85
|
export { range } from './math/range.mjs';
|
package/dist/math/index.d.mts
CHANGED
|
@@ -2,6 +2,8 @@ export { clamp } from './clamp.mjs';
|
|
|
2
2
|
export { inRange } from './inRange.mjs';
|
|
3
3
|
export { mean } from './mean.mjs';
|
|
4
4
|
export { meanBy } from './meanBy.mjs';
|
|
5
|
+
export { median } from './median.mjs';
|
|
6
|
+
export { medianBy } from './medianBy.mjs';
|
|
5
7
|
export { random } from './random.mjs';
|
|
6
8
|
export { randomInt } from './randomInt.mjs';
|
|
7
9
|
export { range } from './range.mjs';
|
package/dist/math/index.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ export { clamp } from './clamp.js';
|
|
|
2
2
|
export { inRange } from './inRange.js';
|
|
3
3
|
export { mean } from './mean.js';
|
|
4
4
|
export { meanBy } from './meanBy.js';
|
|
5
|
+
export { median } from './median.js';
|
|
6
|
+
export { medianBy } from './medianBy.js';
|
|
5
7
|
export { random } from './random.js';
|
|
6
8
|
export { randomInt } from './randomInt.js';
|
|
7
9
|
export { range } from './range.js';
|
package/dist/math/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const sumBy = require('../_chunk/sumBy-
|
|
5
|
+
const sumBy = require('../_chunk/sumBy-RVppiV.js');
|
|
6
6
|
const randomInt = require('../_chunk/randomInt-CF7bZK.js');
|
|
7
7
|
|
|
8
8
|
function round(value, precision = 0) {
|
|
@@ -17,6 +17,8 @@ exports.clamp = sumBy.clamp;
|
|
|
17
17
|
exports.inRange = sumBy.inRange;
|
|
18
18
|
exports.mean = sumBy.mean;
|
|
19
19
|
exports.meanBy = sumBy.meanBy;
|
|
20
|
+
exports.median = sumBy.median;
|
|
21
|
+
exports.medianBy = sumBy.medianBy;
|
|
20
22
|
exports.range = sumBy.range;
|
|
21
23
|
exports.rangeRight = sumBy.rangeRight;
|
|
22
24
|
exports.sum = sumBy.sum;
|
package/dist/math/index.mjs
CHANGED
|
@@ -2,6 +2,8 @@ export { clamp } from './clamp.mjs';
|
|
|
2
2
|
export { inRange } from './inRange.mjs';
|
|
3
3
|
export { mean } from './mean.mjs';
|
|
4
4
|
export { meanBy } from './meanBy.mjs';
|
|
5
|
+
export { median } from './median.mjs';
|
|
6
|
+
export { medianBy } from './medianBy.mjs';
|
|
5
7
|
export { random } from './random.mjs';
|
|
6
8
|
export { randomInt } from './randomInt.mjs';
|
|
7
9
|
export { range } from './range.mjs';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the median of an array of numbers.
|
|
3
|
+
*
|
|
4
|
+
* The median is the middle value of a sorted array.
|
|
5
|
+
* If the array has an odd number of elements, the median is the middle value.
|
|
6
|
+
* If the array has an even number of elements, it returns the average of the two middle values.
|
|
7
|
+
*
|
|
8
|
+
* If the array is empty, this function returns `NaN`.
|
|
9
|
+
*
|
|
10
|
+
* @param {number[]} nums - An array of numbers to calculate the median.
|
|
11
|
+
* @returns {number} The median of all the numbers in the array.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const arrayWithOddNumberOfElements = [1, 2, 3, 4, 5];
|
|
15
|
+
* const result = median(arrayWithOddNumberOfElements);
|
|
16
|
+
* // result will be 3
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const arrayWithEvenNumberOfElements = [1, 2, 3, 4];
|
|
20
|
+
* const result = median(arrayWithEvenNumberOfElements);
|
|
21
|
+
* // result will be 2.5
|
|
22
|
+
*/
|
|
23
|
+
declare function median(nums: readonly number[]): number;
|
|
24
|
+
|
|
25
|
+
export { median };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the median of an array of numbers.
|
|
3
|
+
*
|
|
4
|
+
* The median is the middle value of a sorted array.
|
|
5
|
+
* If the array has an odd number of elements, the median is the middle value.
|
|
6
|
+
* If the array has an even number of elements, it returns the average of the two middle values.
|
|
7
|
+
*
|
|
8
|
+
* If the array is empty, this function returns `NaN`.
|
|
9
|
+
*
|
|
10
|
+
* @param {number[]} nums - An array of numbers to calculate the median.
|
|
11
|
+
* @returns {number} The median of all the numbers in the array.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const arrayWithOddNumberOfElements = [1, 2, 3, 4, 5];
|
|
15
|
+
* const result = median(arrayWithOddNumberOfElements);
|
|
16
|
+
* // result will be 3
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const arrayWithEvenNumberOfElements = [1, 2, 3, 4];
|
|
20
|
+
* const result = median(arrayWithEvenNumberOfElements);
|
|
21
|
+
* // result will be 2.5
|
|
22
|
+
*/
|
|
23
|
+
declare function median(nums: readonly number[]): number;
|
|
24
|
+
|
|
25
|
+
export { median };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function median(nums) {
|
|
2
|
+
if (nums.length === 0) {
|
|
3
|
+
return NaN;
|
|
4
|
+
}
|
|
5
|
+
const sorted = nums.slice().sort((a, b) => a - b);
|
|
6
|
+
const middleIndex = Math.floor(sorted.length / 2);
|
|
7
|
+
if (sorted.length % 2 === 0) {
|
|
8
|
+
return (sorted[middleIndex - 1] + sorted[middleIndex]) / 2;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return sorted[middleIndex];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { median };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the median of an array of elements when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
4
|
+
*
|
|
5
|
+
* The median is the middle value of a sorted array.
|
|
6
|
+
* If the array has an odd number of elements, the median is the middle value.
|
|
7
|
+
* If the array has an even number of elements, it returns the average of the two middle values.
|
|
8
|
+
*
|
|
9
|
+
* If the array is empty, this function returns `NaN`.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements in the array.
|
|
12
|
+
* @param {T[]} items An array to calculate the median.
|
|
13
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
14
|
+
* @returns {number} The median of all the numbers as determined by the `getValue` function.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* medianBy([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], x => x.a); // Returns: 3
|
|
18
|
+
* medianBy([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }], x => x.a); // Returns: 2.5
|
|
19
|
+
* medianBy([], x => x.a); // Returns: NaN
|
|
20
|
+
*/
|
|
21
|
+
declare function medianBy<T>(items: readonly T[], getValue: (element: T) => number): number;
|
|
22
|
+
|
|
23
|
+
export { medianBy };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the median of an array of elements when applying
|
|
3
|
+
* the `getValue` function to each element.
|
|
4
|
+
*
|
|
5
|
+
* The median is the middle value of a sorted array.
|
|
6
|
+
* If the array has an odd number of elements, the median is the middle value.
|
|
7
|
+
* If the array has an even number of elements, it returns the average of the two middle values.
|
|
8
|
+
*
|
|
9
|
+
* If the array is empty, this function returns `NaN`.
|
|
10
|
+
*
|
|
11
|
+
* @template T - The type of elements in the array.
|
|
12
|
+
* @param {T[]} items An array to calculate the median.
|
|
13
|
+
* @param {(element: T) => number} getValue A function that selects a numeric value from each element.
|
|
14
|
+
* @returns {number} The median of all the numbers as determined by the `getValue` function.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* medianBy([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], x => x.a); // Returns: 3
|
|
18
|
+
* medianBy([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }], x => x.a); // Returns: 2.5
|
|
19
|
+
* medianBy([], x => x.a); // Returns: NaN
|
|
20
|
+
*/
|
|
21
|
+
declare function medianBy<T>(items: readonly T[], getValue: (element: T) => number): number;
|
|
22
|
+
|
|
23
|
+
export { medianBy };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.25.2-dev.
|
|
4
|
+
"version": "1.25.2-dev.809+e71f7064",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|