Introduction
xo is a library I use to work with functions, arrays and objects in JavaScript.
Source code
The source is available at: http://github.com/bjdixon/xo.
Installation
Browser
Download and add to your html pages.
<script type="text/javascript" src="xo.min.js"></script>
Node
Using npm:
npm install xo-utils
const xo = require('xo-utils');
Usage
compose
Takes functions and returns a function. The returned function when invoked will invoke each function that was supplied as an argument to compose (in reverse order) returning the result of each invocation as the argument to the next function.
const increment = (a) => a + 1;
const square = (a) => a a;
const squarePlusOne = xo.compose(increment, square);
squarePlusOne(3); // => 10
pipe
Takes functions and returns a function. The returned function when invoked will invoke each function that was supplied as an argument to pipe returning the result of each invocation as the argument to the next function.
const increment = (a) => a + 1;
const square = (a) => a a;
const plusOneSquare = xo.pipe(increment, square);
plusOneSquare(3); // => 16
curry
Takes a function and zero or more arguments to that function. Returns a function that can be invoked with remaining arguments at a later time.
const add = (a, b) => a + b;
var addTen = xo.curry(add, 10);
addTen(32); // => 42
xo.curry(add)(10, 32) === xo.curry(add)(10)(32); // => true
filter
Takes a predicate and an array. Returns an array with only those terms that pass the predicate.
const compare = (id, obj) => id === obj.id;
const objArr = [
{ name: 'a', id: '001' },
{ name: 'b', id: '003' },
{ name: 'c', id: '003' },
{ name: 'd', id: '004' }
];
xo.filter(xo.curry(compare, '003'), objArr); // => [{ name: 'b', id: '003'},{name: 'c', id: '003'}]
findIndex
Takes a predicate and an array. Returns the index of the first term that passes the predicate.
const compare = (id, obj) => id === obj.id;
const objArr = [
{ name: 'a', id: '001' },
{ name: 'b', id: '002' },
{ name: 'c', id: '003' },
{ name: 'd', id: '004' }
];
xo.findIndex(xo.curry(compare, '003'), objArr); // => 2
flatten
Takes an n-dimensional nested array. Returns a flattened 1-dimension array.
const test = [0, 1, [2, 3], [4, [5, 6]], 7, [8, [9]]];
xo.flatten(test); // => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
compact
Takes an array. Returns an array with all falsy values removed.
const test = [1, , false, 2, 3, false];
xo.compact(test); // => [1, 2, 3]
memoize
Takes a function and returns a functions. Invoking the returned function will return cached results if the same arguments have been provided during previous invocations.
const upper = (str) => str.toUpperCase();
var memoUpper = xo.memoize(upper);
memoUpper('foo'); // => "FOO"
memoUpper('foo'); // => "FOO" (cached version)
partial
Takes a function and zero or more arguments to that function. Returns a function that can be invoked with remaining arguments at a later time.
const add = (a, b) => a + b;
var addTen = xo.partial(add, 10);
addTen(32); // => 42
isArray
Type check for Array.
xo.isArray([1, 2, 3]); // => true
xo.isArray(true); // => false
isFunction
Type check for Function.
xo.isFunction(function(){ return true; }); // => true
xo.isFunction(true); // => false
isObject
Type check for Object.
xo.isObject({ a: true }); // => true
xo.isObject(true); // => false
isString
Type check for String.
xo.isString('true'); // => true
xo.isString(true); // => false
isNumber
Type check for Number.
xo.isNumber(42); // => true
xo.isNumber('true'); // => false
isBoolean
Type check for Boolean.
xo.isBoolean(true); // => true
xo.isBoolean('true'); // => false
maybe
Takes a function and returns a function. The returned function will not be invoked if supplied with null or undefined arguments.
const sum = (a, b) => a + b;
var maybeSum = xo.maybe(sum);
maybeSum(2, 3); // => 5
maybeSum(null, 3); // doesn't invoke sum
map
Takes an array and a function. Returns an array that is the result of applying the function to each term of the original array.
const arr = [1, 2, 3, 4];
const square = (a) => a * a;
const out = xo.map(square, arr); // => [1, 4, 9, 16];
reduce
Takes an array, an initial value and a function. Returns a single value that is the result of applying the function to each term of the array and an accumulator.
const arr = [1, 2, 3, 4];
const sum = (a, b) => a + b;
const out = xo.reduce(sum, 0, arr); // => 10;