Home Reference Source

Function

Static Public Summary
public

all(array: Array, predicate: Function): boolean

Passes each element of the array to the given function, and checks if all of the items make the given function return a truthy value, or if all of the items are truthy values.

public

any(array: Array, predicate: Function): boolean

Passes each element of the array to the given function, and checks if any of the items makes the given function return a truthy value, or if any of the items is a truthy value.

public

append(array: Array, args: ...*): Array

Appends the given object(s) on to the end of the given array.

public

assoc(array: Array, obj: *): Array | null

Searches through an array whose elements are also arrays comparing obj with the first element of each contained array.

public

at(array: Array, index: number): *

Returns the element at the given index.

public

bsearch(array: Array, callback: Function): *

By using binary search, finds an a value from this array which meets the given condition in O(log n) where n is the length of the array.

public

bsearchIndex(array: Array, callback: Function): number

By using binary search, finds an index of a value from this array which meets the given condition in O(log n) where n is the length of the array.

public

clear(array: Array): Array

Removes all elements from the array.

public

collect(array: Array, callback: Function): Array

Invokes the given function once for each element of the array.

public

compact(array: Array): Array

Returns a copy of the given array, with all the null and undefined items removed.

public

concat(ary: Array, otherArrays: ...*): Array

Appends the elements of otherArrays into the given array.

public

count(array: Array, predicate: Function): Number

Returns the number of elements.

public

cycle(array: Array, callback: Function, count: Number): *

Calls the given callback for each item count times, or forever if count is not given.

public

deleteAt(array: Array, index: Number): * | null

Deletes the element at the specified index, returning the removed element, or null if the index is out of range.

public

deleteIf(array: Array, callback: Function): Array

Deletes every item of the array for which the given callback returns true.

public

difference(array: Array, otherArrays: ...*): Array

Returns a new array that is a copy of the receiver, removing any items that also appear in any of the arrays given as arguments.

public

dig(array: Array, indices: ...*): Object | null

Retrieves the value object corresponding to each index objects repeatedly.

public

drop(array: Array, count: number): Array

Drops first n elements from array and returns the rest of the elements in an array.

public

dropWhile(array: Array, predicate: Function): Array

Drops elements up to, but not including, the first element for which the predicate returns null or false and returns an array containing the remaining elements.

public

each(array: Array, callback: Function): Array

Calls the given callback once for each item in the array, passing that item to the callback.

public

eachIndex(array: Array, callback: Function): Array

Iterates the items of the array and passes their index to the callback.

public

rbjsDelete(array: Array, object: *, block: Function): *

Delets all items from the array that are equal to a given object.

Static Public

public all(array: Array, predicate: Function): boolean source

import all from 'rbjs/methods/all/index.js'

Passes each element of the array to the given function, and checks if all of the items make the given function return a truthy value, or if all of the items are truthy values.

Params:

NameTypeAttributeDescription
array Array
predicate Function
  • optional

This function will be called for each array item. If this function always returned truthy values, all returns true. If not given, defaults to item => item.

Return:

boolean

Whether all of the items in the array are truthy values, or made the given function always return truthy values.

Example:

 all([1, 'string', true], Boolean); // true
 rbjs([1, 'string', true]).all(Boolean); // true

public any(array: Array, predicate: Function): boolean source

import any from 'rbjs/methods/any/index.js'

Passes each element of the array to the given function, and checks if any of the items makes the given function return a truthy value, or if any of the items is a truthy value.

Params:

NameTypeAttributeDescription
array Array
predicate Function
  • optional

This function will be called for each array item. If this function ever returns a truthy value, any returns true. If not given, defaults to item => item.

Return:

boolean

Whether any of the items in the array is a truthy value, or made the given function return a truthy value.

Example:

 any([null, undefined, true], Boolean); // true
 rbjs([null, undefined, true]).any(Boolean); // true

public append(array: Array, args: ...*): Array source

import append from 'rbjs/methods/append/index.js'

Appends the given object(s) on to the end of the given array.

Params:

NameTypeAttributeDescription
array Array
args ...*

The items to append to the array

Return:

Array

The array with the new items appended to the end

Example:

 append([1, 2], 3); // => [1, 2, 3]
 append([1, 2], 3, 4, 5); // => [1, 2, 3, 4, 5]
 rbjs([1, 2]).append(3); // => [1, 2, 3]
 rbjs([1, 2]).append(3, 4, 5); // => [1, 2, 3, 4, 5]

public assoc(array: Array, obj: *): Array | null source

import assoc from 'rbjs/methods/assoc/index.js'

Searches through an array whose elements are also arrays comparing obj with the first element of each contained array.

Params:

NameTypeAttributeDescription
array Array
obj *

The object to be compared against the first element of each child array.

Return:

Array | null

