operator

Functional operators

operator.js

Functional operators for JavaScript, both client- and server-friendly.

Client-side, you need only include the latest version of the operator.js file in your page (download or link); this will add a single global operator to window.

You're also covered if you're using something like require.js or browserify to load your modules. Operator knows the module dance, and will check all of exports, module, and define so it can do the right thing.

For usage in node.js, install it via npm:

npm install operator

...And then require it:

var op = require('operator');

Contents

  • Everything lives in one namespace. I typically require it as op; when no module systems are present, it lives in a global named operator.
  • Throughout this API, I use "defined" to mean x != null (which is equivalent to typeof x !== 'undefined' && x !== null).
  • In progress! Use the source, Luke.

Operators which do nothing other than return a value.

Identity operator.

Constant operator: returns a function which returns k.

No-op operator (technically returns ).

Returns the current context of execution (this).

Returns a new object.

Returns a new array.

Returns o if defined, def otherwise.

True if o is defined.

True if o is not defined.

Returns a function which tests if its argument is equal to k.

Operators which deal with arguments to functions.

Returns the second argument.

Returns the n-th argument.

Returns a function that applies fn with whatever arguments it gets, but swapping the first two:

op.flip(fn)(a, b, c) === fn(b, a, c)

Returns a function that applies fn, but limited to n arguments:

op.aritize(fn, 1)(a, b, c) === fn(a)

Returns a function that applies fn, but limited to one argument (the same as op.aritize(fn, 1), but slightly faster):

op.it(fn)(a, b, c) === fn(a)

Log base-2.

Find a bug or want to contribute? Open a ticket (or fork the source!) on github. You're also welcome to send me email at dsc@less.ly.

operator.js was written by David Schoonover (in Coco, a dialect of CoffeeScript that compiles down to JavaScript). It is open-source software and freely available under the MIT License.