Created
March 24, 2018 08:47
-
-
Save christianscott/9fbbe69040d9081afc6673faa61ba67e to your computer and use it in GitHub Desktop.
HOF to add `pipe` method to any function
This file contains hidden or 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
const assert = require("assert") | |
function pipes(func) { | |
return function () { | |
const value = func.apply(null, arguments) | |
return { | |
value, | |
pipe(func2) { | |
return func2(value) | |
} | |
} | |
} | |
} | |
const _add = (a, b) => a + b | |
const add = pipes(_add) | |
assert( | |
add(10, 10) | |
.pipe(x => x + 1) === 21, | |
"should pipe" | |
) | |
assert( | |
add(10, 10).value === 20, | |
"should expose value" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment