Compose Middleware Style Functions

Compose a list of functions so each wraps the next, matching the middleware style used by many web frameworks.

JavascriptResilience

Code

snippet
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);
}