Skip to content

Instantly share code, notes, and snippets.

@Tymski
Last active August 2, 2017 14:50
Show Gist options
  • Save Tymski/2e9632db00ac76d65d0329ef65996c3f to your computer and use it in GitHub Desktop.
Save Tymski/2e9632db00ac76d65d0329ef65996c3f to your computer and use it in GitHub Desktop.
JavaScript pipe, object oriented pipe declaration with example, pipeline
//Pipe definition:
Object.defineProperty(Object.prototype, 'pipe',{
value: function(...f){ return f.reduce((val, fun) => fun(val), this)},
writable: true,
configurable: true,
enumerable: false
});
//Easier logging to console:
log = console.log;
Object.prototype.log = function(){ log(this.toString())};
//Our functions for piping:
doubleSay = x => x + ", " + x
exclaim = x => x + "!"
capitalize = x => x[0] = x[0].toUpperCase() + x.slice(1)
//Displaying: "Hello, hello!" in console:
"hello"
.pipe(doubleSay)
.pipe(exclaim)
.pipe(capitalize)
.log()
//hello = "Hello, hello!"
hello = "hello".pipe(doubleSay,exclaim,capitalize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment