all

Test if all elements of array satisfy function

(fn: Function) => (source: Array): boolean
Parameters
fn (Function) Function that all elements need to satisfy
source (Array) Input array
Returns
boolean: True if all objects satisfy, false otherwise
Example
all(isNumber)([1, 2, 3])
// => true

all(is)([1, "asd", null])
// => false

allBy

Test if object properties match all objects in input array

(subset: Object) => (source: Object[]): boolean
Parameters
subset (Object) Set of properties that should match
source (Array) Input array
Returns
boolean: True if all objects match, false otherwise
Example
all({
  id: isString
})([
  { id: "uuid1", name: "foo"}
  { id: "uuid2", name: "bar"}
])
// => true

any

Test if at least one element of array satisfies function

(fn: Function) => (source: Array): boolean
Parameters
fn (Function) Function to be satisfied
source (Array) Input array
Returns
boolean: True if at least one object passes, false otherwise
Example
any(isNumber)([1, "string", NaN])
// => true

any(is)([null])
// => false

anyBy

Test if object properties match any object in input array

(subset: Object) => (source: Object[]): boolean
Parameters
subset (Object) Set of properties that should match
source (Array<Object>) Input array
Returns
boolean: True if at least one object matches, false otherwise
Example
anyBy({
  id: isNumber,
  name: "lorem",
})([
  { id: "uuid", name: "lorem" },
  { id: 2, name: "foo" },
  { id: 3, name: "lorem", foo: "bar" },
])
// => true

cases

Functional case statement.

<A, B>( conditions: [(A) => boolean, (A) => B][], otherwise: (A) => B ) => A => B
Parameters
conditions (any) List of 2-tuples of functions (if, then)
otherwise (any) Function to call if no condition matches. Defaults to identity.
input (any) Value to check
Returns
any: The result of calling the first matching then function or the otherwise function on the input.
Example
cases([
 [x === 0, x => x * 2],
 [x === 1, x => x],
], x => x + 1)(2) = 3

concat

Merge two arrays/values into one array

(source1: Array|mixed) => (source2: Array|mixed): Array
Parameters
source1 ((Array | mixed)) First input source
source2 ((Array | mixed)) Second input source
Returns
Array: Array with concatenated values
Example
concat([1])([4, 5])
// => [1, 4, 5]

concat(1)(4)
// => [1, 4]

clone

Creates a new instance of the object with same properties than original. Will not inherit prototype, only own enumerable properties.

<T>(source: T): T
Parameters
source (any) Input value
Returns
any: Cloned value
Example
let x = {a: [1]}

clone(x)
// => {a: [1]}

close(x) === x
// => false

contains

Test if string contains substring

(search: string) => (source: string): boolean
Parameters
search (string) Search string
source (string) Source string
Returns
boolean:
Example
contains("ipsum")("lorem ipsum")
// => true

count

Count items in array that satisfies a function

(fn: Function) => (source: Array): number
Parameters
fn (Function) Test function
source (Array) Source array
Returns
number: Number of items that satisfied fn
Example
count([1, 2, 3])
// => 3

count(
 item => item.score === 10
)([
  { name: "Bob", score: 1, subject: "CS" },
  { name: "Alice", score: 10, subject: "Math" },
  { name: "Hatter", score: 10, subject: "Math"}
])
// => 2

countBy

Count objects that match

(subset: Object) => (source: Object[]): number
Parameters
subset (Object) Set of properties that should match
source (Array) Input array
Returns
number: Number of objects that match subset
Example
countBy({
 subject: value => value === "Math"
 score: value => value > 5
})([
  { name: "Bob", score: 1, subject: "CS" },
  { name: "Alice", score: 10, subject: "Math" },
  { name: "Hatter", score: 10, subject: "Math"}
])
// => 2

curry

Partially apply a function.

<A>(fn: (...args: mixed[]) => A) => CurryReturnType<A> where CurryReturnType<A> = (...mixed[]) => A | CurryReturnType<A>
Parameters
fn (any) The function to apply
args (any) The arguments to apply, in order
Returns
any: If the number of arguments provided is sufficient to call the function, call the function and return the result. Otherwise, return a new function which takes additional parameters, returning the result of calling curry on the function with the provided parameters.
Example
curry((a, b) => a + b)(1)(2) = 3

