Last active
January 27, 2016 21:48
-
-
Save matths/28f8a6f65751b508c78b to your computer and use it in GitHub Desktop.
composition with lodash _.flow(), and with flowArg to differently distribute arguments
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
var _ = require('lodash'); // 4.0.1 | |
// filter implementations doesn't matter right now | |
var _method = function (method, handler) { | |
return function (req, res, next) { | |
console.log('_method middleware', method); | |
handler(req, res, next); | |
// or call next() | |
} | |
} | |
var _path = function (path, handler) { | |
return function (req, res, next) { | |
console.log('_path middleware', path); | |
handler(req, res, next); | |
// or call next() | |
} | |
} | |
console.log('------------------------------'); | |
console.log('A:'); | |
var listenerA = _method('get', | |
_path('p/a/t/h/', function (req, res, next) { | |
console.log('end:', req, res); | |
}) | |
); | |
listenerA('request fake', 'response fake'); | |
console.log('------------------------------'); | |
var _get = _.partial(_method, 'get'); | |
console.log('B:'); | |
var listenerB = _get( | |
_path('p/a/t/h/', function (req, res, next) { | |
console.log('end:', req, res); | |
}) | |
); | |
listenerB('request fake', 'response fake'); | |
console.log('------------------------------'); | |
var _getPath = _.flow(_path, _get); | |
console.log('C:'); | |
var listenerC = _getPath('p/a/t/h/', function (req, res, next) { | |
console.log('end:', req, res); | |
}); | |
listenerC('request fake', 'response fake'); | |
console.log('------------------------------'); | |
var _flowArg = function (f, g) { | |
return function(gArg, fArg, handler) { | |
return g(gArg, f(fArg, handler)) | |
}; | |
} | |
var _requestPath = _flowArg(_path, _method); | |
console.log('D:'); | |
var listenerD = _requestPath('get', 'p/a/t/h/', function (req, res, next) { | |
console.log('end:', req, res); | |
}); | |
listenerD('request fake', 'response fake'); | |
console.log('------------------------------'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment