Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Last active January 28, 2017 11:35
Show Gist options
  • Save railsstudent/352cee62633795bde5a3c34774b40c3f to your computer and use it in GitHub Desktop.
Save railsstudent/352cee62633795bde5a3c34774b40c3f to your computer and use it in GitHub Desktop.
function chainFunctions(fns) {
for (let fname in fns) {
let originalFn = fns[fname];
let fn = (...inputs) => {
if (this.result != null) {
inputs.splice(0, 0, this.result);
}
let cloneCF = new chainFunctions(fns);
cloneCF.result = originalFn.apply(this, inputs);
return cloneCF
};
Object.defineProperty(this, fname, { value: fn });
}
}
chainFunctions.prototype.execute = function() { return this.result; }
function chain(fns) {
return new chainFunctions(fns);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment