Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_.groupsOf #696

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
<li>- <a href="#initial">initial</a></li>
<li>- <a href="#last">last</a></li>
<li>- <a href="#rest">rest</a></li>
<li>- <a href="#chunk">chunk</a></li>
<li>- <a href="#compact">compact</a></li>
<li>- <a href="#flatten">flatten</a></li>
<li>- <a href="#without">without</a></li>
Expand Down Expand Up @@ -691,6 +692,21 @@ <h2 id="arrays">Array Functions</h2>
<pre>
_.rest([5, 4, 3, 2, 1]);
=&gt; [4, 3, 2, 1]
</pre>

<p id="chunk">
<b class="header">chunk</b><code>_.chunk(array, n)</code>
<br />
Returns an array of arrays in <b>chunks</b> of size <b>n</b> with the last
group contining the remaining elements if <b>n</b> does not evenly divide the
length of <b>array</b>.
</p>
<pre>
_.chunk([1, 2, 3, 4, 5, 6], 3);
=&gt; [[1, 2, 3], [4, 5, 6]]

_.chunk([1, 2, 3, 4, 5, 6], 4);
=&gt; [[1, 2, 3, 4], [5, 6]]
</pre>

<p id="compact">
Expand Down
15 changes: 15 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ $(document).ready(function() {
equal(_.flatten(result).join(','), '2,3,2,3', 'works well with _.map');
});

test("arrays: chunk", function() {
var list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

var chunks = _.chunk(list, 5);
equal(chunks[0].join(',') + chunks[1].join(','),
'1,2,3,4,56,7,8,9,0',
'can chunk by a value that evenly divides the input array\'s length');

chunks = _.chunk(list, 3)
equal(chunks[0].join(',') + chunks[1].join(',') + chunks[2].join(',') + chunks[3].join(','),
'1,2,34,5,67,8,90',
'can chunk by a value that does not evenly divide the input array\'s length');
});


test("arrays: initial", function() {
equal(_.initial([1,2,3,4,5]).join(", "), "1, 2, 3, 4", 'working initial()');
equal(_.initial([1,2,3,4],2).join(", "), "1, 2", 'initial can take an index');
Expand Down
10 changes: 10 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@
return slice.call(array, (index == null) || guard ? 1 : index);
};

// Return an array of arrays of length at least n
_.chunk = function(array, n) {
var chunks = [];
var numChunks = Math.ceil(array.length / n);
for(var i = 0; i < numChunks; i++) {
chunks.push(array.slice(i * n, i * n + n));
}
return chunks;
}

// Trim out all falsy values from an array.
_.compact = function(array) {
return _.filter(array, function(value){ return !!value; });
Expand Down