Skip to content

Commit

Permalink
fix: Allow an array of functions as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Aug 29, 2022
1 parent bf50fa0 commit 0fdef52
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers.js
Expand Up @@ -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');
Expand Down
30 changes: 30 additions & 0 deletions test/parallel.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
});
30 changes: 30 additions & 0 deletions test/series.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
});
30 changes: 30 additions & 0 deletions test/settleParallel.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
});
30 changes: 30 additions & 0 deletions test/settleSeries.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
});
12 changes: 12 additions & 0 deletions test/verifyArguments.js
Expand Up @@ -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]);
Expand Down

0 comments on commit 0fdef52

Please sign in to comment.