Skip to content

Commit 0fdef52

Browse files
committed
fix: Allow an array of functions as argument
1 parent bf50fa0 commit 0fdef52

File tree

6 files changed

+133
-1
lines changed

6 files changed

+133
-1
lines changed

lib/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function buildOnSettled(done) {
5757
}
5858

5959
function verifyArguments(args) {
60-
args = Array.prototype.slice.apply(args);
60+
args = Array.prototype.concat.apply([], args);
6161

6262
if (!args.length) {
6363
throw new Error('A set of functions to combine is required');

test/parallel.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe('parallel', function () {
3535
});
3636
});
3737

38+
it('allows an array of functions', function (done) {
39+
bach.parallel([fn1, fn2, fn3])(function (error, results) {
40+
expect(error).toEqual(null);
41+
expect(results).toEqual([1, 2, 3]);
42+
done();
43+
});
44+
});
45+
3846
it('should execute functions in parallel, passing error', function (done) {
3947
function slowFn(done) {
4048
setTimeout(function () {
@@ -75,4 +83,26 @@ describe('parallel', function () {
7583
});
7684
done();
7785
});
86+
87+
it('allows array of functions & extensions object', function (done) {
88+
var arr = [];
89+
var fns = [fn1, fn2, fn3];
90+
bach.parallel([fn1, fn2, fn3], {
91+
create: function (fn, idx) {
92+
expect(fns).toContain(fn);
93+
arr[idx] = fn;
94+
return arr;
95+
},
96+
before: function (storage) {
97+
expect(storage).toEqual(arr);
98+
},
99+
after: function (result, storage) {
100+
expect(storage).toEqual(arr);
101+
},
102+
})(function (error) {
103+
expect(error).toEqual(null);
104+
expect(arr).toEqual(fns);
105+
});
106+
done();
107+
});
78108
});

test/series.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe('series', function () {
3535
});
3636
});
3737

38+
it('allows an array of functions', function (done) {
39+
bach.series([fn1, fn2, fn3])(function (error, results) {
40+
expect(error).toEqual(null);
41+
expect(results).toEqual([1, 2, 3]);
42+
done();
43+
});
44+
});
45+
3846
it('should execute functions in series, passing error', function (done) {
3947
function slowFn(done) {
4048
setTimeout(function () {
@@ -74,4 +82,26 @@ describe('series', function () {
7482
});
7583
done();
7684
});
85+
86+
it('allows array of functions & extensions object', function (done) {
87+
var arr = [];
88+
var fns = [fn1, fn2, fn3];
89+
bach.series([fn1, fn2, fn3], {
90+
create: function (fn, idx) {
91+
expect(fns).toContain(fn);
92+
arr[idx] = fn;
93+
return arr;
94+
},
95+
before: function (storage) {
96+
expect(storage).toEqual(arr);
97+
},
98+
after: function (result, storage) {
99+
expect(storage).toEqual(arr);
100+
},
101+
})(function (error) {
102+
expect(error).toEqual(null);
103+
expect(arr).toEqual(fns);
104+
});
105+
done();
106+
});
77107
});

test/settleParallel.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe('settleParallel', function () {
3535
});
3636
});
3737

38+
it('allows an array of functions', function (done) {
39+
bach.settleParallel([fn1, fn2, fn3])(function (errors, results) {
40+
expect(errors).toEqual(null);
41+
expect(results).toEqual([1, 2, 3]);
42+
done();
43+
});
44+
});
45+
3846
it('should execute functions in parallel, passing settled errors and results', function (done) {
3947
function slowFn(done) {
4048
setTimeout(function () {
@@ -75,4 +83,26 @@ describe('settleParallel', function () {
7583
});
7684
done();
7785
});
86+
87+
it('allows array of functions & extensions object', function (done) {
88+
var arr = [];
89+
var fns = [fn1, fn2, fn3];
90+
bach.settleParallel([fn1, fn2, fn3], {
91+
create: function (fn, idx) {
92+
expect(fns).toContain(fn);
93+
arr[idx] = fn;
94+
return arr;
95+
},
96+
before: function (storage) {
97+
expect(storage).toEqual(arr);
98+
},
99+
after: function (result, storage) {
100+
expect(storage).toEqual(arr);
101+
},
102+
})(function (error) {
103+
expect(error).toEqual(null);
104+
expect(arr).toEqual(fns);
105+
});
106+
done();
107+
});
78108
});

test/settleSeries.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ describe('settleSeries', function () {
3535
});
3636
});
3737

38+
it('allows an array of functions', function (done) {
39+
bach.settleSeries([fn1, fn2, fn3])(function (errors, results) {
40+
expect(errors).toEqual(null);
41+
expect(results).toEqual([1, 2, 3]);
42+
done();
43+
});
44+
});
45+
3846
it('should execute functions in series, passing settled errors and results', function (done) {
3947
function slowFn(done) {
4048
setTimeout(function () {
@@ -75,4 +83,26 @@ describe('settleSeries', function () {
7583
});
7684
done();
7785
});
86+
87+
it('allows array of functions & extensions object', function (done) {
88+
var arr = [];
89+
var fns = [fn1, fn2, fn3];
90+
bach.settleSeries([fn1, fn2, fn3], {
91+
create: function (fn, idx) {
92+
expect(fns).toContain(fn);
93+
arr[idx] = fn;
94+
return arr;
95+
},
96+
before: function (storage) {
97+
expect(storage).toEqual(arr);
98+
},
99+
after: function (result, storage) {
100+
expect(storage).toEqual(arr);
101+
},
102+
})(function (error) {
103+
expect(error).toEqual(null);
104+
expect(arr).toEqual(fns);
105+
});
106+
done();
107+
});
78108
});

test/verifyArguments.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ describe('verifyArguments', function () {
1313
done();
1414
});
1515

16+
it('flattens arrays one level deep', function (done) {
17+
var args = [validArg, validArg];
18+
expect(verifyArguments([args])).toEqual(args);
19+
done();
20+
});
21+
22+
it('only flattens one level of multi-level array', function (done) {
23+
var args = [validArg, [validArg]];
24+
expect(verifyArguments([args])).toEqual(args);
25+
done();
26+
});
27+
1628
it('should throw descriptive error message on invalid argument', function (done) {
1729
function invalid() {
1830
verifyArguments([validArg, 'invalid', validArg]);

0 commit comments

Comments
 (0)