Skip to content

Commit

Permalink
Fix: Verify arguments to series/parallel functions (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource authored and phated committed Jun 27, 2016
1 parent b5c3aa0 commit ca32513
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/createParallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

var _ = require('lodash');
var async = require('async');
var verifyArguments = require('./verifyArguments');

function createParallel(mapFn){

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

function parallel(done){
async.map(args, mapFn, done);
Expand Down
3 changes: 2 additions & 1 deletion lib/createSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

var _ = require('lodash');
var async = require('async');
var verifyArguments = require('./verifyArguments');

function createSeries(mapFn){

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

function series(done){
async.mapSeries(args, mapFn, done);
Expand Down
13 changes: 13 additions & 0 deletions lib/verifyArguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var assert = require('assert');

module.exports = function(args){

assert(args.length > 0, 'A set of functions to combine is mandatory');

args.forEach(function(arg, argIdx){
assert.equal(typeof arg, 'function',
'Only functions can be combined, got ' + typeof arg + ' for argument ' + argIdx);
});

return args;
};
32 changes: 32 additions & 0 deletions test/verifyArguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

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

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

function validArg() {
}

test('should act as pass-through for a valid set of arguments', function(t){
var args = [validArg, validArg];
t.deepEqual(verifyArguments(args), args);
t.end();
});

test('should throw descriptive error message on invalid argument', function(t){
t.throws(function(){
verifyArguments([validArg, 'invalid', validArg]);
}, {
name: 'AssertionError', message: 'Only functions can be combined, got string for argument 1'
}, 'should throw AssertionError');
t.end();
});

test('should throw descriptive error message on when no arguments provided', function(t){
t.throws(function(){
verifyArguments([]);
}, {
name: 'AssertionError', message: 'A set of functions to combine is mandatory'
}, 'should throw AssertionError');
t.end();
});

0 comments on commit ca32513

Please sign in to comment.