Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify arguments to serries / parallel functions #5

Merged
merged 1 commit into from
Aug 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});