op-lift

turns js operators into delicious first-class functions

Usage no npm install needed!

<script type="module">
  import opLift from 'https://cdn.skypack.dev/op-lift';
</script>

README

op-lift

Turn js operators into delicious first-class functions.

Example

var lift = require('op-lift')

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

Install & Test

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

API

lift.infix(String) -> Function

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

Supported operators:

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

lift.prefix(String) -> Function

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' ]

Supported opertors:

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

lift.postfix(String) -> Function

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

Supported operators:

The supported operators are: ++ --

Tastes great with..

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.