Skip to content

Instantly share code, notes, and snippets.

@danallison
Created January 7, 2016 23:09
Show Gist options
  • Save danallison/2e7b6d673d6f52c2bea8 to your computer and use it in GitHub Desktop.
Save danallison/2e7b6d673d6f52c2bea8 to your computer and use it in GitHub Desktop.
Like _.flow, but also accepts arrays of functions.
function flowMap () {
var functions = [].slice.call(arguments); // convert arguments to array
return function () {
var _this = this; // maintain context
return functions.reduce(function (args, fn) {
// for each function or array of functions,
// pass in the previous return value.
if (_.isArray(fn)) {
return [fn.map(function (_fn) { return _fn.apply(_this, args); })];
} else {
return [fn.apply(_this, args)];
}
}, arguments)[0];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment