Skip to content

Commit

Permalink
Update: Consolidate individual helper modules to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 27, 2016
1 parent 2dda613 commit ae16385
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 92 deletions.
11 changes: 0 additions & 11 deletions lib/getExtensions.js

This file was deleted.

63 changes: 63 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

var assert = require('assert');

var _ = require('lodash');

function getExtensions(lastArg){
if(typeof lastArg !== 'function'){
return lastArg;
}

return {};
}

function buildOnSettled(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;
}

function verifyArguments(args){
args = _.flatten(args);

assert.ok(args.length, 'A set of functions to combine is required');

_.forEach(args, function(arg, argIdx){
var isFunction = _.isFunction(arg);
if(isFunction){
return;
}

var msg = 'Only functions can be combined, got ' + typeof arg + ' for argument ' + argIdx;
assert.ok(isFunction, msg);
});

return args;
}

module.exports = {
getExtensions: getExtensions,
onSettled: buildOnSettled,
verifyArguments: verifyArguments
};
31 changes: 0 additions & 31 deletions lib/onSettled.js

This file was deleted.

7 changes: 3 additions & 4 deletions lib/parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ var _ = require('lodash');
var asyncDone = require('async-done');
var nowAndLater = require('now-and-later');

var getExtensions = require('./getExtensions');
var verifyArguments = require('./verifyArguments');
var helpers = require('./helpers');

function buildParallel(){
var args = verifyArguments(arguments);
var args = helpers.verifyArguments(arguments);

var extensions = getExtensions(_.last(args));
var extensions = helpers.getExtensions(_.last(args));

function parallel(done){
nowAndLater.parallel(args, asyncDone, extensions, done);
Expand Down
7 changes: 3 additions & 4 deletions lib/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ var _ = require('lodash');
var asyncDone = require('async-done');
var nowAndLater = require('now-and-later');

var getExtensions = require('./getExtensions');
var verifyArguments = require('./verifyArguments');
var helpers = require('./helpers');

function buildSeries(){
var args = verifyArguments(arguments);
var args = helpers.verifyArguments(arguments);

var extensions = getExtensions(_.last(args));
var extensions = helpers.getExtensions(_.last(args));

function series(done){
nowAndLater.series(args, asyncDone, extensions, done);
Expand Down
14 changes: 6 additions & 8 deletions lib/settleParallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ var _ = require('lodash');
var asyncSettle = require('async-settle');
var nowAndLater = require('now-and-later');

var onSettled = require('./onSettled');
var getExtensions = require('./getExtensions');
var verifyArguments = require('./verifyArguments');
var helpers = require('./helpers');

function buildParallel(){
var args = verifyArguments(arguments);
function buildSettleParallel(){
var args = helpers.verifyArguments(arguments);

var extensions = getExtensions(_.last(args));
var extensions = helpers.getExtensions(_.last(args));

function settleParallel(done){
nowAndLater.parallel(args, asyncSettle, extensions, onSettled(done));
nowAndLater.parallel(args, asyncSettle, extensions, helpers.onSettled(done));
}

return settleParallel;
}

module.exports = buildParallel;
module.exports = buildSettleParallel;
14 changes: 6 additions & 8 deletions lib/settleSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ var _ = require('lodash');
var asyncSettle = require('async-settle');
var nowAndLater = require('now-and-later');

var onSettled = require('./onSettled');
var getExtensions = require('./getExtensions');
var verifyArguments = require('./verifyArguments');
var helpers = require('./helpers');

function buildSeries(){
var args = verifyArguments(arguments);
function buildSettleSeries(){
var args = helpers.verifyArguments(arguments);

var extensions = getExtensions(_.last(args));
var extensions = helpers.getExtensions(_.last(args));

function settleSeries(done){
nowAndLater.series(args, asyncSettle, extensions, onSettled(done));
nowAndLater.series(args, asyncSettle, extensions, helpers.onSettled(done));
}

return settleSeries;
}

module.exports = buildSeries;
module.exports = buildSettleSeries;
25 changes: 0 additions & 25 deletions lib/verifyArguments.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/verifyArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

var verifyArguments = require('../lib/verifyArguments');
var verifyArguments = require('../lib/helpers').verifyArguments;

function validArg() {
}
Expand Down

0 comments on commit ae16385

Please sign in to comment.