es-toolkit 1.16.0 → 1.17.0-dev.504

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.
Files changed (129) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +1 -1
  3. package/dist/_chunk/{index-CwRt_M.js → index-CoqN5B.js} +1 -0
  4. package/dist/_chunk/{initial-y0QrPY.js → initial-Ci2bn_.js} +41 -26
  5. package/dist/array/at.d.mts +18 -0
  6. package/dist/array/at.d.ts +18 -0
  7. package/dist/array/at.mjs +10 -0
  8. package/dist/array/countBy.d.mts +5 -5
  9. package/dist/array/countBy.d.ts +5 -5
  10. package/dist/array/index.d.mts +2 -0
  11. package/dist/array/index.d.ts +2 -0
  12. package/dist/array/index.js +4 -17
  13. package/dist/array/index.mjs +2 -0
  14. package/dist/array/initial.d.mts +1 -1
  15. package/dist/array/initial.d.ts +1 -1
  16. package/dist/array/initial.mjs +0 -3
  17. package/dist/array/maxBy.d.mts +3 -2
  18. package/dist/array/maxBy.d.ts +3 -2
  19. package/dist/array/minBy.d.mts +3 -2
  20. package/dist/array/minBy.d.ts +3 -2
  21. package/dist/array/orderBy.d.mts +13 -11
  22. package/dist/array/orderBy.d.ts +13 -11
  23. package/dist/array/orderBy.mjs +10 -7
  24. package/dist/array/pullAt.d.mts +21 -0
  25. package/dist/array/pullAt.d.ts +21 -0
  26. package/dist/array/pullAt.mjs +12 -0
  27. package/dist/array/sampleSize.d.mts +1 -1
  28. package/dist/array/sampleSize.d.ts +1 -1
  29. package/dist/array/sortBy.mjs +2 -14
  30. package/dist/array/tail.mjs +1 -9
  31. package/dist/array/takeRightWhile.d.mts +1 -1
  32. package/dist/array/takeRightWhile.d.ts +1 -1
  33. package/dist/array/uniqBy.d.mts +10 -0
  34. package/dist/array/uniqBy.d.ts +10 -0
  35. package/dist/browser.global.js +1 -1
  36. package/dist/browser.global.js.map +1 -1
  37. package/dist/compat/_internal/compareValues.mjs +11 -0
  38. package/dist/compat/_internal/isKey.mjs +1 -1
  39. package/dist/compat/array/find.d.mts +1 -1
  40. package/dist/compat/array/find.d.ts +1 -1
  41. package/dist/compat/array/findIndex.d.mts +1 -1
  42. package/dist/compat/array/findIndex.d.ts +1 -1
  43. package/dist/compat/array/orderBy.d.mts +5 -5
  44. package/dist/compat/array/orderBy.d.ts +5 -5
  45. package/dist/compat/array/orderBy.mjs +43 -27
  46. package/dist/compat/array/zipObjectDeep.d.mts +2 -1
  47. package/dist/compat/array/zipObjectDeep.d.ts +2 -1
  48. package/dist/compat/function/ary.d.mts +1 -1
  49. package/dist/compat/function/ary.d.ts +1 -1
  50. package/dist/compat/function/bind.d.mts +5 -6
  51. package/dist/compat/function/bind.d.ts +5 -6
  52. package/dist/compat/function/bind.mjs +3 -3
  53. package/dist/compat/function/rest.d.mts +1 -1
  54. package/dist/compat/function/rest.d.ts +1 -1
  55. package/dist/compat/index.d.mts +7 -0
  56. package/dist/compat/index.d.ts +7 -0
  57. package/dist/compat/index.js +67 -54
  58. package/dist/compat/index.mjs +7 -0
  59. package/dist/compat/object/mapKeys.d.mts +1 -1
  60. package/dist/compat/object/mapKeys.d.ts +1 -1
  61. package/dist/compat/object/merge.d.mts +1 -2
  62. package/dist/compat/object/merge.d.ts +1 -2
  63. package/dist/compat/object/mergeWith.d.mts +6 -7
  64. package/dist/compat/object/mergeWith.d.ts +6 -7
  65. package/dist/compat/object/property.d.mts +1 -1
  66. package/dist/compat/object/property.d.ts +1 -1
  67. package/dist/compat/object/set.d.mts +2 -2
  68. package/dist/compat/object/set.d.ts +2 -2
  69. package/dist/compat/predicate/isBoolean.d.mts +1 -1
  70. package/dist/compat/predicate/isBoolean.d.ts +1 -1
  71. package/dist/compat/predicate/isObjectLike.d.mts +2 -1
  72. package/dist/compat/predicate/isObjectLike.d.ts +2 -1
  73. package/dist/compat/predicate/isRegExp.d.mts +9 -0
  74. package/dist/compat/predicate/isRegExp.d.ts +9 -0
  75. package/dist/compat/predicate/isRegExp.mjs +7 -0
  76. package/dist/compat/predicate/isString.d.mts +2 -2
  77. package/dist/compat/predicate/isString.d.ts +2 -2
  78. package/dist/compat/predicate/isTypedArray.d.mts +1 -1
  79. package/dist/compat/predicate/isTypedArray.d.ts +1 -1
  80. package/dist/function/ary.d.mts +10 -1
  81. package/dist/function/ary.d.ts +10 -1
  82. package/dist/function/memoize.d.mts +1 -0
  83. package/dist/function/memoize.d.ts +1 -0
  84. package/dist/function/throttle.d.mts +2 -2
  85. package/dist/function/throttle.d.ts +2 -2
  86. package/dist/index.d.mts +7 -0
  87. package/dist/index.d.ts +7 -0
  88. package/dist/index.js +10 -3
  89. package/dist/index.mjs +7 -0
  90. package/dist/math/sumBy.d.mts +2 -2
  91. package/dist/math/sumBy.d.ts +2 -2
  92. package/dist/predicate/index.d.mts +1 -0
  93. package/dist/predicate/index.d.ts +1 -0
  94. package/dist/predicate/index.js +5 -0
  95. package/dist/predicate/index.mjs +1 -0
  96. package/dist/predicate/isBoolean.d.mts +1 -1
  97. package/dist/predicate/isBoolean.d.ts +1 -1
  98. package/dist/predicate/isPrimitive.d.mts +1 -1
  99. package/dist/predicate/isPrimitive.d.ts +1 -1
  100. package/dist/predicate/isRegExp.d.mts +9 -0
  101. package/dist/predicate/isRegExp.d.ts +9 -0
  102. package/dist/predicate/isRegExp.mjs +5 -0
  103. package/dist/predicate/isString.d.mts +2 -2
  104. package/dist/predicate/isString.d.ts +2 -2
  105. package/dist/predicate/isTypedArray.d.mts +1 -1
  106. package/dist/predicate/isTypedArray.d.ts +1 -1
  107. package/dist/promise/index.d.mts +1 -0
  108. package/dist/promise/index.d.ts +1 -0
  109. package/dist/promise/index.js +2 -1
  110. package/dist/promise/index.mjs +1 -0
  111. package/dist/promise/timeout.d.mts +10 -0
  112. package/dist/promise/timeout.d.ts +10 -0
  113. package/dist/string/camelCase.d.mts +2 -1
  114. package/dist/string/camelCase.d.ts +2 -1
  115. package/dist/string/deburr.d.mts +22 -0
  116. package/dist/string/deburr.d.ts +22 -0
  117. package/dist/string/deburr.mjs +45 -0
  118. package/dist/string/index.d.mts +3 -0
  119. package/dist/string/index.d.ts +3 -0
  120. package/dist/string/index.js +55 -0
  121. package/dist/string/index.mjs +3 -0
  122. package/dist/string/lowerFirst.d.mts +14 -0
  123. package/dist/string/lowerFirst.d.ts +14 -0
  124. package/dist/string/lowerFirst.mjs +5 -0
  125. package/dist/string/upperFirst.d.mts +14 -0
  126. package/dist/string/upperFirst.d.ts +14 -0
  127. package/dist/string/upperFirst.mjs +5 -0
  128. package/package.json +9 -13
  129. package/dist/compat/_internal/getPath.mjs +0 -26
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # es-toolkit Changelog
2
2
 
