These are the available library functions.
identity
A function that takes a single parameter and returns it. Essentially a no-op for mapping.
Usage Type Definition
Copy map (identity , [ 1 , 2 , 3 ])
Copy type or = < Args extends any []>(
... options : Array <( ... args : Args ) => unknown >
) => ( ... args : Args ) : boolean
or
Given several methods which can return a boolean, create a new predicate which logically "ors" them together.
Usage Type Definition
Copy const isNotATeen = or ( lessThan ( 13 ) , greaterThan ( 19 ))
isNotATeen ( 13 )
Copy type or = < Args extends any []>(
... options : Array <( ... args : Args ) => unknown >
) => ( ... args : Args ) : boolean
and
Given several methods which can return a boolean, create a new predicate which logically "ands" them together.
Usage Type Definition
Copy const isBetweenFiveAndTen = and ( greaterThan ( 5 ) , lessThan ( 10 ))
isBetweenFiveAndTen ( 7 )
Copy type and = < Args extends any []>(
... options : Array <( ... args : Args ) => unknown >
) => ( ... args : Args ) : boolean
apply
Given a function that takes multiple parameters, create a new function which takes those parameters as a single tuple instead.
Usage Type Definition
Copy const adder = (a : number , b : string ) => a + b
const tupleAdder = apply (adder)
adder ( 5 , 10 )
tupleAdder ([ 5 , 10 ])
Copy type apply = < T , Args extends any []>( fn : ( ... args : Args []) => T ) : (args : Args ) => T
map
Exactly like Array.prototype.map
, but functional instead of on the Array prototype.
Usage Type Definition
Copy const adder = map (a => a + 1 )
adder ([ 1 , 2 , 3 ])
Copy type map = < T , U >( fn : (a : T ) => U ) => (data : T []) : U []
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 pipe
d series of computations.
Usage Type Definition
Copy const adder = chain (a => a + 1 )
adder (data)
Copy type chain = < T , U >( fn : (a : T ) => U ) => (data : T ) : U
constant
Given a value, return a function that will return that value every time it is called.
Usage Type Definition
Copy const alwaysTrue = constant ( true )
alwaysTrue ()
Copy type constant = < T >(value : T ) => () => T
cond
Basically a functional switch statement. Run several predicates to find the correct code path, then transform the data as requested.
Usage Type Definition
Copy const detectRange = cond (
[ lessThan ( 0 ) , () => "Negative" ] ,
[ lessThan ( 100 ) , () => "Less than 100" ] ,
() => "Greater than 100" ,
)
detectRange ( 101 )
Copy type cond = < T , U >(conditions : Condition < T , U >[]) : (data : T ) => U | undefined
equals
Given a value, create a new function which will take a second value and compare their equality. Uses Javascript's ===
operator.
Usage Type Definition
Copy const equalsFive = equals ( 5 )
equalsFive ( 5 )
equalsFive ( 10 )
Copy type equals = < T >(value : T ) => (data : T ) : boolean
first
Given an array, return the first value or undefined
.
Usage Type Definition
Copy type first = < T >(items : T []) => T | undefined
nth
Given an array, return the nth value or undefined
. nth
is 1-indexed.
Usage Type Definition
Copy nth ([ 1 , 2 , 3 ])( 1 ) // 1
Copy type nth = (n : number ) => < T >(items : T []) => T | undefined
index
Given an array, return the index value or undefined
. index
is 0-indexed.
Usage Type Definition
Copy index ([ 1 , 2 , 3 ])( 1 ) // 2
Copy type index = (n : number ) => < T >(items : T []) => T | undefined
last
Given an array, return the last value or undefined
.
Usage Type Definition
Copy type last = < T >(items : T []) => T | undefined
rest
Given an array, return every item except the first.
Usage Type Definition
Copy rest ([ 1 , 2 , 3 ]) // [2, 3]
Copy type rest = < T >(items : T []) => T []
greaterThan
Create a predicate which checks whether a value is greater than a number.
Usage Type Definition
Copy const greaterThanFive = greaterThan ( 5 )
greaterThanFive ( 10 )
greaterThanFive ( 1 )
Copy type greaterThan = (value : number ) => (data : number ) : boolean
greaterThanEquals
Create a predicate which checks whether a value is greater than or equal a number.
Usage Type Definition
Copy const greaterThanEqualsFive = greaterThanEquals ( 5 )
greaterThanEqualsFive ( 10 )
greaterThanEqualsFive ( 1 )
Copy type greaterThanEquals = (value : number ) => (data : number ) : boolean
lessThan
Create a predicate which checks whether a value is less than a number.
Usage Type Definition
Copy const lessThanFive = lessThan ( 5 )
lessThanFive ( 10 )
lessThanFive ( 1 )
Copy type lessThan = (value : number ) => (data : number ) : boolean
lessThanEquals
Create a predicate which checks whether a value is less than or equal a number.
Usage Type Definition
Copy const lessThanEqualsFive = lessThanEquals ( 5 )
lessThanEqualsFive ( 10 )
lessThanEqualsFive ( 1 )
Copy type lessThanEquals = (value : number ) => (data : number ) : boolean
isBetween
Create a predicate which checks whether a value is between two values.
Usage Type Definition
Copy const isBetween5And10Exclusive = isBetween ( 5 , 10 , false )
isBetween5And10Exclusive ( 10 )
const isBetween5And10Inclusive = isBetween ( 5 , 10 , true )
isBetween5And10Inclusive ( 1 )
Copy type isBetween = (
a : number ,
b : number ,
inclusive ?: boolean ,
) => (data : number ) => boolean
pluck
Create a function which grabs a key from an object.
Usage Type Definition
Copy const getName = pluck ( "name" )
getName ({ name : "Me" , age : Infinity })
Copy type pluck = < U , T extends keyof U >(key : T ) => (data : U ) : U [ T ]
omit
Create a function which removes a key from an object.
Usage Type Definition
Copy const removeName = omit ( "name" )
removeName ({ name : "Me" , age : Infinity })
Copy type omit = < U , T extends keyof U >(key : T ) => (data : U ) : Omit < U , T >
pipe
Create a function runs a series of functions in order, passing the result of each step to the next in the series.
Usage Type Definition
Copy const incrementToStringAndRepeat = pipe (
(a : number ) => a + 1 ,
(b : number ) => b .toString () ,
(c : string ) => c + c ,
)
incrementToStringAndRepeat ( 100 )
Copy type pipe = < A , B , C , D >( a : (data : A ) => B , b : (data : B ) => C , c : (data : C ) => D ) : (data : A ) => D