Returns the first contained array whose first element matches the given obj, or null if no match is found.

Example:

 assoc([
     ['colors', 'red', 'blue', 'green'],
     ['letters', 'a', 'b', 'c'],
     'foo'
 ], 'letters');
 // => ['letters', 'a', 'b', 'c']
 rbjs([
     ['colors', 'red', 'blue', 'green'],
     ['letters', 'a', 'b', 'c'],
     'foo'
 ]).assoc('letters');
 // => ['letters', 'a', 'b', 'c']

public at(array: Array, index: number): * source

import at from 'rbjs/methods/at/index.js'

Returns the element at the given index. A negative index counts from the end of the array. Returns null if the index is out of range.

Params:

NameTypeAttributeDescription
array Array
index number

The index of the item to get from the array. A negative value counts from the end of the array.

Return:

*

The array item at the given index.

Example:

 at([1, 2, 3], 1); // 2
 at([1, 2, 3], -1); // 3
 rbjs([1, 2, 3]).at(1); // 2
 rbjs([1, 2, 3]).at(-1); // 3

public bsearch(array: Array, callback: Function): * source

import bsearch from 'rbjs/methods/bsearch/index.js'

By using binary search, finds an a value from this array which meets the given condition in O(log n) where n is the length of the array.

You can use this method in two modes: a find-minimum mode and a find-any mode. In either case, the elements of the array must be monotone (or sorted) with respect to the given callback.

In find-minimum mode (this is a good choice for typical use cases), the callback must always return true or false, and there must be an index i (0 <= i <= array.length) so that:

  • the callback returns false for any element whose index is less than i, and
  • the callback returns true for any element whose index is greater than or equal to i.

    This method returns the i-th element. If i is equal to array.length, it returns null.

    In find-any mode, the callback must always return a number, and there must be two indices i and j (0 <= i <= j <= array.length) so that:

  • the callback returns a positive number if 0 <= k < i,
  • the callback returns zero if i <= k < j, and
  • the callback returns a negative number if j <= k < array.length.

    Under this condition, this method returns any element whose index is within i...j. If i is equal to j (i.e., there is no element that satisfies the callback), this method returns null.

    You must not mix the two modes at a time; the block must always return either true/false, or always return a number. It is undefined which value is actually picked up at each iteration.

Params:

NameTypeAttributeDescription
array Array
callback Function

This should return a boolean for find-minimum mode, and a number for find-any mode.

Return:

*

An array item that satisfies the given callback.

Example:

 bsearch([0, 4, 7, 10, 12], x => x >= 4); // => 4
 bsearch([1, 2, 3, 4, 5], x => 2 - x); // => 2
 rbjs([0, 4, 7, 10, 12]).bsearch(x => x >= 4); // => 4
 rbjs([1, 2, 3, 4, 5]).bsearch(x => 2 - x); // => 2

public bsearchIndex(array: Array, callback: Function): number source

import bsearchIndex from 'rbjs/methods/bsearchIndex/index.js'

By using binary search, finds an index of a value from this array which meets the given condition in O(log n) where n is the length of the array.

You can use this method in two modes: a find-minimum mode and a find-any mode. In either case, the elements of the array must be monotone (or sorted) with respect to the given callback.

In find-minimum mode (this is a good choice for typical use cases), the callback must always return true or false, and there must be an index i (0 <= i <= array.length) so that:

  • the callback returns false for any element whose index is less than i, and
  • the callback returns true for any element whose index is greater than or equal to i.

    This method returns the index i. If i is equal to array.length, it returns null.

    In find-any mode, the callback must always return a number, and there must be two indices i and j (0 <= i <= j <= array.length) so that:

  • the callback returns a positive number if 0 <= k < i,
  • the callback returns zero if i <= k < j, and
  • the callback returns a negative number if j <= k < array.length.

    Under this condition, this method returns any index within i...j. If i is equal to j (i.e., there is no element that satisfies the callback), this method returns null.

    You must not mix the two modes at a time; the block must always return either true/false, or always return a number. It is undefined which value is actually picked up at each iteration.

Params:

NameTypeAttributeDescription
array Array
callback Function

This should return a boolean for find-minimum mode, and a number for find-any mode.

Return:

number

The corresponding index an element that satisfies the given callback.

Example:

 bsearchIndex([0, 4, 7, 10, 12], x => x >= 4); // => 1
 bsearchIndex([1, 2, 3, 4, 5], x => 2 - x); // => 1
 rbjs([0, 4, 7, 10, 12]).bsearchIndex(x => x >= 4); // => 1
 rbjs([1, 2, 3, 4, 5]).bsearchIndex(x => 2 - x); // => 1

public clear(array: Array): Array source

import clear from 'rbjs/methods/clear/index.js'

