Shakeable, tested javascript utility functions in a pure functional programming style
Short answer: Yet another utility library
Long answer: Skald is a utility library to aid in pure functional javascript programming. It's meant to be shakeable, light-weight, and 100% tested. The entire library will compile down to < 7kb before gzipping (at present) and contains ~90 functions.
Here's an example of a block of a short ES6 module
const sumPlusOne = arr => arr
.map(val => val + 1)
.reduce((acc, val) => acc + val, 0);
export default sumPlusOne;
Here's an example of how that code might be written with skald
import { add, compose, mapBy, reduceBy } from 'skald';
const addOne = add(1);
const mapByAddOne = mapBy(addOne);
const reduceByAdd = reduceBy(add, 0);
const sumPlusOne = compose(reduceByAdd, mapByAddOne);
export default sumPlusOne;
It's a little more code in your module, but:
That's about it.
number
Add two numbers
boolean
Determine if all arguments are truthy or if array is truthy
boolean
| function
Take an array of functions (or values) and determine if all results are true given value
function
| string
Append string to the end of another string
function
| array
Apply functions from an array to corresponding index in other array
Array
Returns an array of passed arguments
*
Returns copy of entry or character at given index in string or array
*
Attempt something. If an error is thrown, return something else. (Wrapper for try / catch)
function
Takes a function and arguments. Leaves undefined arguments unbound and binds defined arguments to their position in arguments list.
function
Execute function with array as arguments
*
Take two arguments and if second argument is truthy, return first.
*
Take two arguments and if second argument is truthy, return first based on val
function
Returns a function which accepts no params and returns the passed value
function
Compose functions from right to left
function
Compose functions from right to left
function
| Array
Returns a new array, which is a merge of at least two arrays
function
| *
Curry arguments to function and return new function
boolean
| function
Allow comparison of two objects or arrays
*
Sets default value if passed value is falsy
function
| *
Take a function with a known signature and allow arguments to be passed until it executes
number
| function
Divide two numbers
boolean
| function
Determine if two values are equal
function
| boolean
Determine if all values in array satisfy function
boolean
| function
Returns true if string is not in string or array
function
| *
Create a function which executes a function based on a defined value
function
Create a function which executes a function with each arg being transformed by a function
function
| arr
Fill an array with a defined value
function
| Array
Filter elements in an array by function
Array
Returns an empty array
Object
Returns an empty object
function
| Object
Get an object with single property and value
function
| *
Get property of an object
function
| boolean
Determine if first value is greater than
function
| boolean
Determine if value is greater than or equal to other value
function
| boolean
Return true if object has key
*
A function which returns whatever is passed into it
boolean
| function
Returns true if string is in string or array
*
Invoke a function without arguments
boolean
Determine if value is array
boolean
Determine if value is boolean;
boolean
Check whether object, array, or string is empty
boolean
Determine if value is function
boolean
Determine if value is NaN
boolean
Determine if value is null
boolean
Determine if value is function
boolean
Detemine if value is object
boolean
Take a value and determine if it is a promise
boolean
Determine if string is property of object
boolean
Determine if value is string
boolean
Determine if value is undefined
Array
Return array of function iterations of specified length generated from 0-based index
function
| string
Join array to string, delimited by other string
function
| boolean
Determine if value is less than other value
function
| boolean
Determine if value is less than or equal to other value
function
| Array
Map elements in an array by function
function
| *
Execute a function if the argument is not null or undefined
function
Cache return contents of functions
Object
Returns new object, which is a shallow merge of multiple objects
function
| number
Multiply two numbers together
boolean
Returns true if no argument or array value is true
undefined
Executes a noop
boolean
| function
Returns false if truthy, true if falsy, negation if function
function
| boolean
Return true if two values are not equal
boolean
Determine if at least one argument or array value is truthy
boolean
| function
Take an array of functions (or values) and determine if one result is true given value
function
| Array
Generate array based on template of indexes and source
function
| number
Return exponent from one number to another
function
| string
Prepend string to the beginning of another string
function
| *
Reduce array to new value by function
string
| function
Replace search with new value in string
function
Take a function and return a function which accepts args in reverse order
function
| Object
Returns a copy of an object with a new name / value pair
function
| Array
| string
Slice an array or string
function
| boolean
Determine if at least one value in array satisfy function
function
| Array
Split string to array by another string
Array
Convert argument list to array (alias args)
function
| number
Subtract one number from another
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result.
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result.
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result with passed in parameter;
Array
Force args to array if not arrays
boolean
Returns boolean value and converts string 'false' to false
function
Returns function which returns value if value is not a function.
number
Parses int or float or Infinity to numeric value
Object
Forces value into object. If not object, returns {}
Promise
Take a value and if not a promise, make it a promise
string
Converts value to string. Converts undefined to empty string.
*
Safely traverse object nested properties
*
Safely traverse object nested properties
string
Returns typeof value
number
Add two numbers
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number |
b | number |
Example
add(1,2); //=> 3
add(2)(3); //=> 5
boolean
Determine if all arguments are truthy or if array is truthy
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
...args | * |
Example
and(true, true); //=> true
and([true, true]); //=> true
and(true, true, false); //=> false
boolean
| function
Take an array of functions (or values) and determine if all results are true given value
Kind: global function
Since: 1.10.0
Param | Type |
---|---|
fns | Array |
val | * |
Example
const foo = val => val < 10;
const bar = val => val > 5;
andWith([foo, bar], 6); //=> true
andWith([foo, bar])(1); //=> false
function
| string
Append string to the end of another string
Kind: global function
Since: 1.13.0
Param | Type |
---|---|
str | string |
append | string |
Example
appendTo('foo', 'bar'); //=> 'foobar'
appendTo('bar')('baz'); //=> 'barbaz'
function
| array
Apply functions from an array to corresponding index in other array
Kind: global function
Since: 1.11.0
Param | Type |
---|---|
fns | Array |
vals | Array |
Example
const add1 = a => a + 1;
const add2 = a => a + 2;
apply([add1, add2], [0, 0]); //=> [1, 2];
apply([add1])([1, 2, 3]); //=> [2, 2, 3];
Array
Returns an array of passed arguments
Kind: global function
Since: 1.16.0
Param | Type |
---|---|
...vals | * |
Example
args(1, 2, [3, 4]); //=> [1, 2, [3, 4]]
*
Returns copy of entry or character at given index in string or array
Kind: global function
Since: 1.5.0
Param | Type |
---|---|
index | number |
val | Array | string |
Example
at(2, 'foo'); //=> 'o'
at(1)([0, 1, 2]); //=> 1
*
Attempt something. If an error is thrown, return something else. (Wrapper for try / catch)
Kind: global function
Since: 1.7.0
Param | Type |
---|---|
toTry | * |
onError | * |
Example
attempt(() => JSON.parse('<>'), false); //=> false
attempt(5, () => ({})); //=> 5
function
Takes a function and arguments. Leaves undefined arguments unbound and binds defined arguments to their position in arguments list.
Kind: global function
Since: 1.8.0
Param | Type |
---|---|
fn | function |
...args | * |
Example
const foo = (a, b, c) = a + b + c;
bindTo(foo, 1, 2)(3); //=> 6
bindTo(foo, undefined, 1, 2)(1); //=> 4
bindTo(foo, undefined, undefined, 3)(1, 2); //=> 6
bindTo(foo, undefined, 1)(1)(1); //=> 3
function
Execute function with array as arguments
Kind: global function
Since: 1.17.0
Param | Type |
---|---|
fn | function |
arr | Array |
Example
const foo = (a, b) => a + b;
call(foo, [1, 2]) //=> 3
call(foo)([2, 3]) //=> 5
*
Take two arguments and if second argument is truthy, return first.
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
cb | * |
predicate | * |
Example
callback('foo', true); //=> 'foo'
callback('foo', false); //=> null
*
Take two arguments and if second argument is truthy, return first based on val
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
cb | * |
predicate | * |
val | * |
Example
callbackWith(a => a, true, 3); //=> 3
callbackWith(a => a, false, 3); //=> null
function
Returns a function which accepts no params and returns the passed value
Kind: global function
Since: 1.16.0
Param | Type |
---|---|
val | * |
Example
cast(6); //=> () = 6;
function
Compose functions from right to left
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
...args | function |
Example
compose(val => val + 1, val => val + 2); //=> val => val + 3
function
Compose functions from right to left
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
...args | function |
Example
compose(val => val + 1, val => val + 2); //=> val => val + 3
function
| Array
Returns a new array, which is a merge of at least two arrays
Kind: global function
Since: 1.16.0
Param | Type |
---|---|
...args | Array |
Example
const([1, 2], [3, 4]); //=> [1, 2, 3, 4]
function
| *
Curry arguments to function and return new function
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
fn | function |
...args | * |
Example
const foo = (a, b, c) => a + b + c;
curry(foo, 1, 2)(3); //=> 6
curry(foo, 1)(3, 4); //=> 8
curry(foo, 2)(3)(4); //=> 9
curry(foo)(1)(1)(1); // => 3
boolean
| function
Allow comparison of two objects or arrays
Kind: global function
Since: 1.4.0
Param | Type |
---|---|
a | * |
b | * |
Example
deepEquals({}, {}); //=> true
deepEquals([])([]); //=> true
*
Sets default value if passed value is falsy
Kind: global function
Since: 1.2.0
Param | Type |
---|---|
def | * |
val | * |
Example
defaultTo(5, undefined); //=> 5
defaultTo(3)(4); //=> 4
function
| *
Take a function with a known signature and allow arguments to be passed until it executes
Kind: global function
Update:
Since: 1.0.0
Param | Type |
---|---|
fn | function |
Example
const foo = (a, b, c) => a + b + c;
const bar = define(foo);
bar(1); // (b, c) => 1 + b + c
bar(1)(2); // c => 1 + 2 + c
bar(1)(2)(3); // 6
number
| function
Divide two numbers
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number |
b | number |
Example
divide(9, 3); //=> 3
divide(4)(2); //=> 2
boolean
| function
Determine if two values are equal
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | * |
b | * |
Example
equals(1, 1); //=> true
equals(1)(2); //=> false
function
| boolean
Determine if all values in array satisfy function
Kind: global function
Since: 1.17.0
Param | Type |
---|---|
fn | function |
arr | Array |
Example
const isTrue = val => val === true;
everyBy(isTrue, [true, true]) //=> true
everyBy(isTrue)([true, false]) //=> false
boolean
| function
Returns true if string is not in string or array
Kind: global function
Since: 1.4.0
Param | Type |
---|---|
search | string | number |
val | string | Array |
Example
excludes('h', 'hello'); //=> false
excludes('a')('apple'); //=> false
function
| *
Create a function which executes a function based on a defined value
Kind: global function
Since: 1.11.0
Param | Type |
---|---|
val | * |
fn | function |
Example
const addOne = a => a + 1;
executeOn(1, addOne); //=> 2
executeOn(2)(addOne); //=> 3
function
Create a function which executes a function with each arg being transformed by a function
Kind: global function
Since: 1.11.0
Param | Type |
---|---|
fn | function |
...args | * |
Example
const foo = (a, b) => a + b;
const add1 = a => a + 1;
const add2 = b => b + 2;
executeWith(foo, add1, add2)(0, 0); //=> 3
executeWith(foo, add1)(0, 0); //=> 1
function
| arr
Fill an array with a defined value
Kind: global function
Since: 1.17.0
Param | Type |
---|---|
val | val |
arr | Array |
Example
fillBy(1, [undefined, undefined]); //=> [1, 1]
fillBy(2)([undefined, undefined); //=> [2, 2]
function
| Array
Filter elements in an array by function
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
fn | function |
arr | Array |
Example
const foo = val => val < 3;
filterBy(foo, [1, 2, 4]); //=> [1, 2]
filterBy(foo)([2,3]); //=> [2]
Array
Returns an empty array
Kind: global function
Since: 1.16.0
Example
getEmptyArray(); //=> []
Object
Returns an empty object
Kind: global function
Since: 1.16.0
Example
getEmptyObj(); //=> {}
function
| Object
Get an object with single property and value
Kind: global function
Since: 1.15.0
Param | Type |
---|---|
prop | string | number |
value | * |
Example
getObject('a', 1); //=> { a: 1 }
getProp('b')(2); //=> { b: 2 }
function
| *
Get property of an object
Kind: global function
Since: 1.15.0
Param | Type |
---|---|
prop | string | number |
obj | Object |
Example
getProp('a', { a: 1 }); //=> 1
getProp('b')({ b: 2 }); //=> 2
function
| boolean
Determine if first value is greater than
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number | string |
b | number | string |
Example
gt(2, 1); //=> true
gt('b')('c'); //=> false
function
| boolean
Determine if value is greater than or equal to other value
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number | string |
b | number | string |
Example
gte(1, 1); //=> true
gte('b')('a'); //=> true
function
| boolean
Return true if object has key
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
obj | Object |
key | string |
Example
has({ a: 1 }, 'a'); //=> true
has({ a: 1 })('a'); //=> true
has({ a: 1 })('b'); //=> false
*
A function which returns whatever is passed into it
Kind: global function
Params: *
val
Since: 1.5.0
Example
identity(5); //=> 5
identity({}); //=> {}
boolean
| function
Returns true if string is in string or array
Kind: global function
Since: 1.4.0
Param | Type |
---|---|
search | string | number |
val | string | Array |
Example
includes('h', 'hello'); //=> true
includes('a')('apple'); //=> true
*
Invoke a function without arguments
Kind: global function
Since: 1.18.0
Param | Type |
---|---|
fn | function |
Example
var foo = () => 1;
invoke(foo); //=> 1;
boolean
Determine if value is array
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isArray([]); //=> true
isArray(1); //=> false
boolean
Determine if value is boolean;
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isBoolean(1); //=> false
isBoolean(false); //=> true
boolean
Check whether object, array, or string is empty
Kind: global function
Since: 1.5.0
Param | Type |
---|---|
val | Object | Array | string |
Example
isEmpty([]); //=> true
isEmpty({}); //=> true
isEmpty(''); //=> true
boolean
Determine if value is function
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isFunction(1); //=> false
isFunction(() => ({})); //=> true
boolean
Determine if value is NaN
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | number |
Example
isNaN(NaN); //=> true
isNaN(1); //=> false
boolean
Determine if value is null
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isNull(null); //=> true
isNull({}); //=> false
boolean
Determine if value is function
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isNumber(1); //=> true
isNumber([]); //=> false
boolean
Detemine if value is object
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isObject({}); //=> true
isObject(1); //=> false
boolean
Take a value and determine if it is a promise
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
val | * |
Example
isPromise(Promise.resolve()); //=> true
isPromise('foo'); //=> false
boolean
Determine if string is property of object
Kind: global function
Since: 1.13.0
Param | Type |
---|---|
key | string |
obj | Object |
Example
isPropertyOf('foo', { foo: 'a' }); //=> true
isPropertyOf('foo')({ bar: 'b' }); //=> false
boolean
Determine if value is string
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isString('foo'); //=> true;
isString(true); //=> false;
boolean
Determine if value is undefined
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
isUndefined(undefined); //=> true
isUndefined(true); //=> false
Array
Return array of function iterations of specified length generated from 0-based index
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
fn | function |
len | number |
Example
const foo = index => index + 1;
iterate(foo, 3); //=> [1, 2, 3]
iterate(foo)(2); //=> [1, 2]
function
| string
Join array to string, delimited by other string
Kind: global function
Since: 1.13.0
Param | Type |
---|---|
delimiter | string |
arr | Array |
Example
joinBy('.', ['foo', 'bar', 'baz']); //=> 'foo.bar.baz'
joinBy(',')([1, 2, 3]); //=> '1.2.3';
function
| boolean
Determine if value is less than other value
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number | string |
b | number | string |
Example
lt(1, 2); //=> true
lt('a')('b'); //=> true
function
| boolean
Determine if value is less than or equal to other value
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number | string |
b | number | string |
Example
lte(1, 1); //=> true
lte('a')('b'); //=> true
function
| Array
Map elements in an array by function
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
fn | function |
arr | Array |
Example
const foo = val => val + 1;
mapBy(foo, [1, 2]); //=> [2, 3]
mapBy(foo)([4, 5]); //=> [5, 6]
function
| *
Execute a function if the argument is not null or undefined
Kind: global function
Since: 1.23.0
Author: oculus42
Param | Type |
---|---|
fn | function |
val | * |
Example
const foo = val => val + 1;
maybe(foo, 1); //=> 2
maybe(foo)(null); //=> null
function
Cache return contents of functions
Kind: global function
Since: 1.9.0
Param | Type | Description |
---|---|---|
fn | function |
Function to templatize |
[function] | template - Function to determine cache key |
Object
Returns new object, which is a shallow merge of multiple objects
Kind: global function
Since: 1.16.0
Param | Type |
---|---|
...args | Object |
Example
merge({ a: 1 }, { b: 2}); //=> { a: 1, b: 2 }
function
| number
Multiply two numbers together
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number |
b | number |
Example
multiply(2, 3); //=> 6
multiply(2)(2); //=> 4
boolean
Returns true if no argument or array value is true
Kind: global function
Since: 1.4.0
Param | Type |
---|---|
...args | * |
undefined
Executes a noop
Kind: global function
Since: 1.5.0
boolean
| function
Returns false if truthy, true if falsy, negation if function
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
val | * |
Example
const identity = a => a;
not(1); //=> false
not(false); //=> true
not(identity)(true); //=> false
function
| boolean
Return true if two values are not equal
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
a | * |
b | * |
Example
notEquals(1, 2); //=> true
notEquals(3)(4); //=> true
boolean
Determine if at least one argument or array value is truthy
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
...args | * |
Example
or(true, false, false); //=> true
or([false, false, true]); //=> true
boolean
| function
Take an array of functions (or values) and determine if one result is true given value
Kind: global function
Since: 1.10.0
Param | Type |
---|---|
args | Array |
val | * |
Example
const foo = val => val > 10;
const bar = val => val < 5;
orWith([foo, bar], 6); //=> false
orWith([foo, bar])(1); //=> true
function
| Array
Generate array based on template of indexes and source
Kind: global function
Since: 1.19.0
Param | Type |
---|---|
template | Array |
src | Array |
Example
orderBy([1, 2, 0], ['a', 'b', 'c']); //=> ['b', 'c', 'a'];
orderBy([2, 0, 1])(['d', 'e', 'f']); //=> ['f', 'd', 'e'];
function
| number
Return exponent from one number to another
Kind: global function
Since: 1.20.0
Param | Type |
---|---|
exponent | number |
base | number |
Example
power(2, 3); //=> 9
power(2)(2); //=> 4
function
| string
Prepend string to the beginning of another string
Kind: global function
Since: 1.13.0
Param | Type |
---|---|
str | string |
append | string |
Example
prependTo('foo', 'bar'); //=> 'barfoo'
prependTo('bar')('baz'); //=> 'bazbar'
function
| *
Reduce array to new value by function
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
fn | function |
accumulator | * |
arr | Array |
Example
const foo = (acc, val) = acc + val;
reduceBy(foo, 1, [1, 1]); //=> 3
reduceBy(foo, 2)([2, 2]); //=> 6
reduceBy(foo)(3)([3, 3]); //=> 9
string
| function
Replace search with new value in string
Kind: global function
Since: 1.6.0
Param | Type |
---|---|
search | string | RegExp |
rep | string |
str | string |
Example
replaceWith('f', 'b', 'foo'); //=> 'boo'
replaceWith(/o/g)('a')('foo'); //=> 'faa'
function
Take a function and return a function which accepts args in reverse order
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
fn | function |
Example
const foo = (a, b, c) => a + b - c;
reverse(foo); //=> (c)(b)(a) => c + b - a;
function
| Object
Returns a copy of an object with a new name / value pair
Kind: global function
Since: 1.21.0
Param | Type |
---|---|
prop | string | number |
value | * |
obj | Object |
Example
setProp('a', 1, {}); //=> { a: 1 }
getProp('b')(2)({}); //=> { b: 2 }
function
| Array
| string
Slice an array or string
Kind: global function
Since: 1.17.1
Param | Type |
---|---|
start | number |
end | number |
val | Array | string |
Example
sliceFrom(0, 1, [1, 2, 3]) //=> [0];
function
| boolean
Determine if at least one value in array satisfy function
Kind: global function
Since: 1.17.0
Param | Type |
---|---|
fn | function |
arr | Array |
Example
const isTrue = val => val === true;
someBy(isTrue, [true, false]) //=> true
someBy(isTrue)([false, false]) //=> false
function
| Array
Split string to array by another string
Kind: global function
Since: 1.13.0
Param | Type |
---|---|
search | string |
str | string |
Example
splitBy('.', 'foo.bar.baz'); //=> ['foo', 'bar', 'baz']
splitBy(',')('1,2,3'); //=> ['1', '2', '3'];
Array
Convert argument list to array (alias args)
Kind: global function
Since: 1.17.0
Param | Type |
---|---|
...args | * |
Example
spread(1, 2, 3); //=> [1, 2, 3]
function
| number
Subtract one number from another
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
a | number |
b | number |
Example
subtract(3, 2); //=> 1
subtract(2)(1); //=> 1
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result.
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
failure | * |
success | * |
predicate | * |
Example
ternary(1, 2, true); //=> 2
ternary(1, 2, false); //=> 1
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result.
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
predicate | * |
success | * |
failure | * |
Example
ternaryL(true, 1, 2); //=> 1
ternaryL(false, 1, 2); //=> 2
*
Return success or failure based on predicate evaluation. If success or failure are functions, returns executed result with passed in parameter;
Kind: global function
Since: 1.9.0
Param | Type |
---|---|
failure | * |
success | * |
predicate | * |
val | * |
Example
const foo = val => val + 1;
const bar = val => val - 1;
ternaryWith(bar, foo, true, 3); //=> 4
ternaryWith(bar, foo, false, 2); //=> 1
Array
Force args to array if not arrays
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
...args | * |
Example
toArray([1, 2]); //=> [1, 2]
toArray(2, 3); //=> [2, 3]
boolean
Returns boolean value and converts string 'false' to false
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
value | * |
Example
toBoolean('false'); //=> false
toBoolean({}); //=> true
function
Returns function which returns value if value is not a function.
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
value | * |
Example
toFunction(3); //=> () => 3
toFunction(() => 1); //=> () => 1
number
Parses int or float or Infinity to numeric value
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
value | * |
Example
toNumber('Infinity'); //=> Infinity
toNumber('1.0'); //=> 1
toNumber({}); //=> NaN
Object
Forces value into object. If not object, returns {}
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
value | * |
Example
toObject({ a: 1 }); //=> { a: 1 }
toObject(null); //=> null
toObject('foo'); //=> {}
Promise
Take a value and if not a promise, make it a promise
Kind: global function
Since: 1.1.0
Param | Type |
---|---|
val | * |
Example
const foo = toPromise(5);
foo.then(console.log); //=> 5;
string
Converts value to string. Converts undefined to empty string.
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
value | * |
Example
toString('foo'); //=> 'foo'
toString(false); //=> 'false'
toString(undefined); //=> ''
*
Safely traverse object nested properties
Kind: global function
Since: 1.6.0
Param | Type |
---|---|
obj | Object |
path | Array.<string> |
Example
traverse({}, ['a', 'b', 'c']); //=> undefined
traverse({ a: 1 })(['a']); //=> 1
*
Safely traverse object nested properties
Kind: global function
Since: 1.14.0
Param | Type |
---|---|
path | Array.<string> |
obj | Object |
Example
traverse(['a', 'b', 'c'], {}); //=> undefined
traverse(['a'])({ a: 1 }); //=> 1
string
Returns typeof value
Kind: global function
Since: 1.0.0
Param | Type |
---|---|
val | * |
Example
typeOf([]); //=> 'object'
typeOf(undefined); //=> 'undefined'
typeOf(5); //=> 'number'