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.
@@ -0,0 +1,11 @@
1
+ const compareValues = (a, b, order) => {
2
+ if ((a != null && b == null) || a < b) {
3
+ return order === 'desc' ? 1 : -1;
4
+ }
5
+ if ((a == null && b != null) || a > b) {
6
+ return order === 'desc' ? -1 : 1;
7
+ }
8
+ return 0;
9
+ };
10
+
11
+ export { compareValues };
@@ -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 && Object.hasOwn(object, value)));
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 keys to sort by, and an array of order directions.
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) | string | Array<((item: T) => unknown) | string | string[]>} keys - An array of keys (property names or property paths or custom key functions) to sort by.
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 extends object>(collection: T[] | null | undefined, keys?: ((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>, orders?: unknown | unknown[]): 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 keys to sort by, and an array of order directions.
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) | string | Array<((item: T) => unknown) | string | string[]>} keys - An array of keys (property names or property paths or custom key functions) to sort by.
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 extends object>(collection: T[] | null | undefined, keys?: ((item: T) => unknown) | string | Array<((item: T) => unknown) | string | string[]>, orders?: unknown | unknown[]): 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 { getPath } from '../_internal/getPath.mjs';
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, keys, orders) {
5
+ function orderBy(collection, criteria, orders) {
4
6
  if (collection == null) {
5
7
  return [];
6
8
  }
7
- if (!Array.isArray(keys)) {
8
- keys = keys == null ? [] : [keys];
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 compareValues = (a, b, order) => {
14
- if (a < b) {
15
- return order === 'desc' ? 1 : -1;
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 0;
23
+ return target;
21
24
  };
22
- const getValueByPath = (path, obj) => {
23
- if (Array.isArray(path)) {
24
- let value = obj;
25
- for (let i = 0; i < path.length; i++) {
26
- value = value[path[i]];
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 (typeof path === 'function') {
31
- return path(obj);
32
+ if (Array.isArray(criterion)) {
33
+ return getValueByNestedPath(object, criterion);
32
34
  }
33
- return obj[path];
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
- keys = keys.map(key => getPath(key, collection[0]));
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 < keys.length; 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;
@@ -1,3 +1,4 @@
1
+ export { at } from '../array/at.mjs';
1
2
  export { compact } from '../array/compact.mjs';
2
3
  export { countBy } from '../array/countBy.mjs';
3
4
  export { differenceBy } from '../array/differenceBy.mjs';
@@ -1,3 +1,4 @@
1
+ export { at } from '../array/at.js';
1
2
  export { compact } from '../array/compact.js';
2
3
  export { countBy } from '../array/countBy.js';
3
4
  export { differenceBy } from '../array/differenceBy.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const initial = require('../_chunk/initial-DHQAoT.js');
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 && Object.hasOwn(object, value)));
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, keys, orders) {
452
+ function orderBy(collection, criteria, orders) {
469
453
  if (collection == null) {
470
454
  return [];
471
455
  }
472
- if (!Array.isArray(keys)) {
473
- keys = keys == null ? [] : [keys];
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 compareValues = (a, b, order) => {
479
- if (a < b) {
480
- return order === 'desc' ? 1 : -1;
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 0;
470
+ return target;
486
471
  };
487
- const getValueByPath = (path, obj) => {
488
- if (Array.isArray(path)) {
489
- let value = obj;
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 path === 'function') {
496
- return path(obj);
476
+ if (typeof criterion === 'function') {
477
+ return criterion(object);
497
478
  }
498
- return obj[path];
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
- keys = keys.map(key => getPath(key, collection[0]));
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 < keys.length; 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;
@@ -1,3 +1,4 @@
1
+ export { at } from '../array/at.mjs';
1
2
  export { compact } from '../array/compact.mjs';
2
3
  export { countBy } from '../array/countBy.mjs';
3
4
  export { differenceBy } from '../array/differenceBy.mjs';
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ export { at } from './array/at.mjs';
1
2
  export { chunk } from './array/chunk.mjs';
2
3
  export { compact } from './array/compact.mjs';
3
4
  export { countBy } from './array/countBy.mjs';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { at } from './array/at.js';
1
2
  export { chunk } from './array/chunk.js';
2
3
  export { compact } from './array/compact.js';
3
4
  export { countBy } from './array/countBy.js';
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-DHQAoT.js');
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
@@ -1,3 +1,4 @@
1
+ export { at } from './array/at.mjs';
1
2
  export { chunk } from './array/chunk.mjs';
2
3
  export { compact } from './array/compact.mjs';
3
4
  export { countBy } from './array/countBy.mjs';
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.496+58393854",
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 };