- Source:
Methods
(static) allEqual(array) → {Boolean}
Array -> Boolean
- Source:
Checks to see if all elements in an array are the same.
Example
TODO
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | An array of any values |
Returns:
True when all same, false otherwise
- Type
- Boolean
(static) allSettledP(promises) → {Promise}
[Promise] -> Promise<[Object]>
- Source:
A 'promise all' that does not reject when promises are rejected. Instead returns an array of
results. Each result will be an object. The object will have the following form for a
successfully settled promise:
{ state: 'fulfilled', value: }
A rejected result will look like:
{ state: 'rejected', reason: }
Similar to [this](https://github.com/kriskowal/q/wiki/API-Reference#promiseallsettled).
Example
await allSettledP([Promise.resolve('good'), Promise.reject('bad')]);
// => [{ state: 'fulfilled', value: 'good' }, { state: 'rejected', reason: 'bad' }]
Parameters:
Name | Type | Description |
---|---|---|
promises |
Array | An array of promises |
Returns:
A promise that will resolve to an array of settled/rejected results
- Type
- Promise
(static) argsToObj(keys, args) → {Object}
[k1, k2, k3] -> (a, b, c, d) -> { k1: a, k2: b, k3: c }
- Source:
Converts function arguments to an object based on the provided array of keys.
If an argument is passed that doesn't have a key it will be skipped.
Example
argsToObj(['foo', 'bar'])(1, 2); // { foo: 1, bar: 2 }
function foo() {
return argsToObj(['a', 'b', 'c'])(arguments);
}
foo(1, 2, 3, 4); // { a: 1, b: 2, c: 3 }
Parameters:
Name | Type | Description |
---|---|---|
keys |
Array | A list of props to assign arguments to (in order) |
args |
Arguments | Arguments to match to `keys` (in order) |
Returns:
Object of `keys` matched with `args`
- Type
- Object
(static) clampPositive(toClamp) → {Number}
Number -> Number
- Source:
Clamps a numeric value so that it's guaranteed to be 0 or higher. Works with
Numbers and other ordered types like Strings and Dates, though the behavior
when given a String is inconsistent (e.g. `clampPositive('5')` returns the
String '5', `clampPositive('-1')` returns the Number 0, and `clampPositive('foo')`
returns the string 'foo'). If given NaN it will return NaN.
Example
clampPositive(-1); // => 0
clampPositive(0); // => 0
clampPositive(20); // => 20
clampPositive(-Infinity); // => 0
clampPositive(Infinity); // => Infinity
clampPositive(NaN); // => NaN
Parameters:
Name | Type | Description |
---|---|---|
toClamp |
Number | Any number to clamp |
Returns:
`toClamp` or 0 if the number was negative
- Type
- Number
(static) containsAll(needle, haystack) → {Boolean}
Array<* a> -> Array<* a> -> Boolean
- Source:
Predicate that checks to see if each element in an array
exists in a larger array.
Example
containsAll([1, 3, 2], [1, 2, 3, 4, 5]); // => true
Parameters:
Name | Type | Description |
---|---|---|
needle |
Array | list to check with |
haystack |
Array | list to check against |
Returns:
True if all are found, false otherwise
- Type
- Boolean
(static) containsAny(needle, haystack) → {Boolean}
Array<* a> -> Array<* a> -> Boolean
- Source:
Takes two arrays and returns true if any of the values in the first array
are found in the second array.
Example
containsAny([1, 2], [1, 2, 3]); // => true
containsAny([0, 2, 4, 5, 6], [1, 2, 3]); // => true
containsAny(['foo'], ['foo', 'bar']); // => true
containsAny([null], [null, 'foo']); // => true
containsAny([1], ['foo']); // => false
containsAny([], ['foo']); // => false
containsAny(['foo'], []); // => false
Parameters:
Name | Type | Description |
---|---|---|
needle |
Array | list to check with |
haystack |
Array | list to check against |
Returns:
True if any are found, false otherwise
- Type
- Boolean
(static) count(toCount) → {Object}
[a] -> { a: Number }
- Source:
- See:
-
- `R.countBy`
`countBy` the raw values in an array (`identity`). All values are
coerced to strings.
Example
count([1, 1, 2]); // { '1': 2, '2': 1 }
count(['a', 'b', 'a']); // { a: 2, b: 1 }
Parameters:
Name | Type | Description |
---|---|---|
toCount |
Array | list of elements to count |
Returns:
Where keys are the elements of `toCount` and the values are the number of occurrences
- Type
- Object
(static) ensureArray(data) → {Array}
Any a -> [a]
- Source:
Takes a value of any type and returns an array. If the value is array-like
(e.g. NodeList, arguments) it will be converted to an array; otherwise, it
will be wrapped in a new array.
Example
ensureArray('foo'); // => ['foo']
ensureArray(['foo']); // => ['foo']
ensureArray(null); // => [null]
Parameters:
Name | Type | Description |
---|---|---|
data |
Any | Any value to ensure |
Returns:
An array with `data` unless already an array
- Type
- Array
(static) forEachSerialP(func, iterable) → {Promise}
Function -> Iterable -> Promise
- Source:
A promise for each helper (serial). Given an asynchronous function
and an iterable or an object, this will evaluate the function against
each value of the iterable, serially.
Parameters:
Name | Type | Description |
---|---|---|
func |
function | An async function |
iterable |
Iterable | Object or iterable |
Returns:
A promise that will resolve to undefined when iteration is done
- Type
- Promise
(static) mapIndexed(xf, data) → {Array|Object}
((Any a, Number) -> b) -> Array< a > -> Array< b >
- Source:
- See:
-
- `R.map`, `R.addIndex`, `R.mapObjIndexed`
`R.map` with the index added.
Example
map(
(value, index) => `${value}-{index}`,
['a', 'b']
);
// ['a-0', 'b-1']
Parameters:
Name | Type | Description |
---|---|---|
xf |
function | tranformation function, arity 2 |
data |
Array | Object | Data source to iterate over |
Returns:
Transformed values inside the `data` original structure
- Type
- Array | Object
(static) mapP(xf, data) → {Array|Object}
(a -> Promise< b >) -> Array|Object c< a > -> Promise< c< b > >;
- Source:
Just like a normal `map` function, but handles an asynchronous iteration function
Example
mapP((x) => Promise.resolve(x + 1), [1, 2, 3]).then(identity); // => [2, 3, 4]
mapP(
async (x) => {
await sleep(50);
return toUpper(x);
},
{ foo: 'foo', bar: 'bar', baz: 'baz' }
).then(identity);
// { foo: 'FOO', bar: 'BAR', baz: 'BAZ' }
Parameters:
Name | Type | Description |
---|---|---|
xf |
function | Asynchronous tranformation function |
data |
Array | Object | Data source to iterate over |
Returns:
Transformed values inside the `data` original structure
- Type
- Array | Object
(static) mapParallelLimitP(maxParallel, func, iterable) → {Promise}
Number -> Function -> Iterable -> Promise
- Source:
A promise map helper, that limits the number of items being evaluated in parallel.
Given an asynchronous function and an iterable or an object, this will evaluate the
function against each value of the iterable, in parallel, limited by the maxParallel argument.
If the given iterable is neither iterable nor an object, it is just returned.
Note: An iterable will always return a promise for an array. An object will return a promise
for an object.
Parameters:
Name | Type | Description |
---|---|---|
maxParallel |
Number | Maximum number of items to be evaluating at once |
func |
function | An async function |
iterable |
Iterable | Object or iterable |
Returns:
A promise that will resolve to either an array or object when iteration is done
- Type
- Promise
(static) mapSerialP(func, iterable) → {Promise}
Function -> Iterable -> Promise
- Source:
A promise map helper, runs an async function on each item one after the other.
Given an asynchronous function and an iterable or an object, this will evaluate the
function against each value of the iterable.
If the given iterable is neither iterable nor an object, it is just returned.
Note: An iterable will always return a promise for an array. An object will return a promise
for an object.
Parameters:
Name | Type | Description |
---|---|---|
func |
function | An async function |
iterable |
Iterable | Object or iterable |
Returns:
A promise that will resolve to either an array or object when iteration is done
- Type
- Promise
(static) timeoutP(ms, promise) → {Any|Error}
Number -> Promise< * > -> Promise< * >
- Source:
A promise timeout helper. If the given promise does not resolve within the given
number of milliseconds, the promise is rejected. Similar to [this](http://bluebirdjs.com/docs/api/timeout.html)
or [this](https://github.com/kriskowal/q/wiki/API-Reference#promisetimeoutms-message).
NOTE: this does NOT cancel the original promise.
Example
timeoutP(1000, Promise.resolve('hi')).then(identity); // => 'hi'
timeoutP(1000, async (foo) => {
await sleep(1200);
return foo;
}).catch(identity); // => Error<{ code: ETIMEDOUT, name: 'Error', message: '...' }>
Parameters:
Name | Type | Description |
---|---|---|
ms |
Number | Number of milliseconds before timing out the promise |
promise |
Promise | An in-flight promise |
Returns:
The return value of the promise or rejection
- Type
- Any | Error