cooperative
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -5,4 +5,4 @@ var map = require('./map') | ||
return map(array, predicate, options).then(function(predicates) { | ||
return array.filter((item, index) => predicates[index]) | ||
return array.filter(function (item, index) { return predicates[index] }) | ||
}) | ||
} |
@@ -1,2 +0,3 @@ | ||
module.exports = function map(array, map, {interval = 10} = {}) { | ||
module.exports = function map(array, map, options) { | ||
var interval = typeof options == 'object' && options.hasOwnProperty('interval')? options.interval: 10; | ||
var results = [] | ||
@@ -11,3 +12,3 @@ | ||
if (Date.now() - startTime > interval) { | ||
return new Promise((resolve) => { | ||
return new Promise(function (resolve) { | ||
setImmediate(resolve) | ||
@@ -14,0 +15,0 @@ }).then(function () { |
@@ -5,10 +5,10 @@ var map = require('./map') | ||
return map(Object.keys(object), function(key) { | ||
return Promise.resolve(mapper(object[key], key)).then((value) => { | ||
return Promise.resolve(mapper(object[key], key)).then(function (value) { | ||
return [key, value] | ||
}) | ||
}).then((entries) => { | ||
}).then(function (entries) { | ||
var result = {} | ||
entries.forEach(([key, value]) => { | ||
result[key] = value | ||
entries.forEach(function (entry) { | ||
result[entry[0]] = entry[1] | ||
}) | ||
@@ -15,0 +15,0 @@ |
{ | ||
"name": "cooperative", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "cooperative threading versions of map, filter, forEach, etc, suitable for big processing in single-threaded Node.js", | ||
@@ -16,2 +16,3 @@ "main": "inde.js", | ||
"chai": "3.5.0", | ||
"es6-promise": "4.1.0", | ||
"mocha": "3.2.0", | ||
@@ -18,0 +19,0 @@ "underscore": "1.8.3" |
@@ -1,2 +0,4 @@ | ||
module.exports = function reduce(array, operator, initial, {interval = 10} = {}) { | ||
module.exports = function reduce(array, operator, initial, options) { | ||
var interval = typeof options == 'object' && options.hasOwnProperty('interval')? options.interval: 10; | ||
function recurse(index, initial) { | ||
@@ -9,3 +11,3 @@ var startTime = Date.now() | ||
if (Date.now() - startTime > interval) { | ||
return new Promise((resolve) => { | ||
return new Promise(function (resolve) { | ||
setImmediate(resolve) | ||
@@ -12,0 +14,0 @@ }).then(function () { |
@@ -5,22 +5,27 @@ var filter = require('../filter') | ||
require('es6-promise').polyfill(); | ||
describe('filter', function () { | ||
it('filters an array', async function () { | ||
it('filters an array', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await filter(items, (n) => n % 2 == 0) | ||
expect(results).to.eql([2, 4]) | ||
return filter(items, function (n) { return n % 2 == 0 }).then(function (results) { | ||
expect(results).to.eql([2, 4]) | ||
}) | ||
}) | ||
it('filters an array with promise predicate', async function () { | ||
it('filters an array with promise predicate', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await filter(items, (n) => Promise.resolve(n % 2 == 0)) | ||
expect(results).to.eql([2, 4]) | ||
return filter(items, function (n) { return Promise.resolve(n % 2 == 0) }).then(function (results) { | ||
expect(results).to.eql([2, 4]) | ||
}) | ||
}) | ||
it('filters an empty array', async function () { | ||
it('filters an empty array', function () { | ||
var items = [] | ||
var results = await filter(items, (n) => n % 2 == 0) | ||
expect(results).to.eql([]) | ||
return filter(items, function (n) { return n % 2 == 0 }).then(function (results) { | ||
expect(results).to.eql([]) | ||
}) | ||
}) | ||
@@ -46,3 +51,3 @@ | ||
return n % 2 == 0 | ||
}).then(() => { | ||
}).then(function () { | ||
expect(times.length).to.be.greaterThan(1) | ||
@@ -49,0 +54,0 @@ expect(_.max(times)).to.be.lessThan(20) |
@@ -5,20 +5,24 @@ var forEach = require('../forEach') | ||
require('es6-promise').polyfill(); | ||
describe('forEach', function () { | ||
it('acts on each item in the array', async function () { | ||
it('acts on each item in the array', function () { | ||
var results = [] | ||
var items = [1, 2, 3, 4] | ||
await forEach(items, (n) => results.push(-n)) | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
return forEach(items, function (n) { results.push(-n) }).then(function () { | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
}) | ||
}) | ||
it('maps an array with promise mapper', async function () { | ||
it('maps an array with promise mapper', function () { | ||
var results = [] | ||
var items = [1, 2, 3, 4] | ||
await forEach(items, (n) => { | ||
return forEach(items, function (n) { | ||
results.push(-n) | ||
return Promise.resolve() | ||
}).then(function () { | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
}) | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
}) | ||
@@ -44,3 +48,3 @@ | ||
return -n | ||
}).then(() => { | ||
}).then(function () { | ||
expect(times.length).to.be.greaterThan(1) | ||
@@ -47,0 +51,0 @@ expect(_.max(times)).to.be.lessThan(20) |
@@ -5,22 +5,27 @@ var mapObject = require('../mapObject') | ||
require('es6-promise').polyfill(); | ||
describe('mapObject', function () { | ||
it('maps an array', async function () { | ||
it('maps an array', function () { | ||
var items = {a: 1, b: 2} | ||
var results = await mapObject(items, (n) => -n) | ||
expect(results).to.eql({a: -1, b: -2}) | ||
return mapObject(items, function (n) { return -n }).then(function (results) { | ||
expect(results).to.eql({a: -1, b: -2}) | ||
}) | ||
}) | ||
it('maps an array with promise mapper', async function () { | ||
it('maps an array with promise mapper', function () { | ||
var items = {a: 1, b: 2} | ||
var results = await mapObject(items, (n) => Promise.resolve(-n)) | ||
expect(results).to.eql({a: -1, b: -2}) | ||
return mapObject(items, function (n) { return Promise.resolve(-n) }).then(function (results) { | ||
expect(results).to.eql({a: -1, b: -2}) | ||
}) | ||
}) | ||
it('maps an empty array', async function () { | ||
it('maps an empty array', function () { | ||
var items = {} | ||
var results = await mapObject(items, (n) => -n) | ||
expect(results).to.eql({}) | ||
return mapObject(items, function (n) { return -n }).then(function (results) { | ||
expect(results).to.eql({}) | ||
}) | ||
}) | ||
@@ -39,3 +44,3 @@ | ||
var items = _.object(_.range(0, 50).map(i => ['key' + i, i])) | ||
var items = _.object(_.range(0, 50).map(function (i) { return ['key' + i, i] })) | ||
@@ -47,3 +52,3 @@ return mapObject(items, function (n) { | ||
return -n | ||
}).then(() => { | ||
}).then(function () { | ||
expect(times.length).to.be.greaterThan(1) | ||
@@ -50,0 +55,0 @@ expect(_.max(times)).to.be.lessThan(20) |
@@ -5,22 +5,27 @@ var map = require('../map') | ||
require('es6-promise').polyfill(); | ||
describe('map', function () { | ||
it('maps an array', async function () { | ||
it('maps an array', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await map(items, (n) => -n) | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
return map(items, function (n) { return -n }).then(function (results) { | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
}) | ||
}) | ||
it('maps an array with promise mapper', async function () { | ||
it('maps an array with promise mapper', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await map(items, (n) => Promise.resolve(-n)) | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
return map(items, function (n) { return Promise.resolve(-n) }).then(function (results) { | ||
expect(results).to.eql([-1, -2, -3, -4]) | ||
}) | ||
}) | ||
it('maps an empty array', async function () { | ||
it('maps an empty array', function () { | ||
var items = [] | ||
var results = await map(items, (n) => -n) | ||
expect(results).to.eql([]) | ||
return map(items, function (n) { return -n }).then(function (results) { | ||
expect(results).to.eql([]) | ||
}) | ||
}) | ||
@@ -46,3 +51,3 @@ | ||
return -n | ||
}).then(() => { | ||
}).then(function () { | ||
expect(times.length).to.be.greaterThan(1) | ||
@@ -49,0 +54,0 @@ expect(_.max(times)).to.be.lessThan(20) |
@@ -5,22 +5,27 @@ var reduce = require('../reduce') | ||
require('es6-promise').polyfill(); | ||
describe('reduce', function () { | ||
it('filters an array', async function () { | ||
it('filters an array', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await reduce(items, (sum, n) => sum + n, 0) | ||
expect(results).to.eql(10) | ||
return reduce(items, function (sum, n) { return sum + n }, 0).then(function (results) { | ||
expect(results).to.eql(10) | ||
}) | ||
}) | ||
it('filters an array with promise predicate', async function () { | ||
it('filters an array with promise predicate', function () { | ||
var items = [1, 2, 3, 4] | ||
var results = await reduce(items, (sum, n) => Promise.resolve(sum + n), 0) | ||
expect(results).to.eql(10) | ||
return reduce(items, function (sum, n) { return Promise.resolve(sum + n) }, 0).then(function (results) { | ||
expect(results).to.eql(10) | ||
}) | ||
}) | ||
it('filters an empty array', async function () { | ||
it('filters an empty array', function () { | ||
var items = [] | ||
var results = await reduce(items, (sum, n) => sum + n, 0) | ||
expect(results).to.eql(0) | ||
return reduce(items, function (sum, n) { return sum + n }, 0).then(function (results) { | ||
expect(results).to.eql(0) | ||
}) | ||
}) | ||
@@ -46,3 +51,3 @@ | ||
return sum + n | ||
}, 0).then(() => { | ||
}, 0).then(function () { | ||
expect(times.length).to.be.greaterThan(1) | ||
@@ -49,0 +54,0 @@ expect(_.max(times)).to.be.lessThan(20) |
Sorry, the diff of this file is not supported yet
14072
298
4