Skip to main content

Function

after

Takes a function and returns a new function that when called, will suppress the first times invocations.

function after<A extends any[], B>(times: number, fn: (...args: A) => B): (...args: A) => B
function after<A extends any[], B>(fn: (...args: A) => B): (times: number) => (...args: A) => B
const addTwo = F.after(2, N.add(2))

addTwo(4) // → None

addTwo(8) // → None
addTwo(16) // → Some(18)

allPass

Determines whether all the provided predicates return true for the given value.

function allPass<A>(value: A, fns: Array<(_1: A) => boolean>): boolean
function allPass<A>(fns: Array<(_1: A) => boolean>): (value: A) => boolean

always

Returns a function that always returns the provided value.

function always<A>(value: A): () => A

anyPass

Determines whether at least one of the provided predicate returns true for the given value.

function anyPass<A>(value: A, fns: Array<(_1: A) => boolean>): boolean
function anyPass<A>(fns: Array<(_1: A) => boolean>): (value: A) => boolean

before

Takes a function and returns a new function which when called, will allow the first times calls to invoke the given function, and any successive calls will be suppressed and the last result will be returned.

function before<A extends any[], B>(times: number, fn: (...args: A) => B): (...args: A) => B
function before<A extends any[], B>(fn: (...args: A) => B): (times: number) => (...args: A) => B
const addOne = F.before(2, N.add(1))

addOne(3) // → 4

addOne(6) // → 7
addOne(12) // → 7

both

Calls the two provided functions and returns && of the results → fn0(value) && fn1(value).

function both<A>(value: A, fn0: (_1: A) => boolean, fn1: (_1: A) => boolean): boolean
function both<A>(fn0: (_1: A) => boolean, fn1: (_1: A) => boolean): (value: A) => boolean

coerce

Takes a value and coerces a new type.

function coerce<T>(value: any): T

debounce

Takes a function, and returns a new function (no control values) which when called, will only invoke the given input function after a period of inactivity.

function debounce<A extends any[]>(fn: (...args: A) => void, delay: number): (...args: A) => void
function debounce<A extends any[]>(delay: number): (fn: (...args: A) => void) => (...args: A) => void

defaultTo

Returns a default value if value is nullable.

function defaultTo<A, B extends NonNullable<A>>(value: A, defaultValue: B): B
function defaultTo<A, B extends NonNullable<A>>(defaultValue: B): (value: A) => B
F.defaultTo(nullable, 'default') // → 'default'
pipe(2, F.defaultTo(1)) // → 2

either

Calls the two provided functions and returns || of the results → fn0(value) || fn1(value).

function either<A>(value: A, fn0: (_1: A) => boolean, fn1: (_1: A) => boolean): boolean
function either<A>(fn0: (_1: A) => boolean, fn1: (_1: A) => boolean): (value: A) => boolean

equals

Returns true if provided values are equal, otherwise, returns false.

function equals<A, B = A>(snd: A): (fst: B) => boolean
function equals<A, B = A>(fst: A, snd: B): boolean
function equals<A, B = A>(snd: B): (fst: A) => boolean
function equals(): boolean
F.equals([1, [2]], [1, [2]]) // → true
pipe({ name: 'Joe' }, F.equals({ name: 'John' })) // → false

falsy

Always returns false.

function falsy(): boolean

identity

Always returns the provided value, useful as a placeholder function.

function identity<A>(value: A): A

ifElse

Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns the result of truthyFn, otherwise, returns the result of falsyFn.

function ifElse<A, B>(
value: A,
predicateFn: (_1: A) => boolean,
truthyFn: (_1: A) => B,
falsyFn: (_1: A) => B
): B
function ifElse<A, B>(
predicateFn: (_1: A) => boolean,
truthyFn: (_1: A) => B,
falsyFn: (_1: A) => B
): (value: A) => B

ignore

Always returns void, useful as a placeholder function.

function ignore(): void

makeControlledDebounce

Takes a function, and returns a new function (and other control values) which when called, will only invoke the given input function after a period of inactivity. If leading is set to true, the function will be invoked immediately.

function makeControlledDebounce<A extends any[]>(fn: (...args: A) => void, options: Options): Controlled<A>
function makeControlledDebounce<A extends any[]>(options: Options): (fn: (...args: A) => void) => Controlled<A>

makeControlledThrottle

Takes a function and returns a new function (and other control values) which when used, suppresses calls to the given function to only once within the given delay. If leading is set to true, the function will be allowed to run on the first call before the throttling starts.

function makeControlledThrottle<A extends any[]>(fn: (...args: A) => void, options: Options): Controlled<A>
function makeControlledThrottle<A extends any[]>(options: Options): (fn: (...args: A) => void) => Controlled<A>

memoize

Alias for once.

function memoize<A extends any[], B>(fn: (...args: A) => B): (...args: A) => B

memoizeWithKey

Takes a function and returns a new function which once called, stores the result produced by the given function in a closure-based cache, using a cache key created by the function makeKeyFn.

function memoizeWithKey<A extends any[], B>(fn: (...args: A) => B): (makeKeyFn: (...args: A) => string) => (...args: A) => B
function memoizeWithKey<A extends any[], B>(makeKeyFn: (...args: A) => string, fn: (...args: A) => B): (...args: A) => B
let calls = 0
const makeArray = F.memoizeWithKey(S.make, (value: number) => {
calls = calls + 1
return A.makeWithIndex(value, index => index * 2)
})

makeArray(4) // → [0, 2, 4, 6]

makeArray(4) // → [0, 2, 4, 6]
makeArray(4) // → [0, 2, 4, 6]
calls // → 1

once

Takes a function and returns a new function which will invoke the given function once, and any successive calls will be suppressed, returning the value of the first call.

function once<A extends any[], B>(fn: (...args: A) => B): (...args: A) => B
const addTwoOnce = F.once(N.add(2))

addTwoOnce(4) // → 6

addTwoOnce(8) // → 6
addTwoOnce(16) // → 6

tap

Applies a side-effect function on the given value and returns the original value.

function tap<A>(value: A, fn: (_1: A) => void): A
function tap<A>(fn: (_1: A) => void): (value: A) => A
pipe(
A.makeWithIndex(3, N.succ),
F.tap(xs => console.log(xs)),
A.map(value => value * 2),
) // → [2, 4, 6]

throttle

Takes a function and returns a new function (no control values) which when used, suppresses calls to the given function to only once within the given delay.

function throttle<A extends any[]>(fn: (...args: A) => void, delay: number): (...args: A) => void
function throttle<A extends any[]>(delay: number): (fn: (...args: A) => void) => (...args: A) => void

toMutable

Takes a value and converts its immutable type to a mutable one.

function toMutable<T>(value: T): Mutable<T>

truthy

Always returns true.

function truthy(): boolean

tryCatch

Takes a function, which is called in the try/catch block, and returns the Result data type.

function tryCatch<A, B>(value: A, fn: (value: A) => B): Result<B, string>
function tryCatch<A, B>(fn: (value: A) => B): (value: A) => Result<B, string>
type User = {
readonly prop: string
}

F.tryCatch('{"name": "Joe"}', JSON.parse) // → Ok({ name: 'Joe' })

F.tryCatch('<>', JSON.parse) // → Error('Unexpected token < in JSON at position 0')
pipe(
'{"name": "Joe"}',
F.tryCatch<string, User>(JSON.parse),
R.map(user => user.name),
R.getWithDefault('oops'),
) // → 'Joe'

pipe('<>', F.tryCatch(JSON.parse), R.toOption, O.getWithDefault('oops')) // → 'oops'

unless

Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns value, otherwise, returns the result of falsyFn.

function unless<A, B>(value: A, predicateFn: (value: A) => boolean, falsyFn: (value: A) => B): A | B
function unless<A, B>(predicateFn: (value: A) => boolean, falsyFn: (value: A) => B): (value: A) => A | B
F.unless(
'ts',
value => value.length > 2,
value => `${value}-belt`,
) // → 'ts-belt'

pipe(
'ts',
F.unless(
value => value.length > 0,
value => `${value}-belt`,
),
) // → 'ts'

when

Tests the given value by passing it to the predicate, if the predicate is satisfied, the function returns the result of truthyFn, otherwise, returns value.

function when<A, B>(value: A, predicateFn: (value: A) => boolean, truthyFn: (value: A) => B): A | B
function when<A, B>(predicateFn: (value: A) => boolean, truthyFn: (value: A) => B): (value: A) => A | B
F.when(
'ts',
value => value.length > 1,
value => `${value}-belt`,
) // → 'ts-belt'

pipe(
'ts',
F.when(
value => value.length > 2,
value => `${value}-belt`,
),
) // → 'ts'