es-toolkit 1.30.1-dev.983 → 1.30.1-dev.985

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.
@@ -280,6 +280,25 @@ function pullAt(arr, indicesToRemove) {
280
280
  return removed;
281
281
  }
282
282
 
283
+ function remove(arr, shouldRemoveElement) {
284
+ const originalArr = arr.slice();
285
+ const removed = [];
286
+ let resultIndex = 0;
287
+ for (let i = 0; i < arr.length; i++) {
288
+ if (shouldRemoveElement(arr[i], i, originalArr)) {
289
+ removed.push(arr[i]);
290
+ continue;
291
+ }
292
+ if (!Object.hasOwn(arr, i)) {
293
+ delete arr[resultIndex++];
294
+ continue;
295
+ }
296
+ arr[resultIndex++] = arr[i];
297
+ }
298
+ arr.length = resultIndex;
299
+ return removed;
300
+ }
301
+
283
302
  function sample(arr) {
284
303
  const randomIndex = Math.floor(Math.random() * arr.length);
285
304
  return arr[randomIndex];
@@ -419,6 +438,21 @@ function unzipWith(target, iteratee) {
419
438
  return result;
420
439
  }
421
440
 
441
+ function windowed(arr, size, step = 1, { partialWindows = false } = {}) {
442
+ if (size <= 0 || !Number.isInteger(size)) {
443
+ throw new Error('Size must be a positive integer.');
444
+ }
445
+ if (step <= 0 || !Number.isInteger(step)) {
446
+ throw new Error('Step must be a positive integer.');
447
+ }
448
+ const result = [];
449
+ const end = partialWindows ? arr.length : arr.length - size + 1;
450
+ for (let i = 0; i < end; i += step) {
451
+ result.push(arr.slice(i, i + size));
452
+ }
453
+ return result;
454
+ }
455
+
422
456
  function without(array, ...values) {
423
457
  return difference(array, values);
424
458
  }
@@ -510,6 +544,7 @@ exports.minBy = minBy;
510
544
  exports.partition = partition;
511
545
  exports.pull = pull;
512
546
  exports.pullAt = pullAt;
547
+ exports.remove = remove;
513
548
  exports.sample = sample;
514
549
  exports.sampleSize = sampleSize;
515
550
  exports.shuffle = shuffle;
@@ -526,6 +561,7 @@ exports.uniqBy = uniqBy;
526
561
  exports.uniqWith = uniqWith;
527
562
  exports.unzip = unzip;
528
563
  exports.unzipWith = unzipWith;
564
+ exports.windowed = windowed;
529
565
  exports.without = without;
530
566
  exports.xor = xor;
531
567
  exports.xorBy = xorBy;
@@ -31,6 +31,7 @@ export { orderBy } from './orderBy.mjs';
31
31
  export { partition } from './partition.mjs';
32
32
  export { pull } from './pull.mjs';
33
33
  export { pullAt } from './pullAt.mjs';
34
+ export { remove } from './remove.mjs';
34
35
  export { sample } from './sample.mjs';
35
36
  export { sampleSize } from './sampleSize.mjs';
36
37
  export { shuffle } from './shuffle.mjs';
@@ -49,6 +50,7 @@ export { uniqBy } from './uniqBy.mjs';
49
50
  export { uniqWith } from './uniqWith.mjs';
50
51
  export { unzip } from './unzip.mjs';
51
52
  export { unzipWith } from './unzipWith.mjs';
53
+ export { windowed } from './windowed.mjs';
52
54
  export { without } from './without.mjs';
53
55
  export { xor } from './xor.mjs';
54
56
  export { xorBy } from './xorBy.mjs';
@@ -31,6 +31,7 @@ export { orderBy } from './orderBy.js';
31
31
  export { partition } from './partition.js';
32
32
  export { pull } from './pull.js';
33
33
  export { pullAt } from './pullAt.js';
34
+ export { remove } from './remove.js';
34
35
  export { sample } from './sample.js';
35
36
  export { sampleSize } from './sampleSize.js';
36
37
  export { shuffle } from './shuffle.js';
@@ -49,6 +50,7 @@ export { uniqBy } from './uniqBy.js';
49
50
  export { uniqWith } from './uniqWith.js';
50
51
  export { unzip } from './unzip.js';
51
52
  export { unzipWith } from './unzipWith.js';
53
+ export { windowed } from './windowed.js';
52
54
  export { without } from './without.js';
53
55
  export { xor } from './xor.js';
54
56
  export { xorBy } from './xorBy.js';
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const zipWith = require('../_chunk/zipWith-CviBZV.js');
5
+ const zipWith = require('../_chunk/zipWith-Bdyzuy.js');
6
6
 
7
7
  function compareValues(a, b, order) {
8
8
  if (a < b) {
@@ -77,6 +77,7 @@ exports.minBy = zipWith.minBy;
77
77
  exports.partition = zipWith.partition;
78
78
  exports.pull = zipWith.pull;
79
79
  exports.pullAt = zipWith.pullAt;
80
+ exports.remove = zipWith.remove;
80
81
  exports.sample = zipWith.sample;
81
82
  exports.sampleSize = zipWith.sampleSize;
82
83
  exports.shuffle = zipWith.shuffle;
@@ -93,6 +94,7 @@ exports.uniqBy = zipWith.uniqBy;
93
94
  exports.uniqWith = zipWith.uniqWith;
94
95
  exports.unzip = zipWith.unzip;
95
96
  exports.unzipWith = zipWith.unzipWith;
97
+ exports.windowed = zipWith.windowed;
96
98
  exports.without = zipWith.without;
97
99
  exports.xor = zipWith.xor;
98
100
  exports.xorBy = zipWith.xorBy;
@@ -31,6 +31,7 @@ export { orderBy } from './orderBy.mjs';
31
31
  export { partition } from './partition.mjs';
32
32
  export { pull } from './pull.mjs';
33
33
  export { pullAt } from './pullAt.mjs';
34
+ export { remove } from './remove.mjs';
34
35
  export { sample } from './sample.mjs';
35
36
  export { sampleSize } from './sampleSize.mjs';
36
37
  export { shuffle } from './shuffle.mjs';
@@ -49,6 +50,7 @@ export { uniqBy } from './uniqBy.mjs';
49
50
  export { uniqWith } from './uniqWith.mjs';
50
51
  export { unzip } from './unzip.mjs';
51
52
  export { unzipWith } from './unzipWith.mjs';
53
+ export { windowed } from './windowed.mjs';
52
54
  export { without } from './without.mjs';
53
55
  export { xor } from './xor.mjs';
54
56
  export { xorBy } from './xorBy.mjs';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Removes elements from an array based on a predicate function.
3
+ *
4
+ * This function changes `arr` in place.
5
+ * If you want to remove elements without modifying the original array, use `filter`.
6
+ *
7
+ * @template T
8
+ * @param {T[]} arr - The array to modify.
9
+ * @param {(value: T, index: number, array: T[]) => boolean} shouldRemoveElement - The function invoked per iteration to determine if an element should be removed.
10
+ * @returns {T[]} The modified array with the specified elements removed.
11
+ *
12
+ * @example
13
+ * const numbers = [1, 2, 3, 4, 5];
14
+ * remove(numbers, (value) => value % 2 === 0);
15
+ * console.log(numbers); // [1, 3, 5]
16
+ */
17
+ declare function remove<T>(arr: T[], shouldRemoveElement: (value: T, index: number, array: T[]) => boolean): T[];
18
+
19
+ export { remove };
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Removes elements from an array based on a predicate function.
3
+ *
4
+ * This function changes `arr` in place.
5
+ * If you want to remove elements without modifying the original array, use `filter`.
6
+ *
7
+ * @template T
8
+ * @param {T[]} arr - The array to modify.
9
+ * @param {(value: T, index: number, array: T[]) => boolean} shouldRemoveElement - The function invoked per iteration to determine if an element should be removed.
10
+ * @returns {T[]} The modified array with the specified elements removed.
11
+ *
12
+ * @example
13
+ * const numbers = [1, 2, 3, 4, 5];
14
+ * remove(numbers, (value) => value % 2 === 0);
15
+ * console.log(numbers); // [1, 3, 5]
16
+ */
17
+ declare function remove<T>(arr: T[], shouldRemoveElement: (value: T, index: number, array: T[]) => boolean): T[];
18
+
19
+ export { remove };
@@ -0,0 +1,20 @@
1
+ function remove(arr, shouldRemoveElement) {
2
+ const originalArr = arr.slice();
3
+ const removed = [];
4
+ let resultIndex = 0;
5
+ for (let i = 0; i < arr.length; i++) {
6
+ if (shouldRemoveElement(arr[i], i, originalArr)) {
7
+ removed.push(arr[i]);
8
+ continue;
9
+ }
10
+ if (!Object.hasOwn(arr, i)) {
11
+ delete arr[resultIndex++];
12
+ continue;
13
+ }
14
+ arr[resultIndex++] = arr[i];
15
+ }
16
+ arr.length = resultIndex;
17
+ return removed;
18
+ }
19
+
20
+ export { remove };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Options for the windowed function.
3
+ *
4
+ * @interface WindowedOptions
5
+ * @property {boolean} [partialWindows=false] - Whether to include partial windows at the end of the array.
6
+ */
7
+ interface WindowedOptions {
8
+ /**
9
+ * Whether to include partial windows at the end of the array.
10
+ *
11
+ * By default, `windowed` only includes full windows in the result,
12
+ * ignoring any leftover elements that can't form a full window.
13
+ *
14
+ * If `partialWindows` is true, the function will also include these smaller, partial windows at the end of the result.
15
+ */
16
+ partialWindows?: boolean;
17
+ }
18
+ /**
19
+ * Creates an array of sub-arrays (windows) from the input array, each of the specified size.
20
+ * The windows can overlap depending on the step size provided.
21
+ *
22
+ * By default, only full windows are included in the result, and any leftover elements that can't form a full window are ignored.
23
+ *
24
+ * If the `partialWindows` option is set to true in the options object, the function will also include partial windows at the end of the result.
25
+ * Partial windows are smaller sub-arrays created when there aren't enough elements left in the input array to form a full window.
26
+ *
27
+ * @template T
28
+ * @param {readonly T[]} arr - The input array to create windows from.
29
+ * @param {number} size - The size of each window. Must be a positive integer.
30
+ * @param {number} [step=1] - The step size between the start of each window. Must be a positive integer.
31
+ * @param {WindowedOptions} [options={}] - Options object to configure the behavior of the function.
32
+ * @param {boolean} [options.partialWindows=false] - Whether to include partial windows at the end of the array.
33
+ * @returns {T[][]} An array of windows (sub-arrays) created from the input array.
34
+ * @throws {Error} If the size or step is not a positive integer.
35
+ *
36
+ * @example
37
+ * windowed([1, 2, 3, 4], 2);
38
+ * // => [[1, 2], [2, 3], [3, 4]]
39
+ *
40
+ * @example
41
+ * windowed([1, 2, 3, 4, 5, 6], 3, 2);
42
+ * // => [[1, 2, 3], [3, 4, 5]]
43
+ *
44
+ * @example
45
+ * windowed([1, 2, 3, 4, 5, 6], 3, 2, { partialWindows: true });
46
+ * // => [[1, 2, 3], [3, 4, 5], [5, 6]]
47
+ */
48
+ declare function windowed<T>(arr: readonly T[], size: number, step?: number, { partialWindows }?: WindowedOptions): T[][];
49
+
50
+ export { type WindowedOptions, windowed };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Options for the windowed function.
3
+ *
4
+ * @interface WindowedOptions
5
+ * @property {boolean} [partialWindows=false] - Whether to include partial windows at the end of the array.
6
+ */
7
+ interface WindowedOptions {
8
+ /**
9
+ * Whether to include partial windows at the end of the array.
10
+ *
11
+ * By default, `windowed` only includes full windows in the result,
12
+ * ignoring any leftover elements that can't form a full window.
13
+ *
14
+ * If `partialWindows` is true, the function will also include these smaller, partial windows at the end of the result.
15
+ */
16
+ partialWindows?: boolean;
17
+ }
18
+ /**
19
+ * Creates an array of sub-arrays (windows) from the input array, each of the specified size.
20
+ * The windows can overlap depending on the step size provided.
21
+ *
22
+ * By default, only full windows are included in the result, and any leftover elements that can't form a full window are ignored.
23
+ *
24
+ * If the `partialWindows` option is set to true in the options object, the function will also include partial windows at the end of the result.
25
+ * Partial windows are smaller sub-arrays created when there aren't enough elements left in the input array to form a full window.
26
+ *
27
+ * @template T
28
+ * @param {readonly T[]} arr - The input array to create windows from.
29
+ * @param {number} size - The size of each window. Must be a positive integer.
30
+ * @param {number} [step=1] - The step size between the start of each window. Must be a positive integer.
31
+ * @param {WindowedOptions} [options={}] - Options object to configure the behavior of the function.
32
+ * @param {boolean} [options.partialWindows=false] - Whether to include partial windows at the end of the array.
33
+ * @returns {T[][]} An array of windows (sub-arrays) created from the input array.
34
+ * @throws {Error} If the size or step is not a positive integer.
35
+ *
36
+ * @example
37
+ * windowed([1, 2, 3, 4], 2);
38
+ * // => [[1, 2], [2, 3], [3, 4]]
39
+ *
40
+ * @example
41
+ * windowed([1, 2, 3, 4, 5, 6], 3, 2);
42
+ * // => [[1, 2, 3], [3, 4, 5]]
43
+ *
44
+ * @example
45
+ * windowed([1, 2, 3, 4, 5, 6], 3, 2, { partialWindows: true });
46
+ * // => [[1, 2, 3], [3, 4, 5], [5, 6]]
47
+ */
48
+ declare function windowed<T>(arr: readonly T[], size: number, step?: number, { partialWindows }?: WindowedOptions): T[][];
49
+
50
+ export { type WindowedOptions, windowed };
@@ -0,0 +1,16 @@
1
+ function windowed(arr, size, step = 1, { partialWindows = false } = {}) {
2
+ if (size <= 0 || !Number.isInteger(size)) {
3
+ throw new Error('Size must be a positive integer.');
4
+ }
5
+ if (step <= 0 || !Number.isInteger(step)) {
6
+ throw new Error('Step must be a positive integer.');
7
+ }
8
+ const result = [];
9
+ const end = partialWindows ? arr.length : arr.length - size + 1;
10
+ for (let i = 0; i < end; i += step) {
11
+ result.push(arr.slice(i, i + size));
12
+ }
13
+ return result;
14
+ }
15
+
16
+ export { windowed };