1.1.0 • Published 8 years ago

@f/middleware v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

middleware

Build status Git tag NPM version Code style

Utility for middleware composition.

Installation

$ npm install @f/middleware

Usage

var middleware = require('@f/middleware')
var pipe = require('ramda').pipe

var powAdd = middleware(composePipe)
  .use(Math.pow)
  .use(function (arg) {
    return arg + 1
  })

powAdd(2, 4) // => 17

var powAddMultiply = powAdd.add(function (arg) {
  return arg * 2
})

powAddMultiply(2, 4) // => 34

function composePipe(stack) {
  return pipe.apply(null, stack)
}

API

middleware(compose, stack)

  • compose - compose function with signature compose(stack)
  • stack - array of middleware

Returns: A function representing the composition of the stack. The composed function additionally has helper properties for manipulating the stack.

  • .use(fn): adds fn to stack
  • .add(fn): immutably add fn to stack
  • .copy(fn): make copy of stack return new composed function
  • .map(fn): map stack over fn and return new composed function

License

MIT