es-toolkit 1.16.0-dev.455 → 1.16.0-dev.456

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.
@@ -4,23 +4,27 @@ import { toPath } from './toPath.mjs';
4
4
  function getPath(key, object) {
5
5
  if (Array.isArray(key)) {
6
6
  const path = [];
7
+ let target = object;
7
8
  for (let i = 0; i < key.length; i++) {
8
9
  const k = key[i];
9
- if (isKey(k, object)) {
10
- object = object[k];
10
+ if (isKey(k, target)) {
11
+ target = target[k];
11
12
  path.push(k);
12
13
  }
13
14
  else {
14
15
  const keys = toPath(k);
15
16
  for (let i = 0; i < keys.length; i++) {
16
- object = object[keys[i]];
17
+ target = target[keys[i]];
17
18
  path.push(keys[i]);
18
19
  }
19
20
  }
20
21
  }
21
22
  return path;
22
23
  }
23
- return isKey(key, object) ? key : toPath(key);
24
+ if (typeof key === 'function' || isKey(key, object)) {
25
+ return key;
26
+ }
27
+ return toPath(key);
24
28
  }
25
29
 
26
30
  export { getPath };
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * @template T - The type of elements in the array.
10
10
  * @param {T[] | null} collection - The array of objects to be sorted.
11
- * @param {string | Array<string | string[]>} keys - An array of keys (property names or property paths) to sort by.
11
+ * @param {((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>} keys - An array of keys (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
  *
@@ -20,7 +20,7 @@
20
20
  * { user: 'fred', age: 40 },
21
21
  * { user: 'barney', age: 36 },
22
22
  * ];
23
- * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);
23
+ * const result = orderBy(users, ['user', (item) => item.age], ['asc', 'desc']);
24
24
  * // result will be:
25
25
  * // [
26
26
  * // { user: 'barney', age: 36 },
@@ -29,6 +29,6 @@
29
29
  * // { user: 'fred', age: 40 },
30
30
  * // ]
31
31
  */
32
- declare function orderBy<T extends object>(collection?: T[] | null, keys?: string | Array<string | string[]>, orders?: unknown | unknown[]): T[];
32
+ declare function orderBy<T extends object>(collection: T[] | null | undefined, keys?: ((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>, orders?: unknown | unknown[]): T[];
33
33
 
34
34
  export { orderBy };
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * @template T - The type of elements in the array.
10
10
  * @param {T[] | null} collection - The array of objects to be sorted.
11
- * @param {string | Array<string | string[]>} keys - An array of keys (property names or property paths) to sort by.
11
+ * @param {((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>} keys - An array of keys (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
  *
@@ -20,7 +20,7 @@
20
20
  * { user: 'fred', age: 40 },
21
21
  * { user: 'barney', age: 36 },
22
22
  * ];
23
- * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);
23
+ * const result = orderBy(users, ['user', (item) => item.age], ['asc', 'desc']);
24
24
  * // result will be:
25
25
  * // [
26
26
  * // { user: 'barney', age: 36 },
@@ -29,6 +29,6 @@
29
29
  * // { user: 'fred', age: 40 },
30
30
  * // ]
31
31
  */
32
- declare function orderBy<T extends object>(collection?: T[] | null, keys?: string | Array<string | string[]>, orders?: unknown | unknown[]): T[];
32
+ declare function orderBy<T extends object>(collection: T[] | null | undefined, keys?: ((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>, orders?: unknown | unknown[]): T[];
33
33
 
34
34
  export { orderBy };
@@ -19,23 +19,25 @@ function orderBy(collection, keys, orders) {
19
19
  }
20
20
  return 0;
21
21
  };
22
- const getValueByPath = (key, obj) => {
23
- if (Array.isArray(key)) {
22
+ const getValueByPath = (path, obj) => {
23
+ if (Array.isArray(path)) {
24
24
  let value = obj;
25
- for (let i = 0; i < key.length; i++) {
26
- value = value[key[i]];
25
+ for (let i = 0; i < path.length; i++) {
26
+ value = value[path[i]];
27
27
  }
28
28
  return value;
29
29
  }
30
- return obj[key];
30
+ if (typeof path === 'function') {
31
+ return path(obj);
32
+ }
33
+ return obj[path];
31
34
  };
32
35
  keys = keys.map(key => getPath(key, collection[0]));
33
- const shallowCopiedCollection = collection.slice();
34
- const orderedCollection = shallowCopiedCollection.sort((a, b) => {
36
+ return collection.slice().sort((a, b) => {
35
37
  for (let i = 0; i < keys.length; i++) {
36
- const key = keys[i];
37
- const valueA = getValueByPath(key, a);
38
- const valueB = getValueByPath(key, b);
38
+ const path = keys[i];
39
+ const valueA = getValueByPath(path, a);
40
+ const valueB = getValueByPath(path, b);
39
41
  const order = String(orders[i]);
40
42
  const comparedResult = compareValues(valueA, valueB, order);
41
43
  if (comparedResult !== 0) {
@@ -44,7 +46,6 @@ function orderBy(collection, keys, orders) {
44
46
  }
45
47
  return 0;
46
48
  });
47
- return orderedCollection;
48
49
  }
49
50
 
50
51
  export { orderBy };
@@ -442,23 +442,27 @@ function isKey(value, object) {
442
442
  function getPath(key, object) {
443
443
  if (Array.isArray(key)) {
444
444
  const path = [];
445
+ let target = object;
445
446
  for (let i = 0; i < key.length; i++) {
446
447
  const k = key[i];
447
- if (isKey(k, object)) {
448
- object = object[k];
448
+ if (isKey(k, target)) {
449
+ target = target[k];
449
450
  path.push(k);
450
451
  }
451
452
  else {
452
453
  const keys = toPath(k);
453
454
  for (let i = 0; i < keys.length; i++) {
454
- object = object[keys[i]];
455
+ target = target[keys[i]];
455
456
  path.push(keys[i]);
456
457
  }
457
458
  }
458
459
  }
459
460
  return path;
460
461
  }
461
- return isKey(key, object) ? key : toPath(key);
462
+ if (typeof key === 'function' || isKey(key, object)) {
463
+ return key;
464
+ }
465
+ return toPath(key);
462
466
  }
463
467
 
464
468
  function orderBy(collection, keys, orders) {
@@ -480,23 +484,25 @@ function orderBy(collection, keys, orders) {
480
484
  }
481
485
  return 0;
482
486
  };
483
- const getValueByPath = (key, obj) => {
484
- if (Array.isArray(key)) {
487
+ const getValueByPath = (path, obj) => {
488
+ if (Array.isArray(path)) {
485
489
  let value = obj;
486
- for (let i = 0; i < key.length; i++) {
487
- value = value[key[i]];
490
+ for (let i = 0; i < path.length; i++) {
491
+ value = value[path[i]];
488
492
  }
489
493
  return value;
490
494
  }
491
- return obj[key];
495
+ if (typeof path === 'function') {
496
+ return path(obj);
497
+ }
498
+ return obj[path];
492
499
  };
493
500
  keys = keys.map(key => getPath(key, collection[0]));
494
- const shallowCopiedCollection = collection.slice();
495
- const orderedCollection = shallowCopiedCollection.sort((a, b) => {
501
+ return collection.slice().sort((a, b) => {
496
502
  for (let i = 0; i < keys.length; i++) {
497
- const key = keys[i];
498
- const valueA = getValueByPath(key, a);
499
- const valueB = getValueByPath(key, b);
503
+ const path = keys[i];
504
+ const valueA = getValueByPath(path, a);
505
+ const valueB = getValueByPath(path, b);
500
506
  const order = String(orders[i]);
501
507
  const comparedResult = compareValues(valueA, valueB, order);
502
508
  if (comparedResult !== 0) {
@@ -505,7 +511,6 @@ function orderBy(collection, keys, orders) {
505
511
  }
506
512
  return 0;
507
513
  });
508
- return orderedCollection;
509
514
  }
510
515
 
511
516
  function size(target) {
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.455+f08fbd1b",
4
+ "version": "1.16.0-dev.456+4637a17b",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {