Last active
August 2, 2017 14:50
-
-
Save Tymski/2e9632db00ac76d65d0329ef65996c3f to your computer and use it in GitHub Desktop.
JavaScript pipe, object oriented pipe declaration with example, pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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