From 0fdef521beb77592fe6cc9264bb6a2e76585b127 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 29 Aug 2022 13:07:43 -0700 Subject: [PATCH] fix: Allow an array of functions as argument --- lib/helpers.js | 2 +- test/parallel.js | 30 ++++++++++++++++++++++++++++++ test/series.js | 30 ++++++++++++++++++++++++++++++ test/settleParallel.js | 30 ++++++++++++++++++++++++++++++ test/settleSeries.js | 30 ++++++++++++++++++++++++++++++ test/verifyArguments.js | 12 ++++++++++++ 6 files changed, 133 insertions(+), 1 deletion(-) diff --git a/lib/helpers.js b/lib/helpers.js index 3ab1da2..d2a14d7 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -57,7 +57,7 @@ function buildOnSettled(done) { } function verifyArguments(args) { - args = Array.prototype.slice.apply(args); + args = Array.prototype.concat.apply([], args); if (!args.length) { throw new Error('A set of functions to combine is required'); diff --git a/test/parallel.js b/test/parallel.js index 70b2ba1..03d0340 100644 --- a/test/parallel.js +++ b/test/parallel.js @@ -35,6 +35,14 @@ describe('parallel', function () { }); }); + it('allows an array of functions', function (done) { + bach.parallel([fn1, fn2, fn3])(function (error, results) { + expect(error).toEqual(null); + expect(results).toEqual([1, 2, 3]); + done(); + }); + }); + it('should execute functions in parallel, passing error', function (done) { function slowFn(done) { setTimeout(function () { @@ -75,4 +83,26 @@ describe('parallel', function () { }); done(); }); + + it('allows array of functions & extensions object', function (done) { + var arr = []; + var fns = [fn1, fn2, fn3]; + bach.parallel([fn1, fn2, fn3], { + create: function (fn, idx) { + expect(fns).toContain(fn); + arr[idx] = fn; + return arr; + }, + before: function (storage) { + expect(storage).toEqual(arr); + }, + after: function (result, storage) { + expect(storage).toEqual(arr); + }, + })(function (error) { + expect(error).toEqual(null); + expect(arr).toEqual(fns); + }); + done(); + }); }); diff --git a/test/series.js b/test/series.js index d76b4af..89817dc 100644 --- a/test/series.js +++ b/test/series.js @@ -35,6 +35,14 @@ describe('series', function () { }); }); + it('allows an array of functions', function (done) { + bach.series([fn1, fn2, fn3])(function (error, results) { + expect(error).toEqual(null); + expect(results).toEqual([1, 2, 3]); + done(); + }); + }); + it('should execute functions in series, passing error', function (done) { function slowFn(done) { setTimeout(function () { @@ -74,4 +82,26 @@ describe('series', function () { }); done(); }); + + it('allows array of functions & extensions object', function (done) { + var arr = []; + var fns = [fn1, fn2, fn3]; + bach.series([fn1, fn2, fn3], { + create: function (fn, idx) { + expect(fns).toContain(fn); + arr[idx] = fn; + return arr; + }, + before: function (storage) { + expect(storage).toEqual(arr); + }, + after: function (result, storage) { + expect(storage).toEqual(arr); + }, + })(function (error) { + expect(error).toEqual(null); + expect(arr).toEqual(fns); + }); + done(); + }); }); diff --git a/test/settleParallel.js b/test/settleParallel.js index eb7efec..906746a 100644 --- a/test/settleParallel.js +++ b/test/settleParallel.js @@ -35,6 +35,14 @@ describe('settleParallel', function () { }); }); + it('allows an array of functions', function (done) { + bach.settleParallel([fn1, fn2, fn3])(function (errors, results) { + expect(errors).toEqual(null); + expect(results).toEqual([1, 2, 3]); + done(); + }); + }); + it('should execute functions in parallel, passing settled errors and results', function (done) { function slowFn(done) { setTimeout(function () { @@ -75,4 +83,26 @@ describe('settleParallel', function () { }); done(); }); + + it('allows array of functions & extensions object', function (done) { + var arr = []; + var fns = [fn1, fn2, fn3]; + bach.settleParallel([fn1, fn2, fn3], { + create: function (fn, idx) { + expect(fns).toContain(fn); + arr[idx] = fn; + return arr; + }, + before: function (storage) { + expect(storage).toEqual(arr); + }, + after: function (result, storage) { + expect(storage).toEqual(arr); + }, + })(function (error) { + expect(error).toEqual(null); + expect(arr).toEqual(fns); + }); + done(); + }); }); diff --git a/test/settleSeries.js b/test/settleSeries.js index 87be6fe..7229c68 100644 --- a/test/settleSeries.js +++ b/test/settleSeries.js @@ -35,6 +35,14 @@ describe('settleSeries', function () { }); }); + it('allows an array of functions', function (done) { + bach.settleSeries([fn1, fn2, fn3])(function (errors, results) { + expect(errors).toEqual(null); + expect(results).toEqual([1, 2, 3]); + done(); + }); + }); + it('should execute functions in series, passing settled errors and results', function (done) { function slowFn(done) { setTimeout(function () { @@ -75,4 +83,26 @@ describe('settleSeries', function () { }); done(); }); + + it('allows array of functions & extensions object', function (done) { + var arr = []; + var fns = [fn1, fn2, fn3]; + bach.settleSeries([fn1, fn2, fn3], { + create: function (fn, idx) { + expect(fns).toContain(fn); + arr[idx] = fn; + return arr; + }, + before: function (storage) { + expect(storage).toEqual(arr); + }, + after: function (result, storage) { + expect(storage).toEqual(arr); + }, + })(function (error) { + expect(error).toEqual(null); + expect(arr).toEqual(fns); + }); + done(); + }); }); diff --git a/test/verifyArguments.js b/test/verifyArguments.js index 546f065..1428c39 100644 --- a/test/verifyArguments.js +++ b/test/verifyArguments.js @@ -13,6 +13,18 @@ describe('verifyArguments', function () { done(); }); + it('flattens arrays one level deep', function (done) { + var args = [validArg, validArg]; + expect(verifyArguments([args])).toEqual(args); + done(); + }); + + it('only flattens one level of multi-level array', function (done) { + var args = [validArg, [validArg]]; + expect(verifyArguments([args])).toEqual(args); + done(); + }); + it('should throw descriptive error message on invalid argument', function (done) { function invalid() { verifyArguments([validArg, 'invalid', validArg]);