]> gerrit.simantics Code Review - simantics/district.git/blob - compose.js
1da5e01162dcf5ea52a08667045297e9fe847c07
[simantics/district.git] / compose.js
1 'use strict';
2
3 var callable = require('../../object/valid-callable')
4   , aFrom    = require('../../array/from')
5
6   , apply = Function.prototype.apply, call = Function.prototype.call
7   , callFn = function (arg, fn) { 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 (arg) {
18                 return fns.reduce(callFn, apply.call(first, this, arguments));
19         };
20 };