Skip to content

Commit

Permalink
Enforce 1 being the minimal chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Jul 28, 2015
1 parent b221fe7 commit 3a955a2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
9 changes: 4 additions & 5 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,11 @@
test('chunk', function() {
deepEqual(_.chunk([], 2), [], 'chunk for empty array returns an empty array');

deepEqual(_.chunk([1, 2, 3], 0), [1, 2, 3], 'chunk into parts of 0 elements returns original array');
deepEqual(_.chunk([1, 2, 3], 0), [], 'chunk into parts of 0 elements returns empty array');
deepEqual(_.chunk([1, 2, 3], -1), [], 'chunk into parts of negative amount of elements returns an empty array');

deepEqual(_.chunk([1, 2, 3], 1), [1, 2, 3], 'chunk into parts of 1 elements returns original array');
deepEqual(_.chunk([1, 2, 3]), [1, 2, 3], 'chunk into parts of 1 elements is default value and returns original array');

deepEqual(_.chunk([1, 2, 3], -1), [1, 2, 3], 'chunk into parts of negative amount of elements returns an empty array');
deepEqual(_.chunk([1, 2, 3], 1), [[1], [2], [3]], 'chunk into parts of 1 elements returns original array');
deepEqual(_.chunk([1, 2, 3]), [[1], [2], [3]], 'chunk into parts of 1 elements is default value and returns original array');

deepEqual(_.chunk([1, 2, 3], 3), [[1, 2, 3]], 'chunk into parts of current array length elements returns the original array');
deepEqual(_.chunk([1, 2, 3], 5), [[1, 2, 3]], 'chunk into parts of more then current array length elements returns the original array');
Expand Down
5 changes: 2 additions & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,14 @@
// Split an **array** into several arrays containing **count** or less elements
// of initial array
_.chunk = function(array, count) {
count = +count || 1;
if (count <= 1) return slice.call(array);
count = count != null ? count : 1;
if (count < 1) return [];

var result = [];
var i = 0, length = array.length;
while (i < length) {
result.push(slice.call(array, i, i += count));
}

return result;
};

Expand Down

0 comments on commit 3a955a2

Please sign in to comment.