Skip to content

Commit

Permalink
Fix #69. Allow compose to pass full argument list through to first fu…
Browse files Browse the repository at this point in the history
…nction
  • Loading branch information
briancavalier committed Jan 17, 2013
1 parent a4d5b1c commit 7e99869
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
16 changes: 9 additions & 7 deletions lib/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
define(['when'], function (when) {
"use strict";

var slice = [].slice;
var slice, undef;

slice = [].slice;

/**
* Create a partial function
Expand Down Expand Up @@ -84,17 +86,17 @@ define(['when'], function (when) {
* @param funcs {Array} array of functions to compose
* @return {Function} composed function
*/
function compose(funcs /*, ...*/) {
function compose(funcs) {

if(arguments.length > 1) {
funcs = slice.call(arguments);
}
var first;
first = funcs[0];
funcs = funcs.slice(1);

return function composed(x) {
return function composed() {
var context = this;
return funcs.reduce(function(result, f) {
return f.call(context, result);
}, x);
}, first.apply(this, arguments));
};
}

Expand Down
20 changes: 19 additions & 1 deletion test/node/base/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ refute = buster.refute;
fail = buster.assertions.fail;

function plusOne(x) {
return x+1;
return plus(x, 1);
}

function plus(x, y) {
return x + y;
}

function Thing(x) {
Expand Down Expand Up @@ -55,6 +59,20 @@ buster.testCase('base:functional', {
).then(done, done);
},

'should allow multiple args when composing a string specification': function(done) {
wire({
f1: plus,
f2: plusOne,
composed: {
compose: 'f1 | f2'
}
}).then(
function(c) {
assert.equals(c.composed(1, 2), 4);
}
).then(done, done);
},

'should compose a string specification with single function': function(done) {
wire({
f1: plusOne,
Expand Down
4 changes: 2 additions & 2 deletions test/node/base/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ buster.testCase('base:module', {
'should use module exports value as component': function(done) {
wire({
test: {
module: 'test/node/fixtures/module'
module: '../fixtures/module'
}
}).then(
}, { require: require }).then(
function(context) {
assert(context.test.success);
},
Expand Down

0 comments on commit 7e99869

Please sign in to comment.