es-toolkit 1.17.0-dev.526 → 1.17.0-dev.528
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/initial-crEecP.js +513 -0
- package/dist/_chunk/{isTypedArray-BBEkFl.js → isTypedArray-Dsrnb1.js} +1 -11
- package/dist/_chunk/{isWeakSet-BRIIbG.js → isWeakSet-CogETi.js} +1 -1
- package/dist/_chunk/{toMerged-BGwYW5.js → toMerged-DN1PPP.js} +1 -1
- package/dist/array/index.js +56 -510
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/_internal/compareValues.mjs +30 -5
- package/dist/compat/array/orderBy.d.mts +6 -6
- package/dist/compat/array/orderBy.d.ts +6 -6
- package/dist/compat/array/orderBy.mjs +26 -18
- package/dist/compat/array/sortBy.d.mts +34 -0
- package/dist/compat/array/sortBy.d.ts +34 -0
- package/dist/compat/array/sortBy.mjs +7 -0
- package/dist/compat/index.d.mts +1 -1
- package/dist/compat/index.d.ts +1 -1
- package/dist/compat/index.js +122 -85
- package/dist/compat/index.mjs +1 -1
- package/dist/index.js +59 -58
- package/dist/object/index.js +1 -1
- package/dist/predicate/index.js +2 -2
- package/dist/predicate/isTypedArray.mjs +1 -11
- package/package.json +1 -1
|
@@ -1,9 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
if (
|
|
3
|
-
return
|
|
1
|
+
function getPriority(a) {
|
|
2
|
+
if (typeof a === 'symbol') {
|
|
3
|
+
return 1;
|
|
4
|
+
}
|
|
5
|
+
if (a === null) {
|
|
6
|
+
return 2;
|
|
7
|
+
}
|
|
8
|
+
if (a === undefined) {
|
|
9
|
+
return 3;
|
|
4
10
|
}
|
|
5
|
-
if (
|
|
6
|
-
return
|
|
11
|
+
if (a !== a) {
|
|
12
|
+
return 4;
|
|
13
|
+
}
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
const compareValues = (a, b, order) => {
|
|
17
|
+
if (a !== b) {
|
|
18
|
+
if (typeof a === 'string' && typeof b === 'string') {
|
|
19
|
+
return order === 'desc' ? b.localeCompare(a) : a.localeCompare(b);
|
|
20
|
+
}
|
|
21
|
+
const aPriority = getPriority(a);
|
|
22
|
+
const bPriority = getPriority(b);
|
|
23
|
+
if (aPriority === bPriority && aPriority === 0) {
|
|
24
|
+
if (a < b) {
|
|
25
|
+
return order === 'desc' ? 1 : -1;
|
|
26
|
+
}
|
|
27
|
+
if (a > b) {
|
|
28
|
+
return order === 'desc' ? -1 : 1;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return order === 'desc' ? bPriority - aPriority : aPriority - bPriority;
|
|
7
32
|
}
|
|
8
33
|
return 0;
|
|
9
34
|
};
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null | undefined;
|
|
1
2
|
/**
|
|
2
3
|
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
3
4
|
*
|
|
4
5
|
* This function takes an array of objects, an array of criteria to sort by, and an array of order directions.
|
|
5
|
-
* It returns the sorted array, ordering by each key according to its corresponding direction
|
|
6
|
-
*
|
|
7
|
-
* it moves to the next key to determine the order.
|
|
6
|
+
* It returns the sorted array, ordering by each key according to its corresponding direction ('asc' for ascending or 'desc' for descending).
|
|
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
10
|
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
|
-
* @param {
|
|
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
13
|
* @returns {T[]} - The sorted array.
|
|
14
14
|
*
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
* // { user: 'fred', age: 40 },
|
|
30
30
|
* // ]
|
|
31
31
|
*/
|
|
32
|
-
declare function orderBy<T>(collection: T[] | object | null | undefined, criteria?:
|
|
32
|
+
declare function orderBy<T>(collection: T[] | object | number | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>, orders?: unknown | unknown[]): T[];
|
|
33
33
|
|
|
34
|
-
export { orderBy };
|
|
34
|
+
export { type Criterion, orderBy };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
+
type Criterion<T> = ((item: T) => unknown) | PropertyKey | PropertyKey[] | null | undefined;
|
|
1
2
|
/**
|
|
2
3
|
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
3
4
|
*
|
|
4
5
|
* This function takes an array of objects, an array of criteria to sort by, and an array of order directions.
|
|
5
|
-
* It returns the sorted array, ordering by each key according to its corresponding direction
|
|
6
|
-
*
|
|
7
|
-
* it moves to the next key to determine the order.
|
|
6
|
+
* It returns the sorted array, ordering by each key according to its corresponding direction ('asc' for ascending or 'desc' for descending).
|
|
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
10
|
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
|
-
* @param {
|
|
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
13
|
* @returns {T[]} - The sorted array.
|
|
14
14
|
*
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
* // { user: 'fred', age: 40 },
|
|
30
30
|
* // ]
|
|
31
31
|
*/
|
|
32
|
-
declare function orderBy<T>(collection: T[] | object | null | undefined, criteria?:
|
|
32
|
+
declare function orderBy<T>(collection: T[] | object | number | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>, orders?: unknown | unknown[]): T[];
|
|
33
33
|
|
|
34
|
-
export { orderBy };
|
|
34
|
+
export { type Criterion, orderBy };
|
|
@@ -3,64 +3,72 @@ import { isKey } from '../_internal/isKey.mjs';
|
|
|
3
3
|
import { toPath } from '../_internal/toPath.mjs';
|
|
4
4
|
|
|
5
5
|
function orderBy(collection, criteria, orders) {
|
|
6
|
-
if (collection == null) {
|
|
6
|
+
if (collection == null || typeof collection === 'number') {
|
|
7
7
|
return [];
|
|
8
8
|
}
|
|
9
|
-
if (
|
|
9
|
+
if (typeof collection === 'object' && !Array.isArray(collection)) {
|
|
10
10
|
collection = Object.values(collection);
|
|
11
11
|
}
|
|
12
12
|
if (!Array.isArray(criteria)) {
|
|
13
|
-
criteria = criteria == null ? [] : [criteria];
|
|
13
|
+
criteria = criteria == null ? [null] : [criteria];
|
|
14
14
|
}
|
|
15
15
|
if (!Array.isArray(orders)) {
|
|
16
16
|
orders = orders == null ? [] : [orders];
|
|
17
17
|
}
|
|
18
|
+
orders = orders.map(order => String(order));
|
|
18
19
|
const getValueByNestedPath = (object, path) => {
|
|
19
20
|
let target = object;
|
|
20
|
-
for (let i = 0; i < path.length && target != null; i
|
|
21
|
+
for (let i = 0; i < path.length && target != null; ++i) {
|
|
21
22
|
target = target[path[i]];
|
|
22
23
|
}
|
|
23
24
|
return target;
|
|
24
25
|
};
|
|
25
26
|
const getValueByCriterion = (criterion, object) => {
|
|
26
|
-
if (object == null) {
|
|
27
|
+
if (object == null || criterion == null) {
|
|
27
28
|
return object;
|
|
28
29
|
}
|
|
30
|
+
if (typeof criterion === 'object' && 'key' in criterion) {
|
|
31
|
+
if (Object.hasOwn(object, criterion.key)) {
|
|
32
|
+
return object[criterion.key];
|
|
33
|
+
}
|
|
34
|
+
return getValueByNestedPath(object, criterion.path);
|
|
35
|
+
}
|
|
29
36
|
if (typeof criterion === 'function') {
|
|
30
37
|
return criterion(object);
|
|
31
38
|
}
|
|
32
39
|
if (Array.isArray(criterion)) {
|
|
33
40
|
return getValueByNestedPath(object, criterion);
|
|
34
41
|
}
|
|
35
|
-
if (typeof
|
|
42
|
+
if (typeof object === 'object') {
|
|
36
43
|
return object[criterion];
|
|
37
44
|
}
|
|
38
|
-
|
|
39
|
-
return object[criterion.key];
|
|
40
|
-
}
|
|
41
|
-
return getValueByNestedPath(object, criterion.path);
|
|
45
|
+
return object;
|
|
42
46
|
};
|
|
43
47
|
const preparedCriteria = criteria.map(criterion => {
|
|
44
48
|
if (Array.isArray(criterion) && criterion.length === 1) {
|
|
45
49
|
criterion = criterion[0];
|
|
46
50
|
}
|
|
47
|
-
if (typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
51
|
+
if (criterion == null || typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
48
52
|
return criterion;
|
|
49
53
|
}
|
|
50
54
|
return { key: criterion, path: toPath(criterion) };
|
|
51
55
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const preparedCollection = collection.map(item => ({
|
|
57
|
+
original: item,
|
|
58
|
+
criteria: preparedCriteria.map(criterion => getValueByCriterion(criterion, item)),
|
|
59
|
+
}));
|
|
60
|
+
return preparedCollection
|
|
61
|
+
.slice()
|
|
62
|
+
.sort((a, b) => {
|
|
63
|
+
for (let i = 0; i < preparedCriteria.length; i++) {
|
|
64
|
+
const comparedResult = compareValues(a.criteria[i], b.criteria[i], orders[i]);
|
|
58
65
|
if (comparedResult !== 0) {
|
|
59
66
|
return comparedResult;
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
69
|
return 0;
|
|
63
|
-
})
|
|
70
|
+
})
|
|
71
|
+
.map(item => item.original);
|
|
64
72
|
}
|
|
65
73
|
|
|
66
74
|
export { orderBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Criterion } from './orderBy.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
5
|
+
*
|
|
6
|
+
* This function takes an array of objects, an array of criteria to sort by.
|
|
7
|
+
* It returns the ascending sorted array, ordering by each key.
|
|
8
|
+
* If values for a key are equal, it moves to the next key to determine the order.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements in the array.
|
|
11
|
+
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
12
|
+
* @param {Criterion<T> | Array<Criterion<T>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
13
|
+
* @returns {T[]} - The ascending sorted array.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Sort an array of objects by 'user' in ascending order and 'age' in descending order.
|
|
17
|
+
* const users = [
|
|
18
|
+
* { user: 'fred', age: 48 },
|
|
19
|
+
* { user: 'barney', age: 34 },
|
|
20
|
+
* { user: 'fred', age: 40 },
|
|
21
|
+
* { user: 'barney', age: 36 },
|
|
22
|
+
* ];
|
|
23
|
+
* const result = sortBy(users, ['user', (item) => item.age])
|
|
24
|
+
* // result will be:
|
|
25
|
+
* // [
|
|
26
|
+
* // { user: 'barney', age: 34 },
|
|
27
|
+
* // { user: 'barney', age: 36 },
|
|
28
|
+
* // { user: 'fred', age: 40 },
|
|
29
|
+
* // { user: 'fred', age: 48 },
|
|
30
|
+
* // ]
|
|
31
|
+
*/
|
|
32
|
+
declare function sortBy<T>(collection: T[] | object | number | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>): T[];
|
|
33
|
+
|
|
34
|
+
export { sortBy };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Criterion } from './orderBy.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
5
|
+
*
|
|
6
|
+
* This function takes an array of objects, an array of criteria to sort by.
|
|
7
|
+
* It returns the ascending sorted array, ordering by each key.
|
|
8
|
+
* If values for a key are equal, it moves to the next key to determine the order.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of elements in the array.
|
|
11
|
+
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
12
|
+
* @param {Criterion<T> | Array<Criterion<T>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
|
|
13
|
+
* @returns {T[]} - The ascending sorted array.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* // Sort an array of objects by 'user' in ascending order and 'age' in descending order.
|
|
17
|
+
* const users = [
|
|
18
|
+
* { user: 'fred', age: 48 },
|
|
19
|
+
* { user: 'barney', age: 34 },
|
|
20
|
+
* { user: 'fred', age: 40 },
|
|
21
|
+
* { user: 'barney', age: 36 },
|
|
22
|
+
* ];
|
|
23
|
+
* const result = sortBy(users, ['user', (item) => item.age])
|
|
24
|
+
* // result will be:
|
|
25
|
+
* // [
|
|
26
|
+
* // { user: 'barney', age: 34 },
|
|
27
|
+
* // { user: 'barney', age: 36 },
|
|
28
|
+
* // { user: 'fred', age: 40 },
|
|
29
|
+
* // { user: 'fred', age: 48 },
|
|
30
|
+
* // ]
|
|
31
|
+
*/
|
|
32
|
+
declare function sortBy<T>(collection: T[] | object | number | null | undefined, criteria?: Criterion<T> | Array<Criterion<T>>): T[];
|
|
33
|
+
|
|
34
|
+
export { sortBy };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -20,7 +20,6 @@ export { maxBy } from '../array/maxBy.mjs';
|
|
|
20
20
|
export { minBy } from '../array/minBy.mjs';
|
|
21
21
|
export { partition } from '../array/partition.mjs';
|
|
22
22
|
export { pullAt } from '../array/pullAt.mjs';
|
|
23
|
-
export { sortBy } from '../array/sortBy.mjs';
|
|
24
23
|
export { sample } from '../array/sample.mjs';
|
|
25
24
|
export { sampleSize } from '../array/sampleSize.mjs';
|
|
26
25
|
export { shuffle } from '../array/shuffle.mjs';
|
|
@@ -115,6 +114,7 @@ export { flatten } from './array/flatten.mjs';
|
|
|
115
114
|
export { flattenDeep } from './array/flattenDeep.mjs';
|
|
116
115
|
export { flattenDepth } from './array/flattenDepth.mjs';
|
|
117
116
|
export { orderBy } from './array/orderBy.mjs';
|
|
117
|
+
export { sortBy } from './array/sortBy.mjs';
|
|
118
118
|
export { size } from './array/size.mjs';
|
|
119
119
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
120
120
|
export { indexOf } from './array/indexOf.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -20,7 +20,6 @@ export { maxBy } from '../array/maxBy.js';
|
|
|
20
20
|
export { minBy } from '../array/minBy.js';
|
|
21
21
|
export { partition } from '../array/partition.js';
|
|
22
22
|
export { pullAt } from '../array/pullAt.js';
|
|
23
|
-
export { sortBy } from '../array/sortBy.js';
|
|
24
23
|
export { sample } from '../array/sample.js';
|
|
25
24
|
export { sampleSize } from '../array/sampleSize.js';
|
|
26
25
|
export { shuffle } from '../array/shuffle.js';
|
|
@@ -115,6 +114,7 @@ export { flatten } from './array/flatten.js';
|
|
|
115
114
|
export { flattenDeep } from './array/flattenDeep.js';
|
|
116
115
|
export { flattenDepth } from './array/flattenDepth.js';
|
|
117
116
|
export { orderBy } from './array/orderBy.js';
|
|
117
|
+
export { sortBy } from './array/sortBy.js';
|
|
118
118
|
export { size } from './array/size.js';
|
|
119
119
|
export { zipObjectDeep } from './array/zipObjectDeep.js';
|
|
120
120
|
export { indexOf } from './array/indexOf.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const initial = require('../_chunk/initial-crEecP.js');
|
|
6
6
|
const promise_index = require('../_chunk/index-BGZDR9.js');
|
|
7
7
|
const rest$1 = require('../_chunk/rest-Bzm2XK.js');
|
|
8
8
|
const math_index = require('../math/index.js');
|
|
9
9
|
const randomInt = require('../_chunk/randomInt-CF7bZK.js');
|
|
10
|
-
const toMerged = require('../_chunk/toMerged-
|
|
11
|
-
const isWeakSet$1 = require('../_chunk/isWeakSet-
|
|
12
|
-
const isTypedArray$1 = require('../_chunk/isTypedArray-
|
|
10
|
+
const toMerged = require('../_chunk/toMerged-DN1PPP.js');
|
|
11
|
+
const isWeakSet$1 = require('../_chunk/isWeakSet-CogETi.js');
|
|
12
|
+
const isTypedArray$1 = require('../_chunk/isTypedArray-Dsrnb1.js');
|
|
13
13
|
const string_index = require('../string/index.js');
|
|
14
14
|
|
|
15
15
|
function chunk(arr, size = 1) {
|
|
@@ -17,17 +17,17 @@ function chunk(arr, size = 1) {
|
|
|
17
17
|
if (size === 0) {
|
|
18
18
|
return [];
|
|
19
19
|
}
|
|
20
|
-
return
|
|
20
|
+
return initial.chunk(arr, size);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function concat(...values) {
|
|
24
|
-
return
|
|
24
|
+
return initial.flatten(values);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
function difference(arr, ...values) {
|
|
28
28
|
const arr1 = arr;
|
|
29
|
-
const arr2 =
|
|
30
|
-
return
|
|
29
|
+
const arr2 = initial.flatten(values);
|
|
30
|
+
return initial.difference(arr1, arr2);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function fill(array, value, start = 0, end = array.length) {
|
|
@@ -39,7 +39,7 @@ function fill(array, value, start = 0, end = array.length) {
|
|
|
39
39
|
if (!end) {
|
|
40
40
|
end = 0;
|
|
41
41
|
}
|
|
42
|
-
return
|
|
42
|
+
return initial.fill(array, value, start, end);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const IS_PLAIN = /^\w*$/;
|
|
@@ -426,12 +426,37 @@ function flattenDepth(value, depth = 1) {
|
|
|
426
426
|
return flatten(value, depth);
|
|
427
427
|
}
|
|
428
428
|
|
|
429
|
-
|
|
430
|
-
if (
|
|
431
|
-
return
|
|
429
|
+
function getPriority(a) {
|
|
430
|
+
if (typeof a === 'symbol') {
|
|
431
|
+
return 1;
|
|
432
|
+
}
|
|
433
|
+
if (a === null) {
|
|
434
|
+
return 2;
|
|
435
|
+
}
|
|
436
|
+
if (a === undefined) {
|
|
437
|
+
return 3;
|
|
432
438
|
}
|
|
433
|
-
if (
|
|
434
|
-
return
|
|
439
|
+
if (a !== a) {
|
|
440
|
+
return 4;
|
|
441
|
+
}
|
|
442
|
+
return 0;
|
|
443
|
+
}
|
|
444
|
+
const compareValues = (a, b, order) => {
|
|
445
|
+
if (a !== b) {
|
|
446
|
+
if (typeof a === 'string' && typeof b === 'string') {
|
|
447
|
+
return order === 'desc' ? b.localeCompare(a) : a.localeCompare(b);
|
|
448
|
+
}
|
|
449
|
+
const aPriority = getPriority(a);
|
|
450
|
+
const bPriority = getPriority(b);
|
|
451
|
+
if (aPriority === bPriority && aPriority === 0) {
|
|
452
|
+
if (a < b) {
|
|
453
|
+
return order === 'desc' ? 1 : -1;
|
|
454
|
+
}
|
|
455
|
+
if (a > b) {
|
|
456
|
+
return order === 'desc' ? -1 : 1;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return order === 'desc' ? bPriority - aPriority : aPriority - bPriority;
|
|
435
460
|
}
|
|
436
461
|
return 0;
|
|
437
462
|
};
|
|
@@ -450,64 +475,76 @@ function isKey(value, object) {
|
|
|
450
475
|
}
|
|
451
476
|
|
|
452
477
|
function orderBy(collection, criteria, orders) {
|
|
453
|
-
if (collection == null) {
|
|
478
|
+
if (collection == null || typeof collection === 'number') {
|
|
454
479
|
return [];
|
|
455
480
|
}
|
|
456
|
-
if (
|
|
481
|
+
if (typeof collection === 'object' && !Array.isArray(collection)) {
|
|
457
482
|
collection = Object.values(collection);
|
|
458
483
|
}
|
|
459
484
|
if (!Array.isArray(criteria)) {
|
|
460
|
-
criteria = criteria == null ? [] : [criteria];
|
|
485
|
+
criteria = criteria == null ? [null] : [criteria];
|
|
461
486
|
}
|
|
462
487
|
if (!Array.isArray(orders)) {
|
|
463
488
|
orders = orders == null ? [] : [orders];
|
|
464
489
|
}
|
|
490
|
+
orders = orders.map(order => String(order));
|
|
465
491
|
const getValueByNestedPath = (object, path) => {
|
|
466
492
|
let target = object;
|
|
467
|
-
for (let i = 0; i < path.length && target != null; i
|
|
493
|
+
for (let i = 0; i < path.length && target != null; ++i) {
|
|
468
494
|
target = target[path[i]];
|
|
469
495
|
}
|
|
470
496
|
return target;
|
|
471
497
|
};
|
|
472
498
|
const getValueByCriterion = (criterion, object) => {
|
|
473
|
-
if (object == null) {
|
|
499
|
+
if (object == null || criterion == null) {
|
|
474
500
|
return object;
|
|
475
501
|
}
|
|
502
|
+
if (typeof criterion === 'object' && 'key' in criterion) {
|
|
503
|
+
if (Object.hasOwn(object, criterion.key)) {
|
|
504
|
+
return object[criterion.key];
|
|
505
|
+
}
|
|
506
|
+
return getValueByNestedPath(object, criterion.path);
|
|
507
|
+
}
|
|
476
508
|
if (typeof criterion === 'function') {
|
|
477
509
|
return criterion(object);
|
|
478
510
|
}
|
|
479
511
|
if (Array.isArray(criterion)) {
|
|
480
512
|
return getValueByNestedPath(object, criterion);
|
|
481
513
|
}
|
|
482
|
-
if (typeof
|
|
514
|
+
if (typeof object === 'object') {
|
|
483
515
|
return object[criterion];
|
|
484
516
|
}
|
|
485
|
-
|
|
486
|
-
return object[criterion.key];
|
|
487
|
-
}
|
|
488
|
-
return getValueByNestedPath(object, criterion.path);
|
|
517
|
+
return object;
|
|
489
518
|
};
|
|
490
519
|
const preparedCriteria = criteria.map(criterion => {
|
|
491
520
|
if (Array.isArray(criterion) && criterion.length === 1) {
|
|
492
521
|
criterion = criterion[0];
|
|
493
522
|
}
|
|
494
|
-
if (typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
523
|
+
if (criterion == null || typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
495
524
|
return criterion;
|
|
496
525
|
}
|
|
497
526
|
return { key: criterion, path: toPath(criterion) };
|
|
498
527
|
});
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
528
|
+
const preparedCollection = collection.map(item => ({
|
|
529
|
+
original: item,
|
|
530
|
+
criteria: preparedCriteria.map(criterion => getValueByCriterion(criterion, item)),
|
|
531
|
+
}));
|
|
532
|
+
return preparedCollection
|
|
533
|
+
.slice()
|
|
534
|
+
.sort((a, b) => {
|
|
535
|
+
for (let i = 0; i < preparedCriteria.length; i++) {
|
|
536
|
+
const comparedResult = compareValues(a.criteria[i], b.criteria[i], orders[i]);
|
|
505
537
|
if (comparedResult !== 0) {
|
|
506
538
|
return comparedResult;
|
|
507
539
|
}
|
|
508
540
|
}
|
|
509
541
|
return 0;
|
|
510
|
-
})
|
|
542
|
+
})
|
|
543
|
+
.map(item => item.original);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function sortBy(collection, criteria) {
|
|
547
|
+
return orderBy(collection, criteria, ['asc']);
|
|
511
548
|
}
|
|
512
549
|
|
|
513
550
|
function size(target) {
|
|
@@ -538,7 +575,7 @@ function set(obj, path, value) {
|
|
|
538
575
|
|
|
539
576
|
function zipObjectDeep(keys, values) {
|
|
540
577
|
const result = {};
|
|
541
|
-
const zipped =
|
|
578
|
+
const zipped = initial.zip(keys, values);
|
|
542
579
|
for (let i = 0; i < zipped.length; i++) {
|
|
543
580
|
const [key, value] = zipped[i];
|
|
544
581
|
if (key != null) {
|
|
@@ -844,58 +881,57 @@ function min(items = []) {
|
|
|
844
881
|
return minElement;
|
|
845
882
|
}
|
|
846
883
|
|
|
847
|
-
exports.at =
|
|
848
|
-
exports.compact =
|
|
849
|
-
exports.countBy =
|
|
850
|
-
exports.differenceBy =
|
|
851
|
-
exports.differenceWith =
|
|
852
|
-
exports.drop =
|
|
853
|
-
exports.dropRight =
|
|
854
|
-
exports.dropRightWhile =
|
|
855
|
-
exports.dropWhile =
|
|
856
|
-
exports.first =
|
|
857
|
-
exports.flatMap =
|
|
858
|
-
exports.flatMapDeep =
|
|
859
|
-
exports.forEachRight =
|
|
860
|
-
exports.groupBy =
|
|
861
|
-
exports.head =
|
|
862
|
-
exports.initial =
|
|
863
|
-
exports.intersection =
|
|
864
|
-
exports.intersectionBy =
|
|
865
|
-
exports.intersectionWith =
|
|
866
|
-
exports.isSubset =
|
|
867
|
-
exports.join =
|
|
868
|
-
exports.keyBy =
|
|
869
|
-
exports.last =
|
|
870
|
-
exports.maxBy =
|
|
871
|
-
exports.minBy =
|
|
872
|
-
exports.partition =
|
|
873
|
-
exports.pullAt =
|
|
874
|
-
exports.sample =
|
|
875
|
-
exports.sampleSize =
|
|
876
|
-
exports.shuffle =
|
|
877
|
-
exports.
|
|
878
|
-
exports.
|
|
879
|
-
exports.
|
|
880
|
-
exports.
|
|
881
|
-
exports.
|
|
882
|
-
exports.
|
|
883
|
-
exports.
|
|
884
|
-
exports.
|
|
885
|
-
exports.
|
|
886
|
-
exports.
|
|
887
|
-
exports.
|
|
888
|
-
exports.
|
|
889
|
-
exports.
|
|
890
|
-
exports.
|
|
891
|
-
exports.
|
|
892
|
-
exports.
|
|
893
|
-
exports.
|
|
894
|
-
exports.
|
|
895
|
-
exports.
|
|
896
|
-
exports.
|
|
897
|
-
exports.
|
|
898
|
-
exports.zipWith = array_index.zipWith;
|
|
884
|
+
exports.at = initial.at;
|
|
885
|
+
exports.compact = initial.compact;
|
|
886
|
+
exports.countBy = initial.countBy;
|
|
887
|
+
exports.differenceBy = initial.differenceBy;
|
|
888
|
+
exports.differenceWith = initial.differenceWith;
|
|
889
|
+
exports.drop = initial.drop;
|
|
890
|
+
exports.dropRight = initial.dropRight;
|
|
891
|
+
exports.dropRightWhile = initial.dropRightWhile;
|
|
892
|
+
exports.dropWhile = initial.dropWhile;
|
|
893
|
+
exports.first = initial.head;
|
|
894
|
+
exports.flatMap = initial.flatMap;
|
|
895
|
+
exports.flatMapDeep = initial.flatMapDeep;
|
|
896
|
+
exports.forEachRight = initial.forEachRight;
|
|
897
|
+
exports.groupBy = initial.groupBy;
|
|
898
|
+
exports.head = initial.head;
|
|
899
|
+
exports.initial = initial.initial;
|
|
900
|
+
exports.intersection = initial.intersection;
|
|
901
|
+
exports.intersectionBy = initial.intersectionBy;
|
|
902
|
+
exports.intersectionWith = initial.intersectionWith;
|
|
903
|
+
exports.isSubset = initial.isSubset;
|
|
904
|
+
exports.join = initial.join;
|
|
905
|
+
exports.keyBy = initial.keyBy;
|
|
906
|
+
exports.last = initial.last;
|
|
907
|
+
exports.maxBy = initial.maxBy;
|
|
908
|
+
exports.minBy = initial.minBy;
|
|
909
|
+
exports.partition = initial.partition;
|
|
910
|
+
exports.pullAt = initial.pullAt;
|
|
911
|
+
exports.sample = initial.sample;
|
|
912
|
+
exports.sampleSize = initial.sampleSize;
|
|
913
|
+
exports.shuffle = initial.shuffle;
|
|
914
|
+
exports.tail = initial.tail;
|
|
915
|
+
exports.take = initial.take;
|
|
916
|
+
exports.takeRight = initial.takeRight;
|
|
917
|
+
exports.takeRightWhile = initial.takeRightWhile;
|
|
918
|
+
exports.takeWhile = initial.takeWhile;
|
|
919
|
+
exports.toFilled = initial.toFilled;
|
|
920
|
+
exports.union = initial.union;
|
|
921
|
+
exports.unionBy = initial.unionBy;
|
|
922
|
+
exports.unionWith = initial.unionWith;
|
|
923
|
+
exports.uniq = initial.uniq;
|
|
924
|
+
exports.uniqBy = initial.uniqBy;
|
|
925
|
+
exports.uniqWith = initial.uniqWith;
|
|
926
|
+
exports.unzip = initial.unzip;
|
|
927
|
+
exports.unzipWith = initial.unzipWith;
|
|
928
|
+
exports.without = initial.without;
|
|
929
|
+
exports.xor = initial.xor;
|
|
930
|
+
exports.xorBy = initial.xorBy;
|
|
931
|
+
exports.xorWith = initial.xorWith;
|
|
932
|
+
exports.zip = initial.zip;
|
|
933
|
+
exports.zipObject = initial.zipObject;
|
|
934
|
+
exports.zipWith = initial.zipWith;
|
|
899
935
|
exports.AbortError = promise_index.AbortError;
|
|
900
936
|
exports.TimeoutError = promise_index.TimeoutError;
|
|
901
937
|
exports.delay = promise_index.delay;
|
|
@@ -996,6 +1032,7 @@ exports.repeat = repeat;
|
|
|
996
1032
|
exports.rest = rest;
|
|
997
1033
|
exports.set = set;
|
|
998
1034
|
exports.size = size;
|
|
1035
|
+
exports.sortBy = sortBy;
|
|
999
1036
|
exports.spread = spread;
|
|
1000
1037
|
exports.startsWith = startsWith;
|
|
1001
1038
|
exports.zipObjectDeep = zipObjectDeep;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -20,7 +20,6 @@ export { maxBy } from '../array/maxBy.mjs';
|
|
|
20
20
|
export { minBy } from '../array/minBy.mjs';
|
|
21
21
|
export { partition } from '../array/partition.mjs';
|
|
22
22
|
export { pullAt } from '../array/pullAt.mjs';
|
|
23
|
-
export { sortBy } from '../array/sortBy.mjs';
|
|
24
23
|
export { sample } from '../array/sample.mjs';
|
|
25
24
|
export { sampleSize } from '../array/sampleSize.mjs';
|
|
26
25
|
export { shuffle } from '../array/shuffle.mjs';
|
|
@@ -116,6 +115,7 @@ export { flatten } from './array/flatten.mjs';
|
|
|
116
115
|
export { flattenDeep } from './array/flattenDeep.mjs';
|
|
117
116
|
export { flattenDepth } from './array/flattenDepth.mjs';
|
|
118
117
|
export { orderBy } from './array/orderBy.mjs';
|
|
118
|
+
export { sortBy } from './array/sortBy.mjs';
|
|
119
119
|
export { size } from './array/size.mjs';
|
|
120
120
|
export { zipObjectDeep } from './array/zipObjectDeep.mjs';
|
|
121
121
|
export { indexOf } from './array/indexOf.mjs';
|