Created
January 31, 2019 09:19
-
-
Save simonpai/abb3cb114b78475363e9bc0c8f2dea4a to your computer and use it in GitHub Desktop.
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
| function combine() { | |
| return Array.prototype.slice.call(arguments) | |
| .reverse() | |
| .reduce(function(acc, m) { | |
| return function(left, right, next) { | |
| return m(left, right, function(l, r) { | |
| return acc(l, r, next); | |
| }); | |
| }; | |
| }); | |
| } | |
| function set(prop) { | |
| return function(left, right) { | |
| return (left[prop] = right); | |
| } | |
| } | |
| function merge() { | |
| return function(left, right) { | |
| return Object.assign(left, right); | |
| } | |
| } | |
| function exec(prop) { | |
| return prop ? | |
| function(left, right) { | |
| return left[prop](right); | |
| } : | |
| function(left, right) { | |
| return left(right); | |
| } | |
| } | |
| function _mapLeft(fn) { | |
| fn = _asFunc(fn); | |
| return function(left, right, next) { | |
| return next(fn(left), right); | |
| } | |
| } | |
| function _mapRight(fn) { | |
| fn = _asFunc(fn); | |
| return function(left, right, next) { | |
| return next(left, fn(right)); | |
| } | |
| } | |
| function _mapMultiple(fn) { | |
| return function() { | |
| return combine.apply(undefined, Array.prototype.map.call(arguments, fn)); | |
| } | |
| } | |
| function _asFunc(fn) { | |
| if (typeof fn === 'string') { | |
| return function(obj) { | |
| return obj[fn]; | |
| } | |
| } | |
| return fn; | |
| } | |
| module.exports = { | |
| combine: combine, | |
| set: set, | |
| exec: exec, | |
| map: { | |
| left: _mapMultiple(_mapLeft), | |
| right: _mapMultiple(_mapRight) | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment