Skip to content

Commit

Permalink
Use .apply for initial function call in _.compose then use `.call…
Browse files Browse the repository at this point in the history
…` in loop. [closes lodash#463, lodash#464]
  • Loading branch information
Stephen Solka authored and jdalton committed Jan 29, 2014
1 parent 1ec9130 commit ae645cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lodash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4672,14 +4672,17 @@
throw new TypeError;
}
}
var funcsLength = funcs.length;
if (funcsLength === 1) {
return funcs[0];
}
return function() {
var args = arguments,
length = funcs.length;

var length = funcsLength - 1,
result = funcs[length].apply(this, arguments);
while (length--) {
args = [funcs[length].apply(this, args)];
result = funcs[length].call(this, result);
}
return args[0];
return result;
};
}

Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,12 @@
var welcome = _.compose(greet, format);
equal(welcome('pebbles'), 'Hiya Penelope!');
});

test('should return original function if only one function passed', 1, function(){
var noop = function(){};
equal(_.compose(noop), noop);
});

}());

/*--------------------------------------------------------------------------*/
Expand Down

0 comments on commit ae645cd

Please sign in to comment.