Skip to content

Commit

Permalink
feat: Introduce movingWindow in fp
Browse files Browse the repository at this point in the history
  • Loading branch information
Iku-turso committed Jan 15, 2023
1 parent 88f0617 commit 1f212c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/fp/src/movingWindow/movingWindow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default windowSize => things =>
things.reduce(
(accumulated, currentThing, index, allThings) =>
index > allThings.length - windowSize
? accumulated
: [...accumulated, allThings.slice(index, index + windowSize)],
[],
);
13 changes: 13 additions & 0 deletions packages/fp/src/movingWindow/movingWindow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import movingWindow from './movingWindow';

describe('movingWindow', () => {
it('iterates an n-width window through an array', () => {
const actual = movingWindow(3)([1, 2, 3, 4, 5]);

expect(actual).toEqual([
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
]);
});
});

0 comments on commit 1f212c0

Please sign in to comment.