Methods
(static) compact(arr) → {Array}
Takes an array.
Returns an array with all falsy values removed
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | The array containing the elements to test |
Returns:
- Type
- Array
Example
const test = [1, , false, 2, 3, false]
xo.compact(test) // [1, 2, 3]
(static) compose(fnsopt) → {function}
Takes functions and returns a function.
The returned function when invoked will invoke each function
that was supplied as an argument to compose passing the result of
each invocation as the argument to the next function. The functions
supplied as arguments are invoked in reverse order, with the last
argument being called first
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fns |
function |
<optional> |
The functions to be composed |
Returns:
- Type
- function
Example
const increment = a => a + 1
const square = a => a * a
const squarePlusOne = xo.compose(increment, square)
squarePlusOne(3) // 10
(static) curry(fn, argsopt) → {function}
Takes a function with zero or more arguments.
Returns a function that can be invoked with remaining arguments at a later time
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
function | Partially apply this function prefilling some arguments |
|
args |
* |
<optional> |
Initial arguments that the partially applied function will be applied to. |
Returns:
- Type
- function
Example
const greet = (greeting, name) => [greeting, name].join(' ')
const sayHi = xo.curry(greet, 'Hi')
sayHi('Bob') // "Hi Bob"
(static) filter(predicate, arr) → {Array}
Takes an array and a predicate function.
Returns an array with only those terms that pass the predicate
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | The function against which each element of the array will be tested |
arr |
Array | The array containing the elements to test |
Returns:
- Type
- Array
Example
function compare(id, obj) {
return 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'}]
(static) find(predicate, {Array}) → {String}
Takes an object or an array and a predicate function.
Returns the value of the first term that passes the predicate
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | The function against which each property of the collection will be tested |
{Array} |
Object | collection - The object or array containing the elements to test |
Returns:
{Number}
- Type
- String
Example
function compare(id, obj) {
return 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.find(xo.curry(compare, '003'), obj) // {yes: { name: 'a', id: '003' }}
(static) findIndex(predicate, arr) → {Number}
Takes an array and a predicate function.
Returns the index of the first term that passes the predicate
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | The function against which each element of the array will be tested |
arr |
Array | The array containing the elements to test |
Returns:
- Type
- Number
Example
function compare(id, obj) {
return 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
(static) findKey(predicate, obj) → {String}
Takes an object and a predicate function.
Returns the key of the first term that passes the predicate
Parameters:
Name | Type | Description |
---|---|---|
predicate |
function | The function against which each property of the object will be tested |
obj |
Object | The object containing the elements to test |
Returns:
- Type
- String
Example
function compare(id, obj) {
return 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(xo.curry(compare, '003'), obj) // yes
(static) flatten(arr) → {Array}
Takes an n-dimensional nested array.
Returns a flattened 1-dimensional array.
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | The array that will be recursively flattened |
Returns:
- Type
- Array
Example
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]
(static) isArray(test) → {Boolean}
Type check for Array
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isArray([1, 2, 3]) // true
xo.isArray(true) // false
(static) isBoolean(test) → {Boolean}
Type check for Boolean
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isBoolean(true) // true
xo.isBoolean('true') // false
(static) isFunction(test) → {Boolean}
Type check for Function
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isFunction(function(){ return true }) // true
xo.isFunction(true) // false
(static) isNumber(test) → {Boolean}
Type check for Number
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isNumber(42) // true
xo.isNumber('true') // false
(static) isObject(test) → {Boolean}
Type check for Object
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isObject({ a: true }) // true
xo.isObject(true) // false
(static) isString(test) → {Boolean}
Type check for String
Parameters:
Name | Type | Description |
---|---|---|
test |
* | The argument to be checked |
Returns:
- Type
- Boolean
Example
xo.isString('true') // true
xo.isString(true) // false
(static) map(callback, collection) → {Array}
Takes an array and a function.
Returns an array that is the result of having the function applied
to each term of the supplied array.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | The function to be applied to each term of the supplied array |
collection |
Array | The array that we're operating on |
Returns:
- Type
- Array
Example
const arr = [1, 2, 3, 4]
const square = a => a * a
const out = xo.map(square, arr) // => [1, 4, 9, 16]
(static) maybe(fn) → {function}
Takes a function and returns a function.
The returned function will not be called if supplied with null
or undefined arguments
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The function to be invoked |
Returns:
- Type
- function
Example
const sum = (a, b) => a + b
const maybeSum = xo.maybe(sum)
maybeSum(2, 3) // 5
maybeSum(null, 3) // doesn't invoke sum
(static) memoize(fn) → {function}
Takes a function and returns a function.
Invoking the returned function will return cached results if the same
arguments have been provided during previous invocations.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | The (expensive) function that will have it's return values cached |
Returns:
- Type
- function
Example
const upper = str => str.toUpperCase()
const memoUpper = xo.memoize(upper)
memoUpper('foo') // "FOO"
memoUpper('foo') // "FOO" (cached version)
(static) noConflict() → {Object}
Allows users to avoid conflicts over the xo name
Returns:
- Type
- Object
Example
const ox = xo.noConflict()
(static) partial(fn, argsopt) → {function}
Takes a function with zero or more arguments.
Returns a function that can be invoked with the remaining arguments at a later time
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fn |
function | Partially apply this function prefilling some arguments |
|
args |
* |
<optional> |
Initial arguments that the partially applied function will be applied to. |
Returns:
- Type
- function
Example
const greet = (greeting, name) => [greeting, name].join(' ')
const sayHi = xo.partial(greet, 'Hi')
sayHi('Bob') // "Hi Bob"
(static) pipe(fnsopt) → {function}
Takes functions and returns a function.
The returned function when invoked will invoke each function
that was supplied as an argument to compose passing the result of
each invocation as the argument to the next function
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
fns |
function |
<optional> |
The functions to be composed |
Returns:
- Type
- function
Example
const increment = a => a + 1
const square = a => a * a
const plusOneSquare = xo.pipe(increment, square)
plusOneSquare(3) // 16
(static) reduce(callback, initialValue, collection) → {*}
Takes an array, an initial value and a function.
Returns a single value that is the result of having the function applied
to each term of the supplied array.
Parameters:
Name | Type | Description |
---|---|---|
callback |
function | The function to be applied to each term of the supplied array |
initialValue |
* | The value to use as the first argument to the first call of the callback |
collection |
Array | The array that we're operating on |
Returns:
- Type
- *
Example
const arr = [1, 2, 3, 4]
const sum = (a, b) => a + b
const out = xo.reduce(sum, 0, arr) // => 10