compose.js 580 B

12345678910111213141516171819202122
  1. "use strict";
  2. var callable = require("../../object/valid-callable")
  3. , aFrom = require("../../array/from")
  4. , apply = Function.prototype.apply
  5. , call = Function.prototype.call
  6. , callFn = function (arg, fn) {
  7. return call.call(fn, this, arg);
  8. };
  9. module.exports = function (fn /*, …fnn*/) {
  10. var fns, first;
  11. if (!fn) callable(fn);
  12. fns = [this].concat(aFrom(arguments));
  13. fns.forEach(callable);
  14. fns = fns.reverse();
  15. first = fns[0];
  16. fns = fns.slice(1);
  17. return function (argIgnored) {
  18. return fns.reduce(callFn, apply.call(first, this, arguments));
  19. };
  20. };