op-lift

turns js operators into delicious first-class functions

op-lift

Turn js operators into delicious first-class functions.

var lift = require('op-lift')
 
;[1, 2, 3, 4].reduce(lift.infix('+')) //= 10 

install via npm: npm install op-lift test: cd node_modules/op-lift && npm test

lift takes a string, representing an infix operator, and returns a function that performs the same job.

var lift = require('op-lift')
 
;[1, 2, 3, 4].reduce(lift.infix('*')) //= 24 

The supported operators are: * / % + - << >> >>> < > <= >= instanceof in == != === !== & ^ | || && . ,

Identical to lift.infix, except it returns functions taking a single argument:

var neg = lift.prefix('!')
 
;[true, false, true].map(neg) //= [ false, true, false ] 
var instantiate = lift.prefix('new'),
    type        = lift.prefix('typeof')
 
;[Object, Array, Function].map(instantiate).map(type) //= [ 'object', 'object', 'function' ] 

The supported operators are: void typeof ++ -- + - ~ ! new

Just for the sake of completeness, the postfix operators are included; neither of which do anything useful, since the 'magic' they do is sandboxed:

var i       = 0,
    postInc = lift.postfix('++')
    
postInc(i) //= 0 
i          //= 0 

The supported operators are: ++ --

Since the primary difference between operators and functions is that functions are first class, op-lift works very well with libraries heavy on the higher-order functions, including:

  • tap - allows functions to be injected into the method chain
  • underscore - uses functional iterators, and has standard functional partial application & composition.