Skip to content

Commit

Permalink
New: First implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 27, 2016
1 parent 6f8c885 commit d318821
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"node": true,
"strict": true,
"eqnull": true
}
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var series = require('./series');
var parallel = require('./parallel');
var settleSeries = require('./settleSeries');
var settleParallel = require('./settleParallel');

module.exports = {
series: series,
parallel: parallel,
settleSeries: settleSeries,
settleParallel: settleParallel,
};
31 changes: 31 additions & 0 deletions lib/onSettled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var _ = require('lodash');

function buildOnComplete(done){
function onSettled(error, result){
var settledErrors = _.where(result, { state: 'error' });
var settledResults = _.where(result, { state: 'success' });

var errors;
if(settledErrors.length){
errors = _.pluck(settledErrors, 'value');
}

var results;
if(settledResults.length){
results = _.pluck(settledResults, 'value');
}

error = error || errors;
if(error){
return done(error, results);
}

done(undefined, results);
}

return onSettled;
}

module.exports = buildOnComplete;
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "bach",
"version": "0.0.0",
"description": "Compose async functions",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tap ./test"
},
"repository": {
"type": "git",
"url": "git://github.com/phated/bach"
},
"keywords": [
"async",
"compose",
"fluent",
"composing"
],
"author": "Blaine Bublitz <blaine@iceddev.com> (http://iceddev.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/phated/bach/issues"
},
"homepage": "https://github.com/phated/bach",
"devDependencies": {
"tap": "~0.4.8"
},
"dependencies": {
"async-settle": "~0.1.0",
"async-done": "~0.2.0",
"async": "~0.2.10",
"lodash": "~2.4.1"
}
}
17 changes: 17 additions & 0 deletions parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var _ = require('lodash');
var async = require('async');
var asyncDone = require('async-done');

function buildParallel(){
var args = _.flatten(arguments);

function parallel(done){
async.map(args, asyncDone, done);
}

return parallel;
}

module.exports = buildParallel;
17 changes: 17 additions & 0 deletions series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var _ = require('lodash');
var async = require('async');
var asyncDone = require('async-done');

function buildSeries(){
var args = _.flatten(arguments);

function series(done){
async.mapSeries(args, asyncDone, done);
}

return series;
}

module.exports = buildSeries;
19 changes: 19 additions & 0 deletions settleParallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var _ = require('lodash');
var async = require('async');
var asyncSettle = require('async-settle');

var onSettled = require('./lib/onSettled');

function buildParallel(){
var args = _.flatten(arguments);

function settleParallel(done){
async.map(args, asyncSettle, onSettled(done));
}

return settleParallel;
}

module.exports = buildParallel;
19 changes: 19 additions & 0 deletions settleSeries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

var _ = require('lodash');
var async = require('async');
var asyncSettle = require('async-settle');

var onSettled = require('./lib/onSettled');

function buildParallel(){
var args = _.flatten(arguments);

function settleSeries(done){
async.mapSeries(args, asyncSettle, onSettled(done));
}

return settleSeries;
}

module.exports = buildParallel;
48 changes: 48 additions & 0 deletions test/parallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var test = require('tap').test;

var bach = require('../');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in parallel and call callback with results on completion', function(t){
bach.parallel(fn1, fn2, fn3)(function(error, results){
t.notOk(error, 'error should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
});
});

test('should execute functions in parallel and call callback with error on an error', function(t){
function slowFn(done){
setTimeout(function(){
t.fail('slow function should not be called');
done(null, 2);
}, 500);
}
bach.parallel(fn1, slowFn, fn3, fnError)(function(error, results){
t.ok(error, 'error should be defined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, , 3, undefined], 'results should be [1, , 3, undefined]');
t.ok(error instanceof Error, 'error should be instance of an Error');
t.end();
});
});
48 changes: 48 additions & 0 deletions test/series.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var test = require('tap').test;

var bach = require('../');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in series and call callback with results on completion', function(t){
bach.series(fn1, fn2, fn3)(function(error, results){
t.notOk(error, 'error should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
});
});

test('should execute functions in series and call callback with error on an error', function(t){
function slowFn(done){
setTimeout(function(){
t.ok(true, 'slow function should be called');
done(null, 2);
}, 500);
}
bach.series(fn1, slowFn, fn3, fnError)(function(error, results){
t.ok(error, 'error should be defined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3, undefined], 'results should be [1, 2, 3, undefined]');
t.ok(error instanceof Error, 'error should be instance of an Error');
t.end();
});
});
50 changes: 50 additions & 0 deletions test/settleParallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var test = require('tap').test;

var bach = require('../');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in parallel and call callback with results on settled', function(t){
bach.settleParallel(fn1, fn2, fn3)(function(errors, results){
t.notOk(errors, 'errors should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
});
});

test('should execute functions in parallel and call callback with errors on settled', function(t){
function slowFn(done){
setTimeout(function(){
t.ok(true, 'slow function should be called');
done(null, 2);
}, 500);
}
bach.settleParallel(fn1, slowFn, fn3, fnError)(function(errors, results){
t.ok(errors, 'errors should be defined');
t.ok(results, 'results should be defined');
t.ok(Array.isArray(errors), 'errors should be an array');
t.ok(errors[0] instanceof Error, 'errors should be an array of Error instances');
t.ok(Array.isArray(results), 'results should be an array');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]')
t.end();
});
});
50 changes: 50 additions & 0 deletions test/settleSeries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

var test = require('tap').test;

var bach = require('../');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function fnError(done){
done(new Error('An Error Occurred'));
}

test('should execute functions in series and call callback with results on settled', function(t){
bach.settleSeries(fn1, fn2, fn3)(function(errors, results){
t.notOk(errors, 'errors should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
});
});

test('should execute functions in series and call callback with errors on settled', function(t){
function slowFn(done){
setTimeout(function(){
t.ok(true, 'slow function should be called');
done(null, 2);
}, 500);
}
bach.settleSeries(fn1, slowFn, fn3, fnError)(function(errors, results){
t.ok(errors, 'errors should be defined');
t.ok(results, 'results should be defined');
t.ok(Array.isArray(errors), 'errors should be an array');
t.ok(Array.isArray(results), 'results should be an array');
t.ok(errors[0] instanceof Error, 'errors should be an array of Error instances');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
t.end();
});
});

0 comments on commit d318821

Please sign in to comment.