Removes all elements from the array.

Params:

NameTypeAttributeDescription
array Array

Return:

Array

An empty array.

Example:

 clear([1, 2, 3]); // => []
 rbjs([1, 2, 3]).clear(); // => []

public collect(array: Array, callback: Function): Array source

import collect from 'rbjs/methods/collect/index.js'

Invokes the given function once for each element of the array. Returns an array containing the values returned by the given function.

If the callback function is not given, this returns the original array.

Params:

NameTypeAttributeDescription
array Array
callback Function
  • optional

Will be called for each item of the array. The returned value of this function will be the corresponding item in the resulting array.

Return:

Array

A new array containing the returned values of the given function, or the original array of the callback function is not given.

Example:

 collect([1, 2, 3], x => x * 2); // => [2, 4, 6]
 collect(['a', 'b', 'c'], x => `${x}!`); // => ['a!', 'b!', 'c!']
 rbjs([1, 2, 3]).collect(x => x * 2); // => [2, 4, 6]
 rbjs(['a', 'b', 'c']).collect(x => `${x}!`); // => ['a!', 'b!', 'c!']

public compact(array: Array): Array source

import compact from 'rbjs/methods/compact/index.js'

Returns a copy of the given array, with all the null and undefined items removed.

Params:

NameTypeAttributeDescription
array Array

Return:

Array

A copy of the given array without the null and undefined items.

Example:

 compact([1, 2, undefined, 3, null, 4, 5]); // [1, 2, 3, 4, 5]
 rbjs([1, 2, undefined, 3, null, 4, 5]).compact(); // [1, 2, 3, 4, 5]

public concat(ary: Array, otherArrays: ...*): Array source

import concat from 'rbjs/methods/concat/index.js'

Appends the elements of otherArrays into the given array.

Params:

NameTypeAttributeDescription
ary Array
otherArrays ...*

Return:

Array

Returns the array itself.

Example:

concat([1, 2, 3], [4, 5, 6]); // => [1, 2, 3, 4, 5, 6]
concat(['a', 'b'], ['c'], ['d', 'e']) // => ['a', 'b', 'c', 'd', 'e']
rbjs([1, 2]).concat([3, 4]); // => [1, 2, 3, 4]
rbjs(['a', 'b']).concat(['c', 'd'], ['e'])  // => ['a', 'b', 'c', 'd', 'e']

public count(array: Array, predicate: Function): Number source

import count from 'rbjs/methods/count/index.js'

Returns the number of elements.

If an argument is given, counts the number of elements for which the object or predicate holds true

Params:

NameTypeAttributeDescription
array Array
predicate Function

Return:

Number

Returns the number of elements

Example:

count(['a', 'b', 'c'])  // => 3
count(['a', 'b', 'c'], e => e === 'b')  // => 1
rbjs([1, 2, 3, 4]).count() // => 4
rbjs([1, 2, 3, 4]).count(e => e % 2 === 0) // => 2

public cycle(array: Array, callback: Function, count: Number): * source

import cycle from 'rbjs/methods/cycle/index.js'

Calls the given callback for each item count times, or forever if count is not given.

Does nothing if a non-positive number is given or the array is empty.

Returns undefined if the loop has finished without getting interrupted.

Params:

NameTypeAttributeDescription
array Array
callback Function

Will be called with an item of the given array.

count Number
  • optional

The number of times to cycle through the items of the array.

Return:

*

Example:

 const callback = item => console.log(item);
 cycle([1, 2, 3], callback); // prints: 1, 2, 3, 1, 2, 3,... forever
 cycle([1, 2, 3], callback, 2); // prints: 1, 2, 3, 1, 2, 3
 const callback = item => console.log(item);
 rbjs([1, 2, 3]).cycle(callback); // prints: 1, 2, 3, 1, 2, 3,... forever
 rbjs([1, 2, 3]).cycle(callback, 2); // prints: 1, 2, 3, 1, 2, 3

public deleteAt(array: Array, index: Number): * | null source

import deleteAt from 'rbjs/methods/deleteAt/index.js'

Deletes the element at the specified index, returning the removed element, or null if the index is out of range.

Params:

NameTypeAttributeDescription
array Array
index Number

The index to remove an element from. Can be a negative value, in which case will start from the end of the array.

Return:

* | null

The removed element, or null if the index is out of bounds.

Example:

 deleteAt([1, 2, 3], 1); // => 2
 deleteAt([1, 2, 3], -5); // => null
 rbjs([1, 2, 3]).deleteAt(1); // => 2
 rbjs([1, 2, 3]).deleteAt(-5); // => null

public deleteIf(array: Array, callback: Function): Array source

import deleteIf from 'rbjs/methods/deleteIf/index.js'

Deletes every item of the array for which the given callback returns true.

Params:

