op-lift
Turn js operators into delicious first-class functions.
Example
var lift = ;1 2 3 4 //= 10
Install & Test
install via npm: npm install op-lift
test: cd node_modules/op-lift && npm test
Requires an es5, or es5-shim for older engines.
API
lift(String) -> Function
lift takes a string representing an operator, and returns a function that performs the same job.
var lift = ;1 2 3 4 //= 24 ;true false true //= [ false, true, false ];Object Array Function //= [ 'object', 'object', 'function' ]
It's safe to use with arbitrary strings, too, since lift will simply return null
for unrecognised operators:
//= null
Supported operators:
The supported operators are:
prefix: void typeof ++ -- + - ~ ! new
infix: * / % + - << >> >>> < > <= >= instanceof in == != === !== & ^ | || && . ,
postfix: ()
weirdfix: ?:
Gotchas
No Shortcircuiting
Since the resulting functions simply accept values as arguments, no short-circuiting can be done:
false // an alert still shows up with 'BOO'
No Updating of Variables
Any operator that affects a variables' value won't in its lifted form:
var a = 0a // 1a // 0
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.