es-toolkit 1.30.1 → 1.31.0-dev.989
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/CHANGELOG.md +13 -0
- package/dist/_chunk/{unary-CcTNuC.js → unary-B6qG7C.js} +3 -0
- package/dist/_chunk/{zipWith-nbzldx.js → zipWith-Bdyzuy.js} +47 -4
- package/dist/array/groupBy.mjs +2 -2
- package/dist/array/index.d.mts +2 -0
- package/dist/array/index.d.ts +2 -0
- package/dist/array/index.js +3 -1
- package/dist/array/index.mjs +2 -0
- package/dist/array/pull.mjs +9 -2
- package/dist/array/remove.d.mts +19 -0
- package/dist/array/remove.d.ts +19 -0
- package/dist/array/remove.mjs +20 -0
- package/dist/array/windowed.d.mts +50 -0
- package/dist/array/windowed.d.ts +50 -0
- package/dist/array/windowed.mjs +16 -0
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/pull.d.mts +1 -1
- package/dist/compat/array/pull.d.ts +1 -1
- package/dist/compat/array/pull.mjs +1 -2
- package/dist/compat/array/pullAll.d.mts +19 -0
- package/dist/compat/array/pullAll.d.ts +19 -0
- package/dist/compat/array/pullAll.mjs +7 -0
- package/dist/compat/array/remove.d.mts +50 -0
- package/dist/compat/array/remove.d.ts +50 -0
- package/dist/compat/array/remove.mjs +8 -0
- package/dist/compat/array/reverse.d.mts +24 -0
- package/dist/compat/array/reverse.d.ts +24 -0
- package/dist/compat/array/reverse.mjs +8 -0
- package/dist/compat/function/negate.d.mts +16 -0
- package/dist/compat/function/negate.d.ts +16 -0
- package/dist/compat/function/negate.mjs +10 -0
- package/dist/compat/index.d.mts +9 -2
- package/dist/compat/index.d.ts +9 -2
- package/dist/compat/index.js +56 -11
- package/dist/compat/index.mjs +9 -2
- package/dist/compat/math/subtract.d.mts +17 -0
- package/dist/compat/math/subtract.d.ts +17 -0
- package/dist/compat/math/subtract.mjs +5 -0
- package/dist/compat/object/mergeWith.mjs +6 -2
- package/dist/compat/predicate/isBuffer.d.mts +21 -0
- package/dist/compat/predicate/isBuffer.d.ts +21 -0
- package/dist/compat/predicate/isBuffer.mjs +7 -0
- package/dist/compat/predicate/isMatch.mjs +1 -4
- package/dist/compat/util/methodOf.d.mts +22 -0
- package/dist/compat/util/methodOf.d.ts +22 -0
- package/dist/compat/util/methodOf.mjs +9 -0
- package/dist/function/asyncNoop.d.mts +12 -0
- package/dist/function/asyncNoop.d.ts +12 -0
- package/dist/function/asyncNoop.mjs +3 -0
- package/dist/function/index.d.mts +1 -0
- package/dist/function/index.d.ts +1 -0
- package/dist/function/index.js +2 -1
- package/dist/function/index.mjs +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -2
- package/dist/index.mjs +3 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# es-toolkit Changelog
|
|
2
2
|
|
|
3
|
+
## Version v1.31.0
|
|
4
|
+
|
|
5
|
+
Released on December 27th, 2024.
|
|
6
|
+
|
|
7
|
+
- Added support for the [windowed](https://es-toolkit.slash.page/reference/array/windowed.html), [remove](https://es-toolkit.slash.page/reference/array/remove.html) and [asyncNoop](https://es-toolkit.slash.page/reference/function/asyncNoop.html) functions.
|
|
8
|
+
- Introduced compatibility functions for [pullAll](https://es-toolkit.slash.page/reference/compat/array/pullAll.html), [subtract](https://es-toolkit.slash.page/reference/compat/math/subtract.html), [isBuffer](https://es-toolkit.slash.page/reference/predicate/isBuffer.html), and [methodOf](https://es-toolkit.slash.page/reference/compat/util/methodOf.html).
|
|
9
|
+
- Enhanced the performance of [pull](https://es-toolkit.slash.page/reference/array/pull.html) when working with large arrays.
|
|
10
|
+
- Resolved an issue where [reverse](https://es-toolkit.slash.page/reference/compat/array/reverse.html) was not being exported in our compatibility library.
|
|
11
|
+
- Updated [groupBy](https://es-toolkit.slash.page/reference/array/groupBy.html) to properly handle keys like `toString` or `valueOf`.
|
|
12
|
+
- Fixed [merge](https://es-toolkit.slash.page/reference/object/merge.html) to correctly merge values when `target` or any of its values are `null` or `undefined`.
|
|
13
|
+
|
|
14
|
+
We sincerely thank @T3sT3ro, @D-Sketon, @tuhm1, @willmanduffy, @apeltop, @aken-you, @SaeWooKKang, and @ssi02014 for their contributions. We appreciate your great efforts!
|
|
15
|
+
|
|
3
16
|
## Version v1.30.1
|
|
4
17
|
|
|
5
18
|
Released on December 14th, 2024.
|
|
@@ -19,6 +19,8 @@ function ary(func, n) {
|
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
async function asyncNoop() { }
|
|
23
|
+
|
|
22
24
|
function debounce(func, debounceMs, { signal, edges } = {}) {
|
|
23
25
|
let pendingThis = undefined;
|
|
24
26
|
let pendingArgs = null;
|
|
@@ -193,6 +195,7 @@ function unary(func) {
|
|
|
193
195
|
|
|
194
196
|
exports.after = after;
|
|
195
197
|
exports.ary = ary;
|
|
198
|
+
exports.asyncNoop = asyncNoop;
|
|
196
199
|
exports.debounce = debounce;
|
|
197
200
|
exports.flow = flow;
|
|
198
201
|
exports.flowRight = flowRight;
|
|
@@ -149,11 +149,11 @@ function forEachRight(arr, callback) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
function groupBy(arr, getKeyFromItem) {
|
|
152
|
-
const result =
|
|
152
|
+
const result = {};
|
|
153
153
|
for (let i = 0; i < arr.length; i++) {
|
|
154
154
|
const item = arr[i];
|
|
155
155
|
const key = getKeyFromItem(item);
|
|
156
|
-
if (result
|
|
156
|
+
if (!Object.hasOwn(result, key)) {
|
|
157
157
|
result[key] = [];
|
|
158
158
|
}
|
|
159
159
|
result[key].push(item);
|
|
@@ -256,11 +256,18 @@ function partition(arr, isInTruthy) {
|
|
|
256
256
|
|
|
257
257
|
function pull(arr, valuesToRemove) {
|
|
258
258
|
const valuesSet = new Set(valuesToRemove);
|
|
259
|
-
|
|
259
|
+
let resultIndex = 0;
|
|
260
|
+
for (let i = 0; i < arr.length; i++) {
|
|
260
261
|
if (valuesSet.has(arr[i])) {
|
|
261
|
-
|
|
262
|
+
continue;
|
|
262
263
|
}
|
|
264
|
+
if (!Object.hasOwn(arr, i)) {
|
|
265
|
+
delete arr[resultIndex++];
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
arr[resultIndex++] = arr[i];
|
|
263
269
|
}
|
|
270
|
+
arr.length = resultIndex;
|
|
264
271
|
return arr;
|
|
265
272
|
}
|
|
266
273
|
|
|
@@ -273,6 +280,25 @@ function pullAt(arr, indicesToRemove) {
|
|
|
273
280
|
return removed;
|
|
274
281
|
}
|
|
275
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
|
+
|
|
276
302
|
function sample(arr) {
|
|
277
303
|
const randomIndex = Math.floor(Math.random() * arr.length);
|
|
278
304
|
return arr[randomIndex];
|
|
@@ -412,6 +438,21 @@ function unzipWith(target, iteratee) {
|
|
|
412
438
|
return result;
|
|
413
439
|
}
|
|
414
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
|
+
|
|
415
456
|
function without(array, ...values) {
|
|
416
457
|
return difference(array, values);
|
|
417
458
|
}
|
|
@@ -503,6 +544,7 @@ exports.minBy = minBy;
|
|
|
503
544
|
exports.partition = partition;
|
|
504
545
|
exports.pull = pull;
|
|
505
546
|
exports.pullAt = pullAt;
|
|
547
|
+
exports.remove = remove;
|
|
506
548
|
exports.sample = sample;
|
|
507
549
|
exports.sampleSize = sampleSize;
|
|
508
550
|
exports.shuffle = shuffle;
|
|
@@ -519,6 +561,7 @@ exports.uniqBy = uniqBy;
|
|
|
519
561
|
exports.uniqWith = uniqWith;
|
|
520
562
|
exports.unzip = unzip;
|
|
521
563
|
exports.unzipWith = unzipWith;
|
|
564
|
+
exports.windowed = windowed;
|
|
522
565
|
exports.without = without;
|
|
523
566
|
exports.xor = xor;
|
|
524
567
|
exports.xorBy = xorBy;
|
package/dist/array/groupBy.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
function groupBy(arr, getKeyFromItem) {
|
|
2
|
-
const result =
|
|
2
|
+
const result = {};
|
|
3
3
|
for (let i = 0; i < arr.length; i++) {
|
|
4
4
|
const item = arr[i];
|
|
5
5
|
const key = getKeyFromItem(item);
|
|
6
|
-
if (result
|
|
6
|
+
if (!Object.hasOwn(result, key)) {
|
|
7
7
|
result[key] = [];
|
|
8
8
|
}
|
|
9
9
|
result[key].push(item);
|
package/dist/array/index.d.mts
CHANGED
|
@@ -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';
|
package/dist/array/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/array/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const zipWith = require('../_chunk/zipWith-
|
|
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;
|
package/dist/array/index.mjs
CHANGED
|
@@ -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';
|
package/dist/array/pull.mjs
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
function pull(arr, valuesToRemove) {
|
|
2
2
|
const valuesSet = new Set(valuesToRemove);
|
|
3
|
-
|
|
3
|
+
let resultIndex = 0;
|
|
4
|
+
for (let i = 0; i < arr.length; i++) {
|
|
4
5
|
if (valuesSet.has(arr[i])) {
|
|
5
|
-
|
|
6
|
+
continue;
|
|
6
7
|
}
|
|
8
|
+
if (!Object.hasOwn(arr, i)) {
|
|
9
|
+
delete arr[resultIndex++];
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
arr[resultIndex++] = arr[i];
|
|
7
13
|
}
|
|
14
|
+
arr.length = resultIndex;
|
|
8
15
|
return arr;
|
|
9
16
|
}
|
|
10
17
|
|
|
@@ -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 };
|