Skip to content
Myr

Added Collection#chunk()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by visionmedia · committed by visionmedia · parent 8d74d08 · Browse files · 2 files+28 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+28 added−1 removed

  • tests:+8 added−1 removed
  • other:+20 added−0 removed

2 files+28 added−1 removed

modified:lib/express/collection.js+20 added
297 unmodified lines
298 return max === null ? val :299 val > max ? val :300 max301 })302 },303 304 /**305 * Group collection into several collections grouped by _size_.306 *307 * @param {int} size308 * @return {Collection}309 * @api public310 */311 312 chunk: function(size) {313 var len = this.arr.length, 314 chunks = [], chunk = [], i = 0 315 this.each(function(val, key){316 chunk.push(val)317 if (i++ > 0 && (i % size == 0 || i == len))318 chunks.push(chunk),319 chunk = []320 })321 return $(chunks)322 }323})324325/**
11 unmodified lines
modified:spec/spec.collection.js+8 added-1 removed
215 unmodified lines
216 $([4,5,2,3,62]).min().should.eql 2217 end218 end219 220 describe 'max()'220 describe '#max()'221 it 'should return the max value'222 $([3,5,2,3,43,2]).max().should.eql 43223 end224 end225 226 describe '#chunk()'227 it 'should group into chunks of the given size'228 $([1,1,2,2,3,3]).chunk(2).toArray().should.eql [[1,1],[2,2],[3,3]]229 $([1,1,2,2,3,3]).chunk(4).toArray().should.eql [[1,1,2,2],[3,3]]230 end231 end232 233 end234end