Use .apply for initial function call in _.compose then use .call in loop. [closes #463, #464]

This commit is contained in:
Stephen Solka
2014-01-28 23:33:13 -05:00
committed by John-David Dalton
parent 012a929067
commit 512c8de526
2 changed files with 14 additions and 5 deletions

View File

@@ -4672,14 +4672,17 @@
throw new TypeError; throw new TypeError;
} }
} }
var funcsLength = funcs.length;
if (funcsLength === 1) {
return funcs[0];
}
return function() { return function() {
var args = arguments, var length = funcsLength - 1,
length = funcs.length; result = funcs[length].apply(this, arguments);
while (length--) { while (length--) {
args = [funcs[length].apply(this, args)]; result = funcs[length].call(this, result);
} }
return args[0]; return result;
}; };
} }

View File

@@ -1259,6 +1259,12 @@
var welcome = _.compose(greet, format); var welcome = _.compose(greet, format);
equal(welcome('pebbles'), 'Hiya Penelope!'); equal(welcome('pebbles'), 'Hiya Penelope!');
}); });
test('should return original function if only one function passed', 1, function(){
var noop = function(){};
equal(_.compose(noop), noop);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/