NameTypeAttributeDescription
array Array
callback Function
  • optional

A function that returns true if the passed item should be removed from the array.

Return:

Array

An array with the desired items removed, or the given array if callback is not given.

Example:

 deleteIf([1, 2, 3, 4, 5], x => x < 4); // => [4, 5]
 rbjs([1, 2, 3, 4, 5]).deleteIf(x => x < 4); // => [4, 5]

public difference(array: Array, otherArrays: ...*): Array source

import difference from 'rbjs/methods/difference/index.js'

Returns a new array that is a copy of the receiver, removing any items that also appear in any of the arrays given as arguments. The order is preserved from the original array.

Params:

NameTypeAttributeDescription
array Array
otherArrays ...*

Return:

Array

Returns the diff array

Example:

difference([1, 1, 2, 2, 3, 3, 4, 5], [1, 2, 4]); // => [ 3, 3, 5 ]
difference([1, 'c', 'yep'], [1], [ 'a', 'c' ]);  // => ["yep"]
rbjs([1, 'c', 'yep']).difference([1], ['a', 'c']) // => ["yep"]

public dig(array: Array, indices: ...*): Object | null source

import dig from 'rbjs/methods/dig/index.js'

Retrieves the value object corresponding to each index objects repeatedly.

Params:

NameTypeAttributeDescription
array Array
indices ...*

Return:

Object | null

Example:

dig([[1, [2, 3]]], 0, 1, 1); // => 2
rbjs([[1, [2, 3]]]).dig(0, 1, 1); // => 2

public drop(array: Array, count: number): Array source

import drop from 'rbjs/methods/drop/index.js'

Drops first n elements from array and returns the rest of the elements in an array.

If a negative number is given, raises a TypeError: Invalid arguments.

Params:

NameTypeAttributeDescription
array Array
count number

First n elements to remove from array

Return:

Array

The rest of the elements in the array

Example:

drop([1, 2, 3, 4, 5], 2);  // => [3, 4, 5]
drop([1, 2], 3); // => []
rbjs([1, 2, 3, 4, 5]).drop(2); // => [3, 4, 5]
rbjs([1, 2]).drop(3); // => []

public dropWhile(array: Array, predicate: Function): Array source

import dropWhile from 'rbjs/methods/dropWhile/index.js'

Drops elements up to, but not including, the first element for which the predicate returns null or false and returns an array containing the remaining elements.

If no predicate is given, the array is returned instead.

Params:

NameTypeAttributeDescription
array Array
predicate Function

Return:

Array

array containing the remaining elements

Example:

dropWhile([1, 2, 3, 4, 5, 0], x => x < 3); // => [3, 4, 5, 0]
rbjs([1, 2, 3, false, 5].dropWhile(x => x < 3);  // => [false, 5]

public each(array: Array, callback: Function): Array source

import each from 'rbjs/methods/each/index.js'

Calls the given callback once for each item in the array, passing that item to the callback.

Params:

NameTypeAttributeDescription
array Array
callback Function

Called and passed each item of the array. If this callback expects multiple elements and the array item is another array, each item of that array is passed as individual arguments to the this callback.

Return:

Array

The given array.

Example:

 each([1, 2, 3], callback); // calls `callback` with 1, 2, then 3
 rbjs([1, 2, 3]).each(callback); // calls `callback` with 1, 2, then 3

public eachIndex(array: Array, callback: Function): Array source

import eachIndex from 'rbjs/methods/eachIndex/index.js'

Iterates the items of the array and passes their index to the callback.

Params:

NameTypeAttributeDescription
array Array
callback Function

The function that will be passed the indeces of each array item.

Return:

Array

The original given array.

Example:

 eachIndex(['a', 'b', 'c'], i => console.log(i)); // prints: 0 1 2
 rbjs(['a', 'b', 'c']).eachIndex(i => console.log(i)); // prints: 0 1 2

public rbjsDelete(array: Array, object: *, block: Function): * source

import rbjsDelete from 'rbjs/methods/delete/index.js'

Delets all items from the array that are equal to a given object.

Note that since delete is a reserved keyword in JavaScript, you must use a different identifier when importing this method, e.g. rbjsDelete.

Params:

NameTypeAttributeDescription
array Array
object *

The item to be removed from the array

block Function
  • optional

A function whose return value is returned if the item to be removed is not found.

Return:

*

Returns the last deleted item, or null if no matching item is found.

Example:

 rbjsDelete([1, 2, 2, 3], 2); // => [1, 3]
 rbjsDelete([1, 2, 2, 3], 4, () => 'hello'); // => 'hello'
 rbjs([1, 2, 2, 3]).delete(2); // => [1, 3]
 rbjs([1, 2, 2, 3]).delete(4, () => 'hello'); // => 'hello'