Last active
December 11, 2015 08:58
-
-
Save cromwellryan/4576815 to your computer and use it in GitHub Desktop.
batch mixin for underscore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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