3
+ ## Version v1.17.0
4
+
5
+ Released on August 31st, 2024.
6
+
7
+ ### New Features
8
+
9
+ - Added support for new functions: [at](https://es-toolkit.slash.page/reference/array/at.html), [pullAt](https://es-toolkit.slash.page/reference/array/pullAt.html), [deburr](https://es-toolkit.slash.page/reference/string/deburr.html), [lowerFirst](https://es-toolkit.slash.page/reference/string/lowerFirst.html), [upperFirst](https://es-toolkit.slash.page/reference/string/upperFirst.html), and [isRegExp](https://es-toolkit.slash.page/reference/predicate/isRegExp.html).
10
+ - Enhanced [orderBy](https://es-toolkit.slash.page/reference/array/orderBy.html) and [sortBy](https://es-toolkit.slash.page/reference/array/sortBy.html) to support function criteria.
11
+ - [countBy](https://es-toolkit.slash.page/reference/array/countBy.html) now supports numeric and symbol keys.
12
+
13
+ #### Bug Fixes
14
+
15
+ - Updated type definitions for [throttle](https://es-toolkit.slash.page/reference/function/throttle.html) and [debounce](https://es-toolkit.slash.page/reference/function/debounce.html).
16
+ - (`es-toolkit/compat`) Fixed [orderBy](https://es-toolkit.slash.page/reference/array/orderBy.html) to correctly handle deep keys even when object shapes differ ([#427](https://github.com/toss/es-toolkit/pull/427)).
17
+
3
18
  ## Version v1.16.0
4
19
 
5
20
  Released on August 15th, 2024.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # es-toolkit · [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/toss/slash/blob/main/LICENSE) [![codecov](https://codecov.io/gh/toss/es-toolkit/graph/badge.svg?token=8N5S3AR3C7)](https://codecov.io/gh/toss/es-toolkit) [![NPM badge](https://img.shields.io/npm/v/es-toolkit?logo=npm)](https://www.npmjs.com/package/es-toolkit) [![JSR badge](https://jsr.io/badges/@es-toolkit/es-toolkit)](https://jsr.io/@es-toolkit/es-toolkit)
4
4
 
5
- English | [한국어](https://github.com/toss/es-toolkit/blob/main/README-ko_kr.md) | [简体中文](https://github.com/toss/es-toolkit/blob/main/README-zh_hans.md)
5
+ English | [한국어](https://github.com/toss/es-toolkit/blob/main/README-ko_kr.md) | [简体中文](https://github.com/toss/es-toolkit/blob/main/README-zh_hans.md) | [日本語](https://github.com/toss/es-toolkit/blob/main/README-ja_jp.md)
6
6
 
7
7
  es-toolkit is a state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
8
8
 
@@ -43,4 +43,5 @@ async function withTimeout(run, ms) {
43
43
  exports.AbortError = AbortError;
44
44
  exports.TimeoutError = TimeoutError;
45
45
  exports.delay = delay;
46
+ exports.timeout = timeout;
46
47
  exports.withTimeout = withTimeout;
@@ -2,6 +2,15 @@
2
2
 
3
3
  const randomInt = require('./randomInt-CF7bZK.js');
4
4
 
5
+ function at(arr, indices) {
6
+ const result = [];
7
+ for (let i = 0; i < indices.length; i++) {
8
+ const index = indices[i];
9
+ result.push(arr.at(index));
10
+ }
11
+ return result;
12
+ }
13
+
5
14
  function chunk(arr, size) {
6
15
  if (!Number.isInteger(size) || size <= 0) {
7
16
  throw new Error('Size must be an integer greater than zero.');
@@ -200,6 +209,24 @@ function compareValues(a, b, order) {
200
209
  return 0;
201
210
  }
202
211
 
212
+ function orderBy(arr, criteria, orders) {
213
+ return arr.slice().sort((a, b) => {
214
+ const ordersLength = orders.length;
215
+ for (let i = 0; i < criteria.length; i++) {
216
+ const order = ordersLength > i ? orders[i] : orders[ordersLength - 1];
217
+ const criterion = criteria[i];
218
+ const criterionIsFunction = typeof criterion === 'function';
219
+ const valueA = criterionIsFunction ? criterion(a) : a[criterion];
220
+ const valueB = criterionIsFunction ? criterion(b) : b[criterion];
221
+ const result = compareValues(valueA, valueB, order);
222
+ if (result !== 0) {
223
+ return result;
224
+ }
225
+ }
226
+ return 0;
227
+ });
228
+ }
229
+
203
230
  function partition(arr, isInTruthy) {
204
231
  const truthy = [];
205
232
  const falsy = [];
@@ -214,20 +241,17 @@ function partition(arr, isInTruthy) {
214
241
  return [truthy, falsy];
215
242
  }
216
243
 
244
+ function pullAt(arr, indicesToRemove) {
245
+ const removed = at(arr, indicesToRemove);
246
+ const indices = new Set(indicesToRemove.slice().sort((x, y) => y - x));
247
+ for (const index of indices) {
248
+ arr.splice(index, 1);
249
+ }
250
+ return removed;
251
+ }
252
+
217
253
  function sortBy(arr, criteria) {
218
- return arr.slice().sort((a, b) => {
219
- for (let i = 0; i < criteria.length; i++) {
220
- const iteratee = criteria[i];
221
- const iterateeIsFunction = typeof iteratee === 'function';
222
- const valueA = iterateeIsFunction ? iteratee(a) : a[iteratee];
223
- const valueB = iterateeIsFunction ? iteratee(b) : b[iteratee];
224
- const result = compareValues(valueA, valueB, 'asc');
225
- if (result !== 0) {
226
- return result;
227
- }
228
- }
229
- return 0;
230
- });
254
+ return orderBy(arr, criteria, ['asc']);
231
255
  }
232
256
 
233
257
  function sample(arr) {
@@ -430,15 +454,7 @@ function isSubset(superset, subset) {
430
454
  }
431
455
 
432
456
  function tail(arr) {
433
- const len = arr.length;
434
- if (len <= 1) {
435
- return [];
436
- }
437
- const result = new Array(len - 1);
438
- for (let i = 1; i < len; i++) {
439
- result[i - 1] = arr[i];
440
- }
441
- return result;
457
+ return arr.slice(1);
442
458
  }
443
459
 
444
460
  function toFilled(arr, value, start = 0, end = arr.length) {
@@ -457,15 +473,12 @@ function last(arr) {
457
473
  }
458
474
 
459
475
  function initial(arr) {
460
- if (arr.length <= 1) {
461
- return [];
462
- }
463
476
  return arr.slice(0, -1);
464
477
  }
465
478
 
479
+ exports.at = at;
466
480
  exports.chunk = chunk;
467
481
  exports.compact = compact;
468
- exports.compareValues = compareValues;
469
482
  exports.countBy = countBy;
470
483
  exports.difference = difference;
471
484
  exports.differenceBy = differenceBy;
@@ -489,7 +502,9 @@ exports.keyBy = keyBy;
489
502
  exports.last = last;
490
503
  exports.maxBy = maxBy;
491
504
  exports.minBy = minBy;
505
+ exports.orderBy = orderBy;
492
506
  exports.partition = partition;
507
+ exports.pullAt = pullAt;
493
508
  exports.sample = sample;
494
509
  exports.sampleSize = sampleSize;
495
510
  exports.shuffle = shuffle;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Retrieves elements from an array at the specified indices.
3
+ *
4
+ * This function supports negative indices, which count from the end of the array.
5
+ *
6
+ * @template T
7
+ * @param {readonly T[]} arr - The array to retrieve elements from.
8
+ * @param {number[]} indices - An array of indices specifying the positions of elements to retrieve.
9
+ * @returns {Array<T | undefined>} A new array containing the elements at the specified indices.
10
+ *
11
+ * @example
12
+ * const numbers = [10, 20, 30, 40, 50];
13
+ * const result = at(numbers, [1, 3, 4]);
14
+ * console.log(result); // [20, 40, 50]
15
+ */
16
+ declare function at<T>(arr: readonly T[], indices: number[]): Array<T | undefined>;
17
+
18
+ export { at };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Retrieves elements from an array at the specified indices.
3
+ *
4
+ * This function supports negative indices, which count from the end of the array.
5
+ *
6
+ * @template T
7
+ * @param {readonly T[]} arr - The array to retrieve elements from.
8
+ * @param {number[]} indices - An array of indices specifying the positions of elements to retrieve.
9
+ * @returns {Array<T | undefined>} A new array containing the elements at the specified indices.
10
+ *
11
+ * @example
12
+ * const numbers = [10, 20, 30, 40, 50];
13
+ * const result = at(numbers, [1, 3, 4]);
14
+ * console.log(result); // [20, 40, 50]
15
+ */
16
+ declare function at<T>(arr: readonly T[], indices: number[]): Array<T | undefined>;
17
+
18
+ export { at };
@@ -0,0 +1,10 @@
1
+ function at(arr, indices) {
2
+ const result = [];
3
+ for (let i = 0; i < indices.length; i++) {
4
+ const index = indices[i];
5
+ result.push(arr.at(index));
6
+ }
7
+ return result;
8
+ }
9
+
10
+ export { at };
@@ -3,18 +3,18 @@
3
3
  * based on a transformation function.
4
4
  *
5
5
  * This function takes an array and a transformation function
6
- * that converts each item in the array to a string. It then
6
+ * that converts each item in the array to a key. It then
7
7
  * counts the occurrences of each transformed item and returns
8
8
  * an object with the transformed items as keys and the counts
9
9
  * as values.
10
10
  *
11
11
  * @template T - The type of the items in the input array.
12
- *
12
+ * @template K - The type of keys.
13
13
  * @param {T[]} arr - The input array to count occurrences.
14
- * @param {(item: T) => string} mapper - The transformation function that maps each item to a string key.
15
- * @returns {Record<string, number>} An object containing the transformed items as keys and the
14
+ * @param {(item: T) => K} mapper - The transformation function that maps each item to a key.
15
+ * @returns {Record<K, number>} An object containing the transformed items as keys and the
16
16
  * counts as values.
17
17
  */
18
- declare function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number>;
18
+ declare function countBy<T, K extends PropertyKey>(arr: readonly T[], mapper: (item: T) => K): Record<K, number>;
19
19
 
20
20
  export { countBy };
@@ -3,18 +3,18 @@
3
3
  * based on a transformation function.
4
4
  *
5
5
  * This function takes an array and a transformation function
6
- * that converts each item in the array to a string. It then
6
+ * that converts each item in the array to a key. It then
7
7
  * counts the occurrences of each transformed item and returns
8
8
  * an object with the transformed items as keys and the counts
9
9
  * as values.
10
10
  *
11
11
  * @template T - The type of the items in the input array.
12
- *
12
+ * @template K - The type of keys.
13
13
  * @param {T[]} arr - The input array to count occurrences.
14
- * @param {(item: T) => string} mapper - The transformation function that maps each item to a string key.
15
- * @returns {Record<string, number>} An object containing the transformed items as keys and the
14
+ * @param {(item: T) => K} mapper - The transformation function that maps each item to a key.
15
+ * @returns {Record<K, number>} An object containing the transformed items as keys and the
16
16
  * counts as values.
17
17
  */
18
- declare function countBy<T>(arr: T[], mapper: (item: T) => string): Record<string, number>;
18
+ declare function countBy<T, K extends PropertyKey>(arr: readonly T[], mapper: (item: T) => K): Record<K, number>;
19
19
 
20
20
  export { countBy };
@@ -1,3 +1,4 @@
1
+ export { at } from './at.mjs';
1
2
  export { chunk } from './chunk.mjs';
2
3
  export { compact } from './compact.mjs';
3
4
  export { countBy } from './countBy.mjs';
@@ -22,6 +23,7 @@ export { maxBy } from './maxBy.mjs';
22
23
  export { minBy } from './minBy.mjs';
23
24
  export { orderBy } from './orderBy.mjs';
24
25
  export { partition } from './partition.mjs';
26
+ export { pullAt } from './pullAt.mjs';
25
27
  export { sortBy } from './sortBy.mjs';
26
28
  export { sample } from './sample.mjs';
27
29
  export { sampleSize } from './sampleSize.mjs';
@@ -1,3 +1,4 @@
1
+ export { at } from './at.js';
1
2
  export { chunk } from './chunk.js';
2
3
  export { compact } from './compact.js';
3
4
  export { countBy } from './countBy.js';
@@ -22,6 +23,7 @@ export { maxBy } from './maxBy.js';
22
23
  export { minBy } from './minBy.js';
23
24
  export { orderBy } from './orderBy.js';
24
25
  export { partition } from './partition.js';
26
+ export { pullAt } from './pullAt.js';
25
27
  export { sortBy } from './sortBy.js';
26
28
  export { sample } from './sample.js';
27
29
  export { sampleSize } from './sampleSize.js';
@@ -2,27 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const initial = require('../_chunk/initial-y0QrPY.js');
5
+ const initial = require('../_chunk/initial-Ci2bn_.js');
6
6
 
7
7
  function flattenDeep(arr) {
8
8
  return initial.flatten(arr, Infinity);
9
9
  }
10
10
 
11
- function orderBy(collection, keys, orders) {
12
- const effectiveOrders = keys.map((_, index) => orders[index] || orders[orders.length - 1]);
13
- return collection.slice().sort((a, b) => {
14
- for (let i = 0; i < keys.length; i++) {
15
- const key = keys[i];
16
- const order = effectiveOrders[i];
17
- const result = initial.compareValues(a[key], b[key], order);
18
- if (result !== 0) {
19
- return result;
20
- }
21
- }
22
- return 0;
23
- });
24
- }
25
-
11
+ exports.at = initial.at;
26
12
  exports.chunk = initial.chunk;
27
13
  exports.compact = initial.compact;
28
14
  exports.countBy = initial.countBy;
@@ -48,7 +34,9 @@ exports.keyBy = initial.keyBy;
48
34
  exports.last = initial.last;
49
35
  exports.maxBy = initial.maxBy;
50
36
  exports.minBy = initial.minBy;
37
+ exports.orderBy = initial.orderBy;
51
38
  exports.partition = initial.partition;
39
+ exports.pullAt = initial.pullAt;
52
40
  exports.sample = initial.sample;
53
41
  exports.sampleSize = initial.sampleSize;
54
42
  exports.shuffle = initial.shuffle;
@@ -75,4 +63,3 @@ exports.zip = initial.zip;
75
63
  exports.zipObject = initial.zipObject;
76
64
  exports.zipWith = initial.zipWith;
77
65
  exports.flattenDeep = flattenDeep;
78
- exports.orderBy = orderBy;
@@ -1,3 +1,4 @@
1
+ export { at } from './at.mjs';
1
2
  export { chunk } from './chunk.mjs';
2
3
  export { compact } from './compact.mjs';
3
4
  export { countBy } from './countBy.mjs';
@@ -22,6 +23,7 @@ export { maxBy } from './maxBy.mjs';
22
23
  export { minBy } from './minBy.mjs';
23
24
  export { orderBy } from './orderBy.mjs';
24
25
  export { partition } from './partition.mjs';
26
+ export { pullAt } from './pullAt.mjs';
25
27
  export { sortBy } from './sortBy.mjs';
26
28
  export { sample } from './sample.mjs';
27
29
  export { sampleSize } from './sampleSize.mjs';
@@ -21,6 +21,6 @@
21
21
  * const newSingleElementArr = initial(singleElementArr);
22
22
  * // newSingleElementArr will be []
23
23
  */
24
- declare function initial<T>(arr: T[]): T[];
24
+ declare function initial<T>(arr: readonly T[]): T[];
25
25
 
26
26
  export { initial };
@@ -21,6 +21,6 @@
21
21
  * const newSingleElementArr = initial(singleElementArr);
22
22
  * // newSingleElementArr will be []
23
23
  */
24
- declare function initial<T>(arr: T[]): T[];
24
+ declare function initial<T>(arr: readonly T[]): T[];
25
25
 
26
26
  export { initial };
@@ -1,7 +1,4 @@
1
1
  function initial(arr) {
2
- if (arr.length <= 1) {
3
- return [];
4
- }
5
2
  return arr.slice(0, -1);
6
3
  }
7
4
 
@@ -5,11 +5,12 @@
5
5
  * @template T - The type of elements in the array.
6
6
  * @param {T[]} items The array of elements to search.
7
7
  * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
8
- * @returns {T} The element with the maximum value as determined by the `getValue` function.
8
+ * @returns {T | undefined} The element with the maximum value as determined by the `getValue` function.
9
9
  * @example
10
10
  * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
11
11
  * maxBy([], x => x.a); // Returns: undefined
12
12
  */
13
- declare function maxBy<T>(items: T[], getValue: (element: T) => number): T;
13
+ declare function maxBy<T>(items: readonly [T, ...T[]], getValue: (element: T) => number): T;
14
+ declare function maxBy<T>(items: readonly T[], getValue: (element: T) => number): T | undefined;
14
15
 
15
16
  export { maxBy };
@@ -5,11 +5,12 @@
5
5
  * @template T - The type of elements in the array.
6
6
  * @param {T[]} items The array of elements to search.
7
7
  * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
8
- * @returns {T} The element with the maximum value as determined by the `getValue` function.
8
+ * @returns {T | undefined} The element with the maximum value as determined by the `getValue` function.
9
9
  * @example
10
10
  * maxBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 3 }
11
11
  * maxBy([], x => x.a); // Returns: undefined
12
12
  */
13
- declare function maxBy<T>(items: T[], getValue: (element: T) => number): T;
13
+ declare function maxBy<T>(items: readonly [T, ...T[]], getValue: (element: T) => number): T;
14
+ declare function maxBy<T>(items: readonly T[], getValue: (element: T) => number): T | undefined;
14
15
 
15
16
  export { maxBy };
@@ -5,11 +5,12 @@
5
5
  * @template T - The type of elements in the array.
6
6
  * @param {T[]} items The array of elements to search.
7
7
  * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
8
- * @returns {T} The element with the minimum value as determined by the `getValue` function.
8
+ * @returns {T | undefined} The element with the minimum value as determined by the `getValue` function.
9
9
  * @example
10
10
  * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
11
11
  * minBy([], x => x.a); // Returns: undefined
12
12
  */
13
- declare function minBy<T>(items: T[], getValue: (element: T) => number): T;
13
+ declare function minBy<T>(items: readonly [T, ...T[]], getValue: (element: T) => number): T;
14
+ declare function minBy<T>(items: readonly T[], getValue: (element: T) => number): T | undefined;
14
15
 
15
16
  export { minBy };
@@ -5,11 +5,12 @@
5
5
  * @template T - The type of elements in the array.
6
6
  * @param {T[]} items The array of elements to search.
7
7
  * @param {(element: T) => number} getValue A function that selects a numeric value from each element.
8
- * @returns {T} The element with the minimum value as determined by the `getValue` function.
8
+ * @returns {T | undefined} The element with the minimum value as determined by the `getValue` function.
9
9
  * @example
10
10
  * minBy([{ a: 1 }, { a: 2 }, { a: 3 }], x => x.a); // Returns: { a: 1 }
11
11
  * minBy([], x => x.a); // Returns: undefined
12
12
  */
13
- declare function minBy<T>(items: T[], getValue: (element: T) => number): T;
13
+ declare function minBy<T>(items: readonly [T, ...T[]], getValue: (element: T) => number): T;
14
+ declare function minBy<T>(items: readonly T[], getValue: (element: T) => number): T | undefined;
14
15
 
15
16
  export { minBy };
@@ -1,16 +1,17 @@
1
- type Order = 'asc' | 'desc';
2
1
  /**
3
- * Sorts an array of objects based on multiple properties and their corresponding order directions.
2
+ * Sorts an array of objects based on the given `criteria` and their corresponding order directions.
4
3
  *
5
- * This function takes an array of objects, an array of keys to sort by, and an array of order directions.
6
- * It returns the sorted array, ordering by each key according to its corresponding direction
7
- * ('asc' for ascending or 'desc' for descending). If values for a key are equal,
8
- * it moves to the next key to determine the order.
4
+ * - If you provide keys, it sorts the objects by the values of those keys.
5
+ * - If you provide functions, it sorts based on the values returned by those functions.
6
+ *
7
+ * The function returns the array of objects sorted in corresponding order directions.
8
+ * If two objects have the same value for the current criterion, it uses the next criterion to determine their order.
9
+ * If the number of orders is less than the number of criteria, it uses the last order for the rest of the criteria.
9
10
  *
10
11
  * @template T - The type of elements in the array.
11
- * @param {T[]} collection - The array of objects to be sorted.
12
- * @param {Array<keyof T>} keys - An array of keys (properties) by which to sort.
13
- * @param {Order[]} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
12
+ * @param {T[]} arr - The array of objects to be sorted.
13
+ * @param {Array<((item: T) => unknown) | keyof T>} criteria - The criteria for sorting. This can be an array of object keys or functions that return values used for sorting.
14
+ * @param {Array<'asc' | 'desc'>} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
14
15
  * @returns {T[]} - The sorted array.
15
16
  *
16
17
  * @example
@@ -21,7 +22,8 @@ type Order = 'asc' | 'desc';
21
22
  * { user: 'fred', age: 40 },
22
23
  * { user: 'barney', age: 36 },
23
24
  * ];
24
- * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);
25
+ *
26
+ * const result = orderBy(users, [obj => obj.user, 'age'], ['asc', 'desc']);
25
27
  * // result will be:
26
28
  * // [
27
29
  * // { user: 'barney', age: 36 },
@@ -30,6 +32,6 @@ type Order = 'asc' | 'desc';
30
32
  * // { user: 'fred', age: 40 },
31
33
  * // ]
32
34
  */
33
- declare function orderBy<T>(collection: T[], keys: Array<keyof T>, orders: Order[]): T[];
35
+ declare function orderBy<T extends object>(arr: T[], criteria: Array<((item: T) => unknown) | keyof T>, orders: Array<'asc' | 'desc'>): T[];
34
36
 
35
37
  export { orderBy };
@@ -1,16 +1,17 @@
1
- type Order = 'asc' | 'desc';
2
1
  /**
3
- * Sorts an array of objects based on multiple properties and their corresponding order directions.
2
+ * Sorts an array of objects based on the given `criteria` and their corresponding order directions.
4
3
  *
5
- * This function takes an array of objects, an array of keys to sort by, and an array of order directions.
6
- * It returns the sorted array, ordering by each key according to its corresponding direction
7
- * ('asc' for ascending or 'desc' for descending). If values for a key are equal,
8
- * it moves to the next key to determine the order.
4
+ * - If you provide keys, it sorts the objects by the values of those keys.
5
+ * - If you provide functions, it sorts based on the values returned by those functions.
6
+ *
7
+ * The function returns the array of objects sorted in corresponding order directions.
8
+ * If two objects have the same value for the current criterion, it uses the next criterion to determine their order.
9
+ * If the number of orders is less than the number of criteria, it uses the last order for the rest of the criteria.
9
10
  *
10
11
  * @template T - The type of elements in the array.
11
- * @param {T[]} collection - The array of objects to be sorted.
12
- * @param {Array<keyof T>} keys - An array of keys (properties) by which to sort.
13
- * @param {Order[]} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
12
+ * @param {T[]} arr - The array of objects to be sorted.
13
+ * @param {Array<((item: T) => unknown) | keyof T>} criteria - The criteria for sorting. This can be an array of object keys or functions that return values used for sorting.
14
+ * @param {Array<'asc' | 'desc'>} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
14
15
  * @returns {T[]} - The sorted array.
15
16
  *
16
17
  * @example
@@ -21,7 +22,8 @@ type Order = 'asc' | 'desc';
21
22
  * { user: 'fred', age: 40 },
22
23
  * { user: 'barney', age: 36 },
23
24
  * ];
24
- * const result = orderBy(users, ['user', 'age'], ['asc', 'desc']);
25
+ *
26
+ * const result = orderBy(users, [obj => obj.user, 'age'], ['asc', 'desc']);
25
27
  * // result will be:
26
28
  * // [
27
29
  * // { user: 'barney', age: 36 },
@@ -30,6 +32,6 @@ type Order = 'asc' | 'desc';
30
32
  * // { user: 'fred', age: 40 },
31
33
  * // ]
32
34
  */
33
- declare function orderBy<T>(collection: T[], keys: Array<keyof T>, orders: Order[]): T[];
35
+ declare function orderBy<T extends object>(arr: T[], criteria: Array<((item: T) => unknown) | keyof T>, orders: Array<'asc' | 'desc'>): T[];
34
36
 
35
37
  export { orderBy };
@@ -1,12 +1,15 @@
1
1
  import { compareValues } from '../_internal/compareValues.mjs';
2
2
 
3
- function orderBy(collection, keys, orders) {
4
- const effectiveOrders = keys.map((_, index) => orders[index] || orders[orders.length - 1]);
5
- return collection.slice().sort((a, b) => {
6
- for (let i = 0; i < keys.length; i++) {
7
- const key = keys[i];
8
- const order = effectiveOrders[i];
9
- const result = compareValues(a[key], b[key], order);
3
+ function orderBy(arr, criteria, orders) {
4
+ return arr.slice().sort((a, b) => {
5
+ const ordersLength = orders.length;
6
+ for (let i = 0; i < criteria.length; i++) {
7
+ const order = ordersLength > i ? orders[i] : orders[ordersLength - 1];
8
+ const criterion = criteria[i];
9
+ const criterionIsFunction = typeof criterion === 'function';
10
+ const valueA = criterionIsFunction ? criterion(a) : a[criterion];
11
+ const valueB = criterionIsFunction ? criterion(b) : b[criterion];
12
+ const result = compareValues(valueA, valueB, order);
10
13
  if (result !== 0) {
11
14
  return result;
12
15
  }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Removes elements from an array at specified indices and returns the removed elements.
3
+ *
4
+ * This function supports negative indices, which count from the end of the array.
5
+ *
6
+ * @template T
7
+ * @param {T[]} arr - The array from which elements will be removed.
8
+ * @param {number[]} indicesToRemove - An array of indices specifying the positions of elements to remove.
9
+ * @returns {Array<T | undefined>} An array containing the elements that were removed from the original array.
10
+ *
11
+ * @example
12
+ * import { pullAt } from './pullAt';
13
+ *
14
+ * const numbers = [10, 20, 30, 40, 50];
15
+ * const removed = pullAt(numbers, [1, 3, 4]);
16
+ * console.log(removed); // [20, 40, 50]
17
+ * console.log(numbers); // [10, 30]
18
+ */
19
+ declare function pullAt<T>(arr: T[], indicesToRemove: number[]): Array<T | undefined>;
20
+
21
+ export { pullAt };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Removes elements from an array at specified indices and returns the removed elements.
3
+ *
4
+ * This function supports negative indices, which count from the end of the array.
5
+ *
6
+ * @template T
7
+ * @param {T[]} arr - The array from which elements will be removed.
8
+ * @param {number[]} indicesToRemove - An array of indices specifying the positions of elements to remove.
9
+ * @returns {Array<T | undefined>} An array containing the elements that were removed from the original array.
10
+ *
11
+ * @example
12
+ * import { pullAt } from './pullAt';
13
+ *
14
+ * const numbers = [10, 20, 30, 40, 50];
15
+ * const removed = pullAt(numbers, [1, 3, 4]);
16
+ * console.log(removed); // [20, 40, 50]
17
+ * console.log(numbers); // [10, 30]
18
+ */
19
+ declare function pullAt<T>(arr: T[], indicesToRemove: number[]): Array<T | undefined>;
20
+
21
+ export { pullAt };
@@ -0,0 +1,12 @@
1
+ import { at } from './at.mjs';
2
+
3
+ function pullAt(arr, indicesToRemove) {
4
+ const removed = at(arr, indicesToRemove);
5
+ const indices = new Set(indicesToRemove.slice().sort((x, y) => y - x));
6
+ for (const index of indices) {
7
+ arr.splice(index, 1);
8
+ }
9
+ return removed;
10
+ }
11
+
12
+ export { pullAt };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * This function takes an array and a number, and returns an array containing the sampled elements using Floyd's algorithm.
5
5
  *
6
- * {@link https://www.nowherenearithaca.com/2013/05/robert-floyds-tiny-and-beautiful.html Floyd's algoritm}
6
+ * {@link https://www.nowherenearithaca.com/2013/05/robert-floyds-tiny-and-beautiful.html Floyd's algorithm}
7
7
  *
8
8
  * @template T - The type of elements in the array.
9
9
  * @param {T[]} array - The array to sample from.
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * This function takes an array and a number, and returns an array containing the sampled elements using Floyd's algorithm.
5
5
  *
6
- * {@link https://www.nowherenearithaca.com/2013/05/robert-floyds-tiny-and-beautiful.html Floyd's algoritm}
6
+ * {@link https://www.nowherenearithaca.com/2013/05/robert-floyds-tiny-and-beautiful.html Floyd's algorithm}
7
7
  *
8
8
  * @template T - The type of elements in the array.
9
9
  * @param {T[]} array - The array to sample from.