Skip to content

Commit

Permalink
Fix bug in chunked()
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Dec 9, 2019
1 parent 96e13b2 commit 2ef997c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.5.3
------
* Fix bug in `chunked()` when input is exactly dividable

v1.5.2
------
* Export `count()` function at the top level 🤦‍♂️
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/more-itertools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe('chunked', () => {
[4, 5],
]);
});

it('works with exactly chunkable list', () => {
expect(Array.from(chunked([1, 2, 3, 4, 5, 6], 3))).toEqual([
[1, 2, 3],
[4, 5, 6],
]);
});
});

describe('first', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/more-itertools.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function* chunked<T>(iterable: Iterable<T>, size: number): Iterable<Array
}
}

yield chunk;
// Yield the remainder, if there is any
if (chunk.length > 0) {
yield chunk;
}
}

/**
Expand Down

0 comments on commit 2ef997c

Please sign in to comment.