Skip to content

Instantly share code, notes, and snippets.

@cromwellryan
Last active December 11, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cromwellryan/4576815 to your computer and use it in GitHub Desktop.
Save cromwellryan/4576815 to your computer and use it in GitHub Desktop.
batch mixin for underscore
node_modules
batch = (collection, size) ->
return unless collection.slice?
numberofbatches = Math.ceil (collection.length / size)
for batchindex in [0...numberofbatches]
do (batchindex) ->
start = batchindex * size
end = start + size
collection[start...end]
exports = module.exports = (_) ->
_.mixin {
batch: batch }
_ = require 'underscore'
require('../batch_mixin')(_)
describe 'batching', ->
it 'wont work on noncollection', ->
batches = _.batch 2, 10
expect(batches).toBeUndefined
it 'empty', ->
batches = _.batch [], 10
expect(batches).toEqual []
it 'same size', ->
batches = _.batch [1..5], 5
expect(batches.length).toBe 1
it 'real thing', ->
batches = _.chain([1..6]).batch(5).value()
expect(batches.length).toBe 2
expect(batches[0]).toEqual [1..5]
expect(batches[1]).toEqual [6]
batches = _.batch [1..3], 5
expect(batches.length).toBe 1
expect(batches[0]).toEqual [1..3]
batches = _.batch [1..10], 3
expect(batches.length).toBe 4
batches = _.batch [1..10], 100
expect(batches.length).toBe 1
{
"name": "batch",
"version": "0.0.0",
"description": "batch mixin for underscore",
"main": "index.js",
"scripts": {
"test": "jasmine-node --autotest --color --coffee ."
},
"repository": {
"type": "git",
"url": "https://gist.github.com/4576815.git"
},
"author": "Ryan Cromwell Sr",
"license": "BSD",
"dependencies": {
"jasmine-node": "~1.0.28",
"underscore": "~1.4.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment