compose.js 560 B

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