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 4900f96
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 5 additions & 5 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,12 @@
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]), [], 'defaults to empty array (chunk size 0)');
deepEqual(_.chunk([1, 2, 3], {}), [], 'nonsense defaults to empty array (chunk size 0)');

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], 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
4 changes: 1 addition & 3 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,15 +699,13 @@
// 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);
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 4900f96

Please sign in to comment.