es-toolkit 1.16.0-dev.496 → 1.16.0-dev.499
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-DHQAoT.js → initial-Ci2bn_.js} +10 -9
- package/dist/array/at.d.mts +18 -0
- package/dist/array/at.d.ts +18 -0
- package/dist/array/at.mjs +1 -1
- package/dist/array/index.d.mts +1 -0
- package/dist/array/index.d.ts +1 -0
- package/dist/array/index.js +2 -1
- package/dist/array/index.mjs +1 -0
- package/dist/array/pullAt.d.mts +3 -6
- package/dist/array/pullAt.d.ts +3 -6
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/_internal/compareValues.mjs +11 -0
- package/dist/compat/_internal/isKey.mjs +1 -1
- package/dist/compat/array/orderBy.d.mts +4 -4
- package/dist/compat/array/orderBy.d.ts +4 -4
- package/dist/compat/array/orderBy.mjs +41 -26
- package/dist/compat/index.d.mts +1 -0
- package/dist/compat/index.d.ts +1 -0
- package/dist/compat/index.js +51 -53
- package/dist/compat/index.mjs +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/index.mjs +1 -0
- package/package.json +1 -1
- package/dist/compat/_internal/getPath.mjs +0 -30
|
@@ -10,7 +10,7 @@ function isKey(value, object) {
|
|
|
10
10
|
return true;
|
|
11
11
|
}
|
|
12
12
|
return ((typeof value === 'string' && (regexIsPlainProp.test(value) || !regexIsDeepProp.test(value))) ||
|
|
13
|
-
(object != null
|
|
13
|
+
(object != null));
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export { isKey };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
3
3
|
*
|
|
4
|
-
* This function takes an array of objects, an array of
|
|
4
|
+
* This function takes an array of objects, an array of criteria to sort by, and an array of order directions.
|
|
5
5
|
* It returns the sorted array, ordering by each key according to its corresponding direction
|
|
6
6
|
* ('asc' for ascending or 'desc' for descending). If values for a key are equal,string
|
|
7
7
|
* 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 {T[] | null} collection - The array of objects to be sorted.
|
|
11
|
-
* @param {((item: T) => unknown) |
|
|
10
|
+
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
|
+
* @param {((item: T) => unknown) | PropertyKey | Array<((item: T) => unknown) | PropertyKey | PropertyKey[]>} 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
|
|
32
|
+
declare function orderBy<T>(collection: T[] | object | null | undefined, criteria?: ((item: T) => unknown) | PropertyKey | Array<((item: T) => unknown) | PropertyKey | PropertyKey[]>, orders?: unknown | unknown[]): T[];
|
|
33
33
|
|
|
34
34
|
export { orderBy };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Sorts an array of objects based on multiple properties and their corresponding order directions.
|
|
3
3
|
*
|
|
4
|
-
* This function takes an array of objects, an array of
|
|
4
|
+
* This function takes an array of objects, an array of criteria to sort by, and an array of order directions.
|
|
5
5
|
* It returns the sorted array, ordering by each key according to its corresponding direction
|
|
6
6
|
* ('asc' for ascending or 'desc' for descending). If values for a key are equal,string
|
|
7
7
|
* 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 {T[] | null} collection - The array of objects to be sorted.
|
|
11
|
-
* @param {((item: T) => unknown) |
|
|
10
|
+
* @param { T[] | object | null | undefined} collection - The array of objects to be sorted.
|
|
11
|
+
* @param {((item: T) => unknown) | PropertyKey | Array<((item: T) => unknown) | PropertyKey | PropertyKey[]>} 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
|
|
32
|
+
declare function orderBy<T>(collection: T[] | object | null | undefined, criteria?: ((item: T) => unknown) | PropertyKey | Array<((item: T) => unknown) | PropertyKey | PropertyKey[]>, orders?: unknown | unknown[]): T[];
|
|
33
33
|
|
|
34
34
|
export { orderBy };
|
|
@@ -1,44 +1,59 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { compareValues } from '../_internal/compareValues.mjs';
|
|
2
|
+
import { isKey } from '../_internal/isKey.mjs';
|
|
3
|
+
import { toPath } from '../_internal/toPath.mjs';
|
|
2
4
|
|
|
3
|
-
function orderBy(collection,
|
|
5
|
+
function orderBy(collection, criteria, orders) {
|
|
4
6
|
if (collection == null) {
|
|
5
7
|
return [];
|
|
6
8
|
}
|
|
7
|
-
if (!Array.isArray(
|
|
8
|
-
|
|
9
|
+
if (!Array.isArray(collection) && typeof collection === 'object') {
|
|
10
|
+
collection = Object.values(collection);
|
|
11
|
+
}
|
|
12
|
+
if (!Array.isArray(criteria)) {
|
|
13
|
+
criteria = criteria == null ? [] : [criteria];
|
|
9
14
|
}
|
|
10
15
|
if (!Array.isArray(orders)) {
|
|
11
16
|
orders = orders == null ? [] : [orders];
|
|
12
17
|
}
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (a > b) {
|
|
18
|
-
return order === 'desc' ? -1 : 1;
|
|
18
|
+
const getValueByNestedPath = (object, path) => {
|
|
19
|
+
let target = object;
|
|
20
|
+
for (let i = 0; i < path.length && target != null; i++) {
|
|
21
|
+
target = target[path[i]];
|
|
19
22
|
}
|
|
20
|
-
return
|
|
23
|
+
return target;
|
|
21
24
|
};
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return value;
|
|
25
|
+
const getValueByCriterion = (criterion, object) => {
|
|
26
|
+
if (object == null) {
|
|
27
|
+
return object;
|
|
28
|
+
}
|
|
29
|
+
if (typeof criterion === 'function') {
|
|
30
|
+
return criterion(object);
|
|
29
31
|
}
|
|
30
|
-
if (
|
|
31
|
-
return
|
|
32
|
+
if (Array.isArray(criterion)) {
|
|
33
|
+
return getValueByNestedPath(object, criterion);
|
|
32
34
|
}
|
|
33
|
-
|
|
35
|
+
if (typeof criterion !== 'object') {
|
|
36
|
+
return object[criterion];
|
|
37
|
+
}
|
|
38
|
+
if (Object.hasOwn(object, criterion.key)) {
|
|
39
|
+
return object[criterion.key];
|
|
40
|
+
}
|
|
41
|
+
return getValueByNestedPath(object, criterion.path);
|
|
34
42
|
};
|
|
35
|
-
|
|
43
|
+
const preparedCriteria = criteria.map(criterion => {
|
|
44
|
+
if (Array.isArray(criterion) && criterion.length === 1) {
|
|
45
|
+
criterion = criterion[0];
|
|
46
|
+
}
|
|
47
|
+
if (typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
48
|
+
return criterion;
|
|
49
|
+
}
|
|
50
|
+
return { key: criterion, path: toPath(criterion) };
|
|
51
|
+
});
|
|
36
52
|
return collection.slice().sort((a, b) => {
|
|
37
|
-
for (let i = 0; i <
|
|
38
|
-
const path = keys[i];
|
|
39
|
-
const valueA = getValueByPath(path, a);
|
|
40
|
-
const valueB = getValueByPath(path, b);
|
|
53
|
+
for (let i = 0; i < criteria.length; i++) {
|
|
41
54
|
const order = String(orders[i]);
|
|
55
|
+
const valueA = getValueByCriterion(preparedCriteria[i], a);
|
|
56
|
+
const valueB = getValueByCriterion(preparedCriteria[i], b);
|
|
42
57
|
const comparedResult = compareValues(valueA, valueB, order);
|
|
43
58
|
if (comparedResult !== 0) {
|
|
44
59
|
return comparedResult;
|
package/dist/compat/index.d.mts
CHANGED
package/dist/compat/index.d.ts
CHANGED
package/dist/compat/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const initial = require('../_chunk/initial-
|
|
5
|
+
const initial = require('../_chunk/initial-Ci2bn_.js');
|
|
6
6
|
const promise_index = require('../_chunk/index-CoqN5B.js');
|
|
7
7
|
const function_index = require('../function/index.js');
|
|
8
8
|
const math_index = require('../math/index.js');
|
|
@@ -426,6 +426,16 @@ function flattenDepth(value, depth = 1) {
|
|
|
426
426
|
return flatten(value, depth);
|
|
427
427
|
}
|
|
428
428
|
|
|
429
|
+
const compareValues = (a, b, order) => {
|
|
430
|
+
if ((a != null && b == null) || a < b) {
|
|
431
|
+
return order === 'desc' ? 1 : -1;
|
|
432
|
+
}
|
|
433
|
+
if ((a == null && b != null) || a > b) {
|
|
434
|
+
return order === 'desc' ? -1 : 1;
|
|
435
|
+
}
|
|
436
|
+
return 0;
|
|
437
|
+
};
|
|
438
|
+
|
|
429
439
|
const regexIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/;
|
|
430
440
|
const regexIsPlainProp = /^\w*$/;
|
|
431
441
|
function isKey(value, object) {
|
|
@@ -436,74 +446,61 @@ function isKey(value, object) {
|
|
|
436
446
|
return true;
|
|
437
447
|
}
|
|
438
448
|
return ((typeof value === 'string' && (regexIsPlainProp.test(value) || !regexIsDeepProp.test(value))) ||
|
|
439
|
-
(object != null
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function getPath(key, object) {
|
|
443
|
-
if (Array.isArray(key)) {
|
|
444
|
-
const path = [];
|
|
445
|
-
let target = object;
|
|
446
|
-
for (let i = 0; i < key.length; i++) {
|
|
447
|
-
const k = key[i];
|
|
448
|
-
if (isKey(k, target)) {
|
|
449
|
-
target = target[k];
|
|
450
|
-
path.push(k);
|
|
451
|
-
}
|
|
452
|
-
else {
|
|
453
|
-
const keys = toPath(k);
|
|
454
|
-
for (let i = 0; i < keys.length; i++) {
|
|
455
|
-
target = target[keys[i]];
|
|
456
|
-
path.push(keys[i]);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
return path;
|
|
461
|
-
}
|
|
462
|
-
if (typeof key === 'function' || isKey(key, object)) {
|
|
463
|
-
return key;
|
|
464
|
-
}
|
|
465
|
-
return toPath(key);
|
|
449
|
+
(object != null));
|
|
466
450
|
}
|
|
467
451
|
|
|
468
|
-
function orderBy(collection,
|
|
452
|
+
function orderBy(collection, criteria, orders) {
|
|
469
453
|
if (collection == null) {
|
|
470
454
|
return [];
|
|
471
455
|
}
|
|
472
|
-
if (!Array.isArray(
|
|
473
|
-
|
|
456
|
+
if (!Array.isArray(collection) && typeof collection === 'object') {
|
|
457
|
+
collection = Object.values(collection);
|
|
458
|
+
}
|
|
459
|
+
if (!Array.isArray(criteria)) {
|
|
460
|
+
criteria = criteria == null ? [] : [criteria];
|
|
474
461
|
}
|
|
475
462
|
if (!Array.isArray(orders)) {
|
|
476
463
|
orders = orders == null ? [] : [orders];
|
|
477
464
|
}
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
if (a > b) {
|
|
483
|
-
return order === 'desc' ? -1 : 1;
|
|
465
|
+
const getValueByNestedPath = (object, path) => {
|
|
466
|
+
let target = object;
|
|
467
|
+
for (let i = 0; i < path.length && target != null; i++) {
|
|
468
|
+
target = target[path[i]];
|
|
484
469
|
}
|
|
485
|
-
return
|
|
470
|
+
return target;
|
|
486
471
|
};
|
|
487
|
-
const
|
|
488
|
-
if (
|
|
489
|
-
|
|
490
|
-
for (let i = 0; i < path.length; i++) {
|
|
491
|
-
value = value[path[i]];
|
|
492
|
-
}
|
|
493
|
-
return value;
|
|
472
|
+
const getValueByCriterion = (criterion, object) => {
|
|
473
|
+
if (object == null) {
|
|
474
|
+
return object;
|
|
494
475
|
}
|
|
495
|
-
if (typeof
|
|
496
|
-
return
|
|
476
|
+
if (typeof criterion === 'function') {
|
|
477
|
+
return criterion(object);
|
|
497
478
|
}
|
|
498
|
-
|
|
479
|
+
if (Array.isArray(criterion)) {
|
|
480
|
+
return getValueByNestedPath(object, criterion);
|
|
481
|
+
}
|
|
482
|
+
if (typeof criterion !== 'object') {
|
|
483
|
+
return object[criterion];
|
|
484
|
+
}
|
|
485
|
+
if (Object.hasOwn(object, criterion.key)) {
|
|
486
|
+
return object[criterion.key];
|
|
487
|
+
}
|
|
488
|
+
return getValueByNestedPath(object, criterion.path);
|
|
499
489
|
};
|
|
500
|
-
|
|
490
|
+
const preparedCriteria = criteria.map(criterion => {
|
|
491
|
+
if (Array.isArray(criterion) && criterion.length === 1) {
|
|
492
|
+
criterion = criterion[0];
|
|
493
|
+
}
|
|
494
|
+
if (typeof criterion === 'function' || Array.isArray(criterion) || isKey(criterion)) {
|
|
495
|
+
return criterion;
|
|
496
|
+
}
|
|
497
|
+
return { key: criterion, path: toPath(criterion) };
|
|
498
|
+
});
|
|
501
499
|
return collection.slice().sort((a, b) => {
|
|
502
|
-
for (let i = 0; i <
|
|
503
|
-
const path = keys[i];
|
|
504
|
-
const valueA = getValueByPath(path, a);
|
|
505
|
-
const valueB = getValueByPath(path, b);
|
|
500
|
+
for (let i = 0; i < criteria.length; i++) {
|
|
506
501
|
const order = String(orders[i]);
|
|
502
|
+
const valueA = getValueByCriterion(preparedCriteria[i], a);
|
|
503
|
+
const valueB = getValueByCriterion(preparedCriteria[i], b);
|
|
507
504
|
const comparedResult = compareValues(valueA, valueB, order);
|
|
508
505
|
if (comparedResult !== 0) {
|
|
509
506
|
return comparedResult;
|
|
@@ -820,6 +817,7 @@ function min(items = []) {
|
|
|
820
817
|
return minElement;
|
|
821
818
|
}
|
|
822
819
|
|
|
820
|
+
exports.at = initial.at;
|
|
823
821
|
exports.compact = initial.compact;
|
|
824
822
|
exports.countBy = initial.countBy;
|
|
825
823
|
exports.differenceBy = initial.differenceBy;
|
package/dist/compat/index.mjs
CHANGED
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const initial = require('./_chunk/initial-
|
|
5
|
+
const initial = require('./_chunk/initial-Ci2bn_.js');
|
|
6
6
|
const array_index = require('./array/index.js');
|
|
7
7
|
const promise_index = require('./_chunk/index-CoqN5B.js');
|
|
8
8
|
const function_index = require('./function/index.js');
|
|
@@ -17,6 +17,7 @@ const string_index = require('./string/index.js');
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
|
+
exports.at = initial.at;
|
|
20
21
|
exports.chunk = initial.chunk;
|
|
21
22
|
exports.compact = initial.compact;
|
|
22
23
|
exports.countBy = initial.countBy;
|
package/dist/index.mjs
CHANGED
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.16.0-dev.
|
|
4
|
+
"version": "1.16.0-dev.499+f3620d76",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { isKey } from './isKey.mjs';
|
|
2
|
-
import { toPath } from './toPath.mjs';
|
|
3
|
-
|
|
4
|
-
function getPath(key, object) {
|
|
5
|
-
if (Array.isArray(key)) {
|
|
6
|
-
const path = [];
|
|
7
|
-
let target = object;
|
|
8
|
-
for (let i = 0; i < key.length; i++) {
|
|
9
|
-
const k = key[i];
|
|
10
|
-
if (isKey(k, target)) {
|
|
11
|
-
target = target[k];
|
|
12
|
-
path.push(k);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
const keys = toPath(k);
|
|
16
|
-
for (let i = 0; i < keys.length; i++) {
|
|
17
|
-
target = target[keys[i]];
|
|
18
|
-
path.push(keys[i]);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return path;
|
|
23
|
-
}
|
|
24
|
-
if (typeof key === 'function' || isKey(key, object)) {
|
|
25
|
-
return key;
|
|
26
|
-
}
|
|
27
|
-
return toPath(key);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export { getPath };
|