From d1a920cbb18808eea5609412189003666ddbd2f2 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Sun, 10 Aug 2014 18:13:11 +0200 Subject: [PATCH] verify arguments to serries / parallel functions Fixes #4 --- lib/createParallel.js | 3 ++- lib/createSeries.js | 3 ++- lib/verifyArguments.js | 13 +++++++++++++ test/verifyArguments.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 lib/verifyArguments.js create mode 100644 test/verifyArguments.js diff --git a/lib/createParallel.js b/lib/createParallel.js index 5be10a3..1efa7ce 100644 --- a/lib/createParallel.js +++ b/lib/createParallel.js @@ -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); diff --git a/lib/createSeries.js b/lib/createSeries.js index bd2f275..345c17d 100644 --- a/lib/createSeries.js +++ b/lib/createSeries.js @@ -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); diff --git a/lib/verifyArguments.js b/lib/verifyArguments.js new file mode 100644 index 0000000..9383569 --- /dev/null +++ b/lib/verifyArguments.js @@ -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; +}; \ No newline at end of file diff --git a/test/verifyArguments.js b/test/verifyArguments.js new file mode 100644 index 0000000..e59ed0a --- /dev/null +++ b/test/verifyArguments.js @@ -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(); +}); \ No newline at end of file