Collection · 17 utilities

Function utilities

Higher-order helpers: compose, curry, memoize, run once. They take functions as input, so each page shows executed usage instead of a JSON example.

Logical xor operator

True when exactly one of two values is truthy.

function xor([a, b]) { return Boolean(a) !== Boolean(b); }function

Identity function

Return the argument unchanged.

function identity(value) { return value; }function

Box handler

Chain transformations on a value, then unwrap the result.

function boxHandler(value) { return { next: f => boxHandler(f(value)), done: f => f(value) }; }…function

Check if a value is a function

True for anything callable: functions, arrows, async and generator functions, classes.

function isFunction(value) { return typeof value === 'function'; }function

Check if a value is a generator function

Read the internal tag of function* and async function* declarations.

function isGeneratorFunction(value) { return ['[object GeneratorFunction]', '[object AsyncGener…function

Check if a value is an async function

Read the internal tag of async function declarations and arrows.

function isAsyncFunction(value) { return Object.prototype.toString.call(value) === '[object Asy…function

Compose functions from left to right

Build one function that passes a value through each function in order.

function pipe(...fns) { return input => fns.reduce((value, fn) => fn(value), input); }function

Compose functions

Build one function that applies functions from right to left, as in f(g(x)).

function compose(...fns) { return input => fns.reduceRight((value, fn) => fn(value), input); }function

Curry a function

Collect arguments across calls until the function has as many as it declares.

function curry(fn, ...args) { return fn.length <= args.length ? fn(...args) : (...more) => curr…function

Delay the evaluation of a function

Turn a call into a thunk: a function that runs the call only when invoked.

function thunkify(fn) { return (...args) => () => fn(...args); }function

Execute a function once

Run a function on the first call and return that result afterwards.

function once(fn) { let done = false, result; return (...args) => { if (!done) { result = fn(..…function

Flip the arguments of a function

Call a function with its arguments in reverse order.

function flip(fn) { return (...args) => fn(...args.reverse()); }function

Memoize a function

Cache results by argument so each input is computed once.

function memoize(fn) { const cache = new Map(); return arg => { if (!cache.has(arg)) cache.set(…function

Partially apply a function

Fix the first arguments now and pass the rest later.

function partial(fn, ...preset) { return (...rest) => fn(...preset, ...rest); }function

Uncurry a function

Call a curried function with several arguments at once.

function uncurry(fn, depth = 1) { return (...args) => args.slice(0, depth).reduce((next, arg) =…function

Try a function in the local code lab, then read its limits before using it in an application.

Type two or more characters. Try “duplicate”, “webhook” or “retry”.