2.3.0 • Published 8 years ago

xo-utils v2.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

Build Status Coverage Status Dependency Status devDependency Status

Helper utils for working with functions, arrays and objects in JavaScript. Full documentation at http://bjdixon.github.io/xo/

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
var xo = require('xo-utils');

var curry = require('xo-utils').curry;

##Contains

##Usage

####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;

const addTen = xo.curry(add, 10);

addTen(32); // => 42

####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) passing the the return value of each as an 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 (in the order supplied) passing the the return value of each as an argument to the next function.

const increment = (a) => a + 1;
const square = (a) => a * a;

const plusOneSquare = xo.pipe(increment, square);
plusOneSquare(3); // => 16

####filter

Takes an array and a predicate. 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(objArr, xo.curry(compare, '003')); // => [{ name: 'b', id: '003'},{name: 'c', id: '003'}] 

####findIndex

Takes an array and a predicate. 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(objArr, xo.curry(compare, '003')); // => 2

####findKey

Takes an object and a predicate. Returns the key of the first term that passes the predicate.

const compare = (id, obj) => id === obj.id;
const obj = {
  hello: { name: 'a', id: '001' },
  goodbye: { name: 'b', id: '002' },
  yes: { name: 'c', id: '003' },
  no: { name: 'd', id: '004' }
} ;
xo.findKey(obj, xo.curry(compare, '003')); // => 'yes'

####find

Takes an object or an array and a predicate. Returns the value of the first term that passes the predicate.

const compare = (id, obj) => id === obj.id;
const obj = {
  hello: { name: 'a', id: '001' },
  goodbye: { name: 'b', id: '002' },
  yes: { name: 'c', id: '003' },
  no: { name: 'd', id: '004' }
};
const objArr = [
  { name: 'a', id: '001' },
  { name: 'b', id: '002' },
  { name: 'c', id: '003' },
  { name: 'd', id: '004' }
];
xo.find(obj, xo.curry(compare, '003')); // => { name: 'c', id: '003' }
xo.find(objArr, xo.curry(compare, '003')); // => { name: 'c', id: '003' }

####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();
const memoUpper = xo.memoize(upper);
memoUpper('foo'); // => "FOO"
memoUpper('foo'); // => "FOO" (cached version)

####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;
const maybeSum = xo.maybe(sum);
maybeSum(2, 3); // => 5
maybeSum(null, 3); // doesn't invoke sum

####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;

const addTen = xo.partial(add, 10);

addTen(32); // => 42

####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;
2.3.0

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

0.4.0

9 years ago

0.3.2

9 years ago