uncurry

Convert a curried function of arity n to a simple function with n parameters.

uncurry
Parameters
fn (any) The function to apply
args (any) The arguments to apply, in order
Returns
any: The result of calling the function on each argument in turn, until a non-function is the return value. If an insufficient number of arguments are provided, return the partially applied function.
Example
uncurry(a => b => c => a + b * c)(1, 2, 3) = 7

debounce

Call function after wait milliseconds have elapsed

(fn: Function, { wait: number, bind: Object }): Function
Parameters
fn (Function) Source function
props (Object?) Properties
Name Description
props.wait number (default 50) Time in milliseconds to wait without calling until invoke
props.bind Object (default null) this provided for the call to fn
Returns
Function: Wrapper function that calls fn after wait passed without calling
Example
// constructor
this.debouncedAutocomplete = debounce(autocompleteFromAPI, {
  wait: 100,
  bind: this
})

// render
<input onChange={this.debouncedAutocomplete} ... />

Determine a variable's type. Looks only for Array, Object and RegExp. Everything else are "Primitive"

Not using ../type/type.js added ~300k ops/sec

Parameters
source (mixed) The source
Returns
string:

Array

Determine if 2 arrays are equal. Order counts: [1, 2] !== [2, 1]

Array(a: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>, b: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a>
Parameters
a (Array) Source input
b (Array) Other source input
Returns
boolean:

Object

Determine if 2 objects are equal

Object(a: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>, b: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a>
Parameters
a (Object) Source input
b (Object) Other source input
Returns
boolean:

RegExp

Determine if 2 regular expressions are equal

RegExp(a: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp">RegExp</a>, b: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp">RegExp</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a>
Parameters
a (RegExp) Source input
b (RegExp) Other source input
Returns
boolean:

Primitive

Shallow equal

Primitive(a: mixed, b: mixed): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a>
Parameters
a (mixed) Source input
b (mixed) Other source input
Returns
boolean:

deepEqual

Determine if two variables are structurally equal (callable curried or uncurried)

(a: any, b: any): boolean
Parameters
a (any) Source input
b (any) Other source input
Returns
boolean: True if inputs are deeply equal, false otherwise
Example
deepEqual(
  { b: 3, a: 2 },
  { a: 2, b: 3 }
)
// => true

deepEqual(
  { a :[1, 2] }
)(
  { a: [2, 1] }
)
// => false

distinct

Remove duplicate values. Will use deepEqual for comparison.

(source: Array): Array
Parameters
source (Array) Input array
Returns
Array: Duplicate free array
Example
distinct([1, 1, 2])
// => [1, 2]

elapsedTime

Calculate elapsed time between to dates. In days, hours, minutes and seconds (callable curried or uncurried).

(startDate: Object, endDate: Object): Object
Parameters
startDate (Date) Start date
endDate (Date) End date
Returns
Object:
Example
elapsedTime(
  new Date("June 1, 2018 00:00:00")
)(
  new Date("June 1, 2018 03:24:00")
)
// => { days: 0, hours: 3, minutes: 24, seconds: 0 }

endsWith

Test if string ends with substring (callable curried or uncurried).

(search: string, source: string): boolean
Parameters
search (string) Search string
source (string) Input string
Returns
boolean: True if source ends with search , false otherwise
Example
endWith("ipsum")("lorem ipsum")
// => true

entries

Return an array of key-value pairs corresponding to the key-value pairs of an Object.

entries
Parameters
object (any) (implicit) The object to return key-value pairs for
Returns
any: Key-value pairs (2-tuples)
Example
entries({ id: 1, name: "test" }) = [["id", 1], ["name", "test"]]

fromEntries

Given an array of key-value tuples, construct an Object.

fromEntries
Parameters
entries (any) (implicit) The key-value pairs to create the object from
Returns
any: An object with the given key-value pairs
Example
fromEntries([["id", 1], ["name", "test"]]) = {
  id: 1,
  name: "test"
}

escapeRegExp

Make safe for RegExp'ing.

(source: string): string
Parameters
source (string) Input string
Returns
string:
Example
escapeRegExp( "lorem. ipsum [dolor]" )
// => "lorem \\. ipsum \\[dolor\\]"

escapeHTML

Escape HTML special chars for safe rendering.

(source: string): string
Parameters
source (string) Input string
Returns
string:
Example
escapeHTML("<script>alert('HERE'S BOBBY')</script>")
// => "&lt;script&gt;alert(&#39;HERE&#39;S BOBBY&#39;)&lt;&#47;script&gt;"

filter

Filter items in array that satisfies test function

(fn: Function) => (source: Array): Array
Parameters
fn (Function) Filter test function
source (Array) Input array
Returns
Array:
Example
filter(is)([1, 2, null, 3, undefined])
// => [1, 2, 3]

filterBy

Filter items in array that match subset

(subset: Object) => (source: Object[]): Object[]
Parameters
subset (Object) Set of properties that should match
source (Array<Object>) Input array
Returns
Array<Object>:
Example
filterBy({
  id: is,
})([
  { id: 1 },
  { id: null, name: "test" },
  { id: 3, foo: "bar" },
  { name: "test" },
])
// => [{ id: 1 }, { id: 3, foo: "bar" }]

findIndexBy

Find the position of first element that matches the subset criteria

(subset: Object) => (source: Object[]): number
Parameters
subset (Object) Object containing one or more fields
source (Array<Object>) Source input
Returns
number:
Example
const comments = [{id: 1, body: ""}, {id: 2, body: "dolor"}]

findIndexBy({id: 2})(comments)
// => 1

findIndexBy({id: 3})(comments)
// => -1

find

Find the first element that satisfies test function.

(fn: Function) => (source: Array): mixed
Parameters
fn (Function) Function applied to each element
source (Array) Input array
Returns
(mixed | undefined): First item that satisfied fn
Example
find(x => x % 2 === 0)([1, 2, 3, 4, 5])
// => 2

findBy

Find the first element that satisfies test function.

(subset: Object) => (source: Object[]): mixed
Parameters
subset (Object) Set of properties that should match
source (Array<Object>) Input array
Returns
(mixed | undefined): First item that satisfied fn
Example
findBy({
  id: 2
})([
  { id: 1, foo: "bar" },
  { id: 2, foo: "ipsum" }
])
// => { id: 2, foo: "ipsum" }

flattenOne

Flatten a single level of an array.

flattenOne
Parameters
source (any)

flatten

Recursively concat all arrays intro a single array

flatten
Parameters
input (Array) Array with nested arrays
Returns
Array: 1 level deep array
Example
flatten([1, [2], [3, [4]]])
// => [1, 2, 3, 4]

flattenBranches

An alternate version of flatten that treats nested arrays as branches, constructing a set of arrays which represent the permutations of concatenations possible when taking a single value from each branch.

Unlike flatten, flattenBranches does not recurse on subelements.

WARNING: As a permutation function, it's subject to combinatorial explosion.

flattenBranches
Example
flattenBranches([1, 2, [3, 4]]) =
  [[1, 2, 3], [1, 2, 4]]

flattenBranches([1, [2, 3], [4, 5]]) =
  [[1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5]]

flip

Reverse the first two parameters of a function.

flip
Parameters
f (any)

flipUncurried

Reverse the first two parameters of an uncurried function.

flipUncurried
Parameters
f (any)

forEach

Call fn over each element of an array

(fn: Function) => (source: Array): undefined
Parameters
fn (Function) Function
source (Array) Source array
Returns
undefined:

prop

Get value from obj property

(key: string) => (source: Object): mixed
Parameters
key (string) Property name
source (object) Source object
Returns
mixed:
Example
prop("lorem")({ lorem: "ipsum" }) // => "ipsum"
prop("not-exist")({ lorem: "ipsum" }) // => undefined

groupBy

Group values from a list into a list of lists.

<A>(grouper: ((x: A) => (y: A) => boolean) | string): (input: A[]) => A[][]
Parameters
grouper (any) One of:
  • A curried function taking two As and returning a boolean.
  • A string, representing a key of objects in the list. In this case A is the type of the value at the given key.
Returns
any: A function taking a A[] , and returning a A[][] . Alternatively, a function taking a Object[] where the value at grouper is an A (if grouper is a string), and returning a Object[][] .

If grouper was passed as a string, each sublist of the result will contain objects where the value at key grouper is equal (===).

Otherwise, each sublist of the result will contain As which return true when passed pairwise to grouper.

Example
groupBy(x => y => x % 2 === y % 2)([1,2,3,4,5]) = [[1,3,5], [2,4]]

groupBy('id')([{ id: 1 }, { id: 2 }, { id: 1 }]) = [[{ id: 1 }, { id: 1 }], [{ id: 2 }]]

gt

Grater compare

gt
Parameters
first (number) First number
second (number) Second number
Returns
boolean:
Example
gt(4)(10)
// => false
gt(14)(10)
// => true

has

Check if value is in array

(value: Function|mixed )( source: Array ): boolean
Parameters
arg (mixed) What to search for
source (Array) Haystack
Returns
boolean: True if has, false otherwise
Example
has( 2 )( [ 1, 2 ] )
// => true
has( 3 )( [ 1, 2 ] )
// => false
has( elm => elm.id === 1 )([{}, {id: 1}])
// => true

hist

Determine the count of all field's distinct values in a list of objects (aka histogram)

(field: string)(source: Object[]): Object
Parameters
field (string) Field name to count
source (Array<Object>) Array of objects
Returns
Object:
Example
const scores = [{
 name   : "Bob",
 score  : 1,
 subject: "Math"
}, {
 name   : "Alice",
 score  : 10,
 subject: "Math"
}, {
 name   : "Hatter",
 score  : 10,
 subject: "Math"
}]

hist( "score" )( scores )
// => { "1": 1, "10": 2 }

i

Identity function

i
Parameters
input (mixed) The input
Returns
mixed:

ifThen

Functional if-then-else

ifThen
Parameters
conditionFn (Function) Condition function
thenFn (Function) Then function
elseFn (Function) Else function, defaults to identity
input (mixed) Input
Returns
mixed:

indexBy

Index an array of objects by field. Only truthy fields will be indexed.

(field: string) => (source: Object[]): Object
Parameters
key (any)
startingObj (any = {})
field (string) The field to index by
array (Array) Input
Returns
Object:
Example
indexBy("id")([
  {id: 1, user_id: 2},
  {id: 2, user_id: 3},
])
// => {
//   1: [{id: 1, user_id: 2}],
//   2: [{id: 2, user_id: 3}],
// }

is

Test if a variable holds something

is(source): boolean
Parameters
source (any) Source variable
Returns
boolean:
Example
is(null)      // => false
is(0)         // => true
is(undefined) // => false
is("")        // => true
is(false)     // => true
is(NaN)       // => false

isEmpty

Determines something is empty

isEmpty
Parameters
input (Any) Something to check if empty
Returns
boolean: True if empty, False otherwise
Example
isEmpty({})               // true
isEmpty(1)                // false
isEmpty(false)            // false
isEmpty("")               // true
isEmpty(null)             // true
isEmpty(undefined)        // true
isEmpty([])               // true
isEmpty(NaN)              // true
isEmpty(/[A-z]/)          // false
isEmpty(new Date())       // false
isEmpty(() => {})         // false
isEmpty(Promise.resolve() // false

isMatch

Determines if one object's properties are equal to another

(subset: Object)(source: Object): boolean
Parameters
subset (Object) Set of properties that should match
source (Object) Object matching against
Returns
boolean: True if all "subset" properties are of equal (shallow compare) value to properties in "source" object, otherwise false
Example
isMatch({
 id: 2,
 parentId: null,
})({
 id: 2,
 parentId: null
 name: "John",
})
// true

isMatch({
 "!parentId": null,
 "name": "John",
})({
 id: 2,
 parentId: null,
 name: "John",
})
// false

isNot

Test if a variable holds nothing

isNot(source): boolean
Parameters
source (any) Source variable
Returns
boolean:
Example
isNot(null)      // => true
isNot(NaN)       // => true
isNot(undefined) // => true

isNot(0)         // => false
isNot("")        // => false
isNot(false)     // => false

join

Join all elements of an array into a string

(separator: string)(source: Array): string
Parameters
separator (string) Separator between each adjacent elements
source (string) Source string
Returns
string:
Example
join( "," )( [ "lorem", "ipsum" ] )
// => "lorem,ipsum"

last

Return last element in array

T[] => T
Parameters
source (Array<T>) Source array
Returns
T: Last element or undefined if empty source
Example
last([1, 2, 3])
// 3

last([])
// undefined

lens

Create a pair of lenses - functions for retrieving or updating the value at an object key.

The lens functions will throw errors if they are passed objects which are invalid for the lens.

lens
Parameters
keys (...any) The path to the value to make lenses for. This must be flat.
Returns
any: A 2-tuple of lenses - the getter and the setter
Example
lens("a", "b", "id")({ a: { b: { id: 1 } } }) = [get, set]

where get(obj)    => 1
      set(3)(obj) => { a: { b: { id: 3 } } }

_lenses

A version of lens that allows nesting, allowing for the creation of multiple lenses simultaneously. Be careful with this one, it's subject to combinatorial explosion.

_lenses
Parameters
keys (...any)
Example
_lenses("a", "b", ["id", "name"]) = [
  lens("a", "b", "id")
  lens("a", "b", "name")
]

_lenses("a", ["b", "c"], ["id", "name"]) = [
  lens("a", "b", "id")
  lens("a", "b", "name")
  lens("a", "c", "id")
  lens("a", "c", "name")
]

lenses

Make several lenses at once, which may be completely unrelated to one another.

Imagine we have an object:

{ a: { b: { id: 1, name: "Test" }, c: { banana: 1, message: "Error" } } }

lenses
Parameters
paths (...any)
Example
lenses(
  ["a", "b", ["id", "name"]],
  ["a", "c", "banana"]
) = [
  [[getId, setId], [getName, setName]],
  [getBanana, setBanana]
]

lt

Less compare

lt
Parameters
first (number) First number
second (number) Second number
Returns
boolean:
Example
lt(4)(10)
// => true
lt(14)(10)
// => false

map

Iterate over an input list, calling fn for each element, return a new array

map
Parameters
fn (Function) The function
list (Array) Array
Returns
Array:

max

Find the maximum value in a source array

( source: Number[] ): Number
Parameters
arg1 ((Array | Function)) Custom transform function or source array
source (Array<number>) Array of numbers
Returns
number:
Example
max([-1, 1, 10, 3])
// => 10

const fn = element => ( new Date( element.time ) )
const source = [
  { time: "2018-05-15T11:20:07.754110Z" },
  { time: "2018-06-11T09:01:54.337344Z" },
  { time: "2018-06-08T08:26:12.711071Z" },
]
max(fn)(source)
// => {time: "2018-06-11T09:01:54.337344Z"}

mergeTwoWith

Merge two objects, provided a function to decide what to do with overlapping keys.

(Object, Function) => Object => Object => Object
Parameters
fns (any = {})
defaultFn (any = defaultMergeFn) The merging function to use by default (if a specific function is not provided for a key in the fn object.) By default, the second value overwrites the first value unless the second value is undefined.
fn (any) An Object of curried functions taking two values, where the function at a key describes how to merge duplicate values at the key.
a (any) The first object
b (any) The second object
Returns
any: An object with properties merged from a and b
Example
mergeTwoWith({ isLoading: a => b => a || b })({
  isLoading: false,
  a: undefined,
  b: 2
})({
  isLoading: true,
  a: "test",
  b: undefined
}) = {
  isLoading: true,
  a: "test",
  b: 2
}

mergeTwo

Merge two objects. Keys from the second object corresponding to undefined values will be ignored, otherwise same-named keys from the second object will take priority.

Object => Object => Object
Parameters
a (any) The first object
b (any) The second object
Returns
any: An object with the props from both input objects merged together
Example
mergeTwo({ id: 1, name: "test" })(
  { id: undefined, name: "test2", error: "Error" }
) = { id: 1, name: "test2", error: "Error" }

mergeWith

Merge n objects, provided functions which describe what to do in the case of overlapping keys.

(Object, Function) => Object[] => Object
Parameters
fns (any)
defaultFn (any) The merging function to use by default
fn (any) An Object of curried functions taking two values, where the function at a key describes how to merge duplicate values at the key.
a (any) The first object
b (any) The second object
Returns
any: An object with properties merged from all source objects
Example
mergeWith({ isLoading: a => b => a || b })({
  isLoading: false,
  a: undefined,
  b: 2
}, {
  isLoading: true,
  a: "test",
  b: undefined
}, {
  isLoading: false,
  a: "test2",
  c: false
}) = {
  isLoading: true,
  a: "test2",
  b: 2,
  c: false
}

merge

Combine from left to right, 2 or more objects into a new single one. Properties will be shallow copied. Those with the same name will be overwriten by right most object.

( ...source: Object[] ): Object
Parameters
source (Array<Object>) Array of objects
Returns
Object:
Example
merge({a: "lorem"}, {b: "ipsum", c: 41}, {c: 42, b: undefined})
// => { a: "lorem", b: "ipsum", c: 42 }

min

Find the minimum value in a source array

( source: Number[] ): Number
Parameters
source (Array<number>) Array of numbers
Returns
number:
Example
min([-1, 1, 10, 3])
// => -1

partition

Split a list based on a predicate function.

<A>(fn: (x: A) => boolean) => (input: A[]) => [A[], A[]]
Parameters
fn (any) A predicate function.
Returns
any: A function taking a A[] and returning a two-tuple of A[] s. The first element of the tuple consists of elements for which the predicate returned true , the second of elements for which it returned false .
Example
partition(x => x % 2 === 0)([1,2,3,4,5]) = [[2,4],[1,3,5]]

pick

Returns a partial copy of an object containing only the key-value pairs for which the provided predicate function returns true.

pick

pickKeys

Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored.

( keys: string[] ) => ( source: Object ): Object
Parameters
keys (any) The properties to keep from the source
source (any) (implicit) The input object
Returns
any: The input object, minus any keys which
Example
pick(["id", "name"])({id: 2, name: "lorem", description: "lorem ipsum"})
// => {id: 2, name: lorem}

pipe

Performs left-to-right function composition. The leftmost function may have any arity, the remaining functions must be unary.

pipe
Parameters
fn (Array) Array of functions, call each one in order with the input of previous one's result
input (Array) Arguments array
Returns
mixed:
Example
pipe( inc, inc )( 2 )
// => 4

pluck

Returns a new list by extracting the same named property off all objects in the source list

( field: string ) => ( source: Object[] ): mixed[]
Parameters
field (string) Field name to extract values from
source (Array<Object>) Array of objects
Returns
number:
Example
pluck("position")([{id: 1, position: 3}, {id:2, position: -1}])
// => [3, -1]

push

Add element at end of array

( ...elements: mixed ) => ( input: Array ): Array
Parameters
element (mixed) Element to be added
input (Array) Array to add to
Returns
Array:
Example
push( 2 )( [ 1 ] ) // => [ 1, 2 ]
push( 2, 4 )( [ 1 ] ) // => [ 1, 2, 4 ]

reduce

Apply a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

(fn: Function, defaultAcc: mixed) => (source: Array): mixed
Parameters
fn (Function) Reduce function
defaultAcc (Object) Default value for the accumulator
source (Array) Source input
Returns
mixed:

remove

Remove value from array (strict equality check)

(value: any) => (source: Array): Array
Parameters
value (mixed) Value to remove
source (Array) Input array
Returns
Array:
Example
remove(2)([1, 2, 3])
// => [1, 3]

remove(null)([1, null, 2, 3, null])
// => [1, 2, 3]

replaceString

Replace substring in string

replaceString
Parameters
oldString (string) The old string
newString (string) The new string
Returns
string:

replaceArray

Replace element in array (shallow equal)

replaceArray
Parameters
oldElm (mixed) The old elm
newElm (mixed) The new elm
Returns
Array:

replace

Replace substring if source is string, replace element (shallow equal) if source is Array

(oldElm: string|mixed, newElm: string|mixed) => (source: Array): Array
Parameters
oldElm ((string | mixed)) To be cloned
newElm ((string | mixed)) Copy of this object.
source ((string | Array)) Source array
Returns
(string | Array):

rename

Rename object properties.

rename
Parameters
keys (any) A mapping from old names to new ones
source (any) (implicit) The object to rename keys of
Example
rename({
  test: "test2"
})({ test: 2, unrelated: false }) = { test2: 2, unrelated: false }

sequence

Create an array of numbers

(step: number) => (start: number, end: number): number[]
Parameters
step (number) The step
start (number) Left side interval
end (number) Right side interval
Returns
Array<number>:
Example
sequence(1)(1, 5)   // [1, 2, 3, 4, 5]
sequence(3)(1, 5)   // [1, 4]
sequence(-1)(2, -3) // [ 2, 1, 0, -1, -2, -3 ]

set

Shallow clone of an object, setting or overriding a property with the given value

( key: string, value: mixed ) => ( source: Object ): Object
Parameters
key (string) Property name
value (mixed) Property value
source (Object) Source object
Returns
Object:
Example
set( "a", "lorem" )( { b: "ipsum" } )
// => { a: "lorem", b: "ipsum" }

sort

Sort array using custom function

( fn: Function ) => ( source: Array ): Array
Parameters
fn (Function) Sort function
source (Array) Array
Returns
Array:
Example
sort((a,b) => a.id-b.id)([{id:2}, {id: 1}])
// => [{id:1}, {id: 2}]

sortBy

Sort an array of objects by a custom field

( field: string, direction: string ) => ( source: Array ): Array
Parameters
field (string) Sort field name
direction (string) Sort direction
source (Array) Input array
Returns
Array:
Example
sortBy( "position" )( [
  { id: 1, position: 3 },
  { id: 2, position: 2 },
  { id: 3 },
  { id: 4, position: 5 },
  { id: 5, position: null },
] )
// [
//  { id: 2, position: 2 },
//  { id: 1, position: 3 },
//  { id: 4, position: 5 },
//  { id: 5, position: null },
//  { id: 3 },
//]

split

Splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

( separator: string|RegExp )( source: string ): Array
Parameters
separator ((string | RegExp)) Points where each split should occur
source (string) Source string
Returns
Array:
Example
split( "," )( "lorem,ipsum" )
// [ "lorem", "ipsum" ]

tail

Return last element in array

T[] => T[]
Parameters
source (Array) Source array
Returns
Array: All but first elements in array
Example
tail([1, 2, 3])
// => [2, 3]

throttle

Call a function only if it hasn't been called in the last timeWindow ms.

throttle
Parameters
fn (function) Function to be ran
timeWindow (integer) Time between each fn call
Returns
function: Either return fn if you've passed the timeWindow or return a timer that will run the fn in timeWindow ms

toLower

Convert string to lower case

( source: string ): string
Parameters
source (string) Source string
Returns
string:
Example
toLower("Lorem Ipsum")
// "lorem ipsum"

toggle

Add if not exists, remove otherwise

(item: mixed) => (input: Array): Array
Parameters
item (mixed) Toggable value
Returns
Array:
Example
toggle(1)([1, 2])
// => [2]
toggle(1)([2])
// => [1, 2]

top

Extract top elements from array

(count: number) => (source: Array): Array
Parameters
count (number) Number of elements to extract
source (Array) Source array
Returns
Array:
Example
top(2)([ 1, 2, 3 ])
// => [1, 2]

type

From ramda: Gives a single-word string description of the (native) type of a value, returning such answers as "Object", "Number", "Array", or "Null".

Does not attempt to distinguish user Object types any further, reporting them all as "Object".

type
Parameters
input (mixed) Something to check type on
Returns
string:
Example
type({})                // "Object"
type(1)                 // "Number"
type(false)             // "Boolean"
type("s")               // "String"
type(null)              // "Null"
type(undefined)         // "Undefined"
type([])                // "Array"
type(/[A-z]/)           // "RegExp"
type(new Date())        // "Date"
type(() => {})          // "Function"
type(Promise.resolve()) // "Promise"

zipWith

Combine pairs of values from two lists by an arbitrary function. If the lists differ in length, the output will have the length of the shorter list.

<A, B, C>(fn: A => B => C) => A[] => B[] => C[]
Parameters
fn (any) The function to combine values by
as (any) The first list
bs (any) The second list
Example
zipWith(a => b => a * b)([2,3,4])([2,5,6]) = [4, 15, 24]

zip

Combine pairs of values from two lists into a list of tuples. If the lists differ in length, the output will have the length of the shorter list. An alias of zipWith where the fn parameter is a tupling function.

<A, B>(A[]) => B[] => [A, B][]
Parameters
as (any)
Example
zip([2,3,4])([2,5,6]) = [[2, 2], [3, 5], [4, 6]]

zipFindWith

Combine pairs of values from two lists matching a binary predicate into a list of arbitrary values.

<A, B, C>( p: (A) => B => boolean ) => (fn: (A) => B => C) => (A[]) => (B[]) => C[]
Parameters
p (any) A binary predicate function taking a value from each list and returning a boolean
fn (any) A function taking a value from each list and returning anything
as (any) The first list
bs (any) The second list
Example
zipFindWith(
  a => b => (a * a) === b
)(a => b => a + b)([1, 2, 3])([4, 9, 1]) = [2, 6, 12]

zipFind

Combine pairs of values from two lists matching a binary predicate into a list of tuples. An alias of zipFindWith where the fn parameter is a tupling function.

<A, B>(p: (A) => B => boolean) => (A[]) => (B[]) => [A, B][]
Parameters
p (any)
Example
zipFind(
  a => b => (a * a) === b
)([1, 2, 3])([4, 9, 1]) = [[1, 1], [2, 4], [3, 9]]

zipByWith

Combine pairs of values from two lists, where the second list must be a list of objects and the value from the second list matches a matching object (see isMatch for details) generated by a value from the first list. The values are combined via an arbitrary function. An alias of zipFindWith where the p parameter is generated based on the matching object returned by getMatchObj.

<A, B, C>( getMatchObj: (A) => Object ) => (fn: (A) => B => C) => (A[]) => (B[]) => C[]
Parameters
getMatchObj (any)
Example
zipByWith(
  a => ({ aId: a.id })
)(a => b => merge(a, b))([
  { id: 1, color: 'red' },
  { id: 2, color: 'green' },
])([
  { aId: 2, fruit: 'pear' },
  { aId: 1, fruit: 'apple' },
]) = [
  { id: 1, aId: 1, color: 'red', fruit: 'apple' },
  { id: 2, aId: 2, color: 'green', fruit: 'pear' }
]

zipBy

Combine pairs of values from two lists, where the second list must be a list of objects and the value from the second list matches a matching object (see isMatch for details) generated by a value from the first list. The values are combined into a tuple. An alias of zipByWith where values are combined by a tupling function.

<A, B>( getMatchObj: (A) => Object ) => (A[]) => (B[]) => [A, B][]
Parameters
getMatchObj (any)
Example
zipByWith(
  a => ({ aId: a.id })
)([
  { id: 1, color: 'red' },
  { id: 2, color: 'green' },
])([
  { aId: 2, fruit: 'pear' },
  { aId: 1, fruit: 'apple' },
]) = [
  [{ id: 1, color: 'red' }, { aId: 1, fruit: 'apple' }],
  [{ id: 2, color: 'green' }, { aId: 2, fruit: 'pear' }],
]

zipToObj

Create an object from two arrays, one containing keys, the other values. Bost arrays will be trimmed to the smallest length.

( keys: Array ) => ( values: Array ): Object
Parameters
keys (Array) Array with keys
values (Array) Array with values
Returns
Object:
Example
zipToObj( [ a, b ] )( [ 1, 2 ] ) // => { a: 1, b: 2 }
zipToObj( [ a ] )( [ 1, 2 ] ) // => { a: 1 }