Compose Middleware Style Functions
Compose a list of functions so each wraps the next, matching the middleware style used by many web frameworks.
Code
export function compose(...middleware) {
if (middleware.length === 0) return (input) => input;
return middleware.reduceRight((next, layer) => {
return (input) => layer(input, next);
});
}
export function pipe(...transforms) {
return (input) => transforms.reduce((value, fn) => fn(value), input);
}Report a problem with this exampleOpens a pre-filled issue on GitHub.