Functions

These are the available library functions.

identity

A function that takes a single parameter and returns it. Essentially a no-op for mapping.

map(identity, [1, 2, 3])

or

Given several methods which can return a boolean, create a new predicate which logically "ors" them together.

const isNotATeen = or(lessThan(13), greaterThan(19))
isNotATeen(13)

and

Given several methods which can return a boolean, create a new predicate which logically "ands" them together.

const isBetweenFiveAndTen = and(greaterThan(5), lessThan(10))
isBetweenFiveAndTen(7)

apply

Given a function that takes multiple parameters, create a new function which takes those parameters as a single tuple instead.

const adder = (a: number, b: string) => a + b
const tupleAdder = apply(adder)
adder(5, 10)
tupleAdder([5, 10])

map

Exactly like Array.prototype.map, but functional instead of on the Array prototype.

const adder = map(a => a + 1)
adder([1, 2, 3])

chain

Given a value and a function, run the function on the value. This is like map, but for working on any data type. Useful in piped series of computations.

const adder = chain(a => a + 1)
adder(data)

constant

Given a value, return a function that will return that value every time it is called.

const alwaysTrue = constant(true)
alwaysTrue()

cond

Basically a functional switch statement. Run several predicates to find the correct code path, then transform the data as requested.

const detectRange = cond(
  [lessThan(0), () => "Negative"],
  [lessThan(100), () => "Less than 100"],
  () => "Greater than 100",
)

detectRange(101)

equals

Given a value, create a new function which will take a second value and compare their equality. Uses Javascript's === operator.

const equalsFive = equals(5)
equalsFive(5)
equalsFive(10)

first

Given an array, return the first value or undefined.

first([1, 2, 3])

nth

Given an array, return the nth value or undefined. nth is 1-indexed.

nth([1, 2, 3])(1) // 1

index

Given an array, return the index value or undefined. index is 0-indexed.

index([1, 2, 3])(1) // 2

last

Given an array, return the last value or undefined.

last([1, 2, 3])

rest

Given an array, return every item except the first.

rest([1, 2, 3]) // [2, 3]

greaterThan

Create a predicate which checks whether a value is greater than a number.

const greaterThanFive = greaterThan(5)
greaterThanFive(10)
greaterThanFive(1)

greaterThanEquals

Create a predicate which checks whether a value is greater than or equal a number.

const greaterThanEqualsFive = greaterThanEquals(5)
greaterThanEqualsFive(10)
greaterThanEqualsFive(1)

lessThan

Create a predicate which checks whether a value is less than a number.

const lessThanFive = lessThan(5)
lessThanFive(10)
lessThanFive(1)

lessThanEquals

Create a predicate which checks whether a value is less than or equal a number.

const lessThanEqualsFive = lessThanEquals(5)
lessThanEqualsFive(10)
lessThanEqualsFive(1)

isBetween

Create a predicate which checks whether a value is between two values.

const isBetween5And10Exclusive = isBetween(5, 10, false)
isBetween5And10Exclusive(10)

const isBetween5And10Inclusive = isBetween(5, 10, true)
isBetween5And10Inclusive(1)

pluck

Create a function which grabs a key from an object.

const getName = pluck("name")
getName({ name: "Me", age: Infinity })

omit

Create a function which removes a key from an object.

const removeName = omit("name")
removeName({ name: "Me", age: Infinity })

pipe

Create a function runs a series of functions in order, passing the result of each step to the next in the series.

const incrementToStringAndRepeat = pipe(
  (a: number) => a + 1,
  (b: number) => b.toString(),
  (c: string) => c + c,
)

incrementToStringAndRepeat(100)

Last updated