Skip to content

Utils Reference

Coverage License NPM Version Open Issues Size

⚒️ Lou’s TypeScript curried functional utils.

Usage

📦 Node

Install @lou.codes/utils as a dependency:

Terminal window
pnpm add @lou.codes/utils
# or
npm install @lou.codes/utils
# or
yarn add @lou.codes/utils

Import it and use it:

import { get } from "@lou.codes/utils";
get("foo")({ foo: "bar" }); // "bar"

🦕 Deno

Import @lou.codes/utils using the npm: prefix, and use it directly:

import { get } from "npm:@lou.codes/utils";
get("foo")({ foo: "bar" }); // "bar"

🌎 Browser

Import @lou.codes/utils using esm.sh, and use it directly:

<script type="module">
import { get } from "https://esm.sh/@lou.codes/utils";
get("foo")({ foo: "bar" }); // "bar"
</script>

Classes

bind

bind<Context>(context): <Method>(method: Method) => Method

Bounds the given context to the given method.

Type parameters

Name
Context

Parameters

NameType
contextContext

Returns

fn

Curried function with context in context.

▸ <Method>(method): Method

Type parameters
NameType
Methodextends Function
Parameters
NameType
methodMethod
Returns

Method

Example

const bindConsole = bind(console);
const log = bindConsole(console.log);
log("Hello world!"); // Logs "Hello world!" without context errors.

View source


construct

construct<Constructor>(constructor): (…constructorArguments: ConstructorParameters<Constructor>) => InstanceType<Constructor>

Creates a new instance of the given constructor with the given arguments.

Type parameters

NameType
Constructorextends Class<never>

Parameters

NameType
constructorConstructor

Returns

fn

Curried function with constructor in context.

▸ (...constructorArguments): InstanceType<Constructor>

Parameters
NameType
...constructorArgumentsConstructorParameters<Constructor>
Returns

InstanceType<Constructor>

Example

const constructDate = construct(Date);
constructDate(); // Returns the current Date (like `new Date()`).

View source


constructArray

constructArray<Item>(length): Item[]

Functional alias for new Array().

Type parameters

Name
Item

Parameters

NameType
lengthnumber

Returns

Item[]

View source

constructArray<Items>(...items): Items[number][]

Functional alias for new Array().

Type parameters

NameType
Itemsextends ReadOnlyArray

Parameters

NameType
...itemsItems

Returns

Items[number][]

View source


constructDate

constructDate(date?): Date

Functional alias for new Date().

Parameters

NameType
date (optional)string | number | { getVarDate: () => VarDate ; [toPrimitive]: (hint: "default") => string(hint: "string") => string(hint: "number") => number(hint: string) => string | number ; getDate: () => number ; getDay: () => number ; getFullYear: () => number ; getHours: () => number ; getMilliseconds: () => number ; getMinutes: () => number ; getMonth: () => number ; getSeconds: () => number ; getTime: () => number ; getTimezoneOffset: () => number ; getUTCDate: () => number ; getUTCDay: () => number ; getUTCFullYear: () => number ; getUTCHours: () => number ; getUTCMilliseconds: () => number ; getUTCMinutes: () => number ; getUTCMonth: () => number ; getUTCSeconds: () => number ; setDate: (date: number) => number ; setFullYear: (year: number, month?: number, date?: number) => number ; setHours: (hours: number, min?: number, sec?: number, ms?: number) => number ; setMilliseconds: (ms: number) => number ; setMinutes: (min: number, sec?: number, ms?: number) => number ; setMonth: (month: number, date?: number) => number ; setSeconds: (sec: number, ms?: number) => number ; setTime: (time: number) => number ; setUTCDate: (date: number) => number ; setUTCFullYear: (year: number, month?: number, date?: number) => number ; setUTCHours: (hours: number, min?: number, sec?: number, ms?: number) => number ; setUTCMilliseconds: (ms: number) => number ; setUTCMinutes: (min: number, sec?: number, ms?: number) => number ; setUTCMonth: (month: number, date?: number) => number ; setUTCSeconds: (sec: number, ms?: number) => number ; toDateString: () => string ; toISOString: () => string ; toJSON: (key?: any) => string ; toLocaleDateString: () => string(locales?: string | string[], options?: DateTimeFormatOptions) => string(locales?: LocalesArgument, options?: DateTimeFormatOptions) => string ; toLocaleString: () => string(locales?: string | string[], options?: DateTimeFormatOptions) => string(locales?: LocalesArgument, options?: DateTimeFormatOptions) => string ; toLocaleTimeString: () => string(locales?: string | string[], options?: DateTimeFormatOptions) => string(locales?: LocalesArgument, options?: DateTimeFormatOptions) => string ; toString: () => string ; toTimeString: () => string ; toUTCString: () => string ; valueOf: () => number }

Returns

Date

View source


constructError

constructError(...constructorArguments): Error

Functional alias for new Error().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructIntlCollator

constructIntlCollator(...constructorArguments): Collator

Functional alias for new Intl.Collator(), which enables language-sensitive string comparison.

Parameters

NameType
...constructorArguments[locales?: string | string[], options?: CollatorOptions]

Returns

Collator

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator

View source


constructIntlDateTimeFormat

constructIntlDateTimeFormat(...constructorArguments): DateTimeFormat

Functional alias for new Intl.DateTimeFormat(), which enables language-sensitive date and time formatting.

Parameters

NameType
...constructorArguments[locales?: string | string[], options?: DateTimeFormatOptions]

Returns

DateTimeFormat

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat

View source


constructIntlLocale

constructIntlLocale(tag, options?): Locale

Functional alias for new Intl.Locale(), which represents a Unicode locale identifier.

Parameters

NameTypeDescription
tagReadOnly<Locale | LocaleIdentifier>-
options (optional)Object-
options.baseName (optional)stringA string containing the language, and the script and region if available.
options.calendar (optional)stringThe part of the Locale that indicates the locale’s calendar era.
options.caseFirst (optional)LocaleCollationCaseFirstFlag that defines whether case is taken into account for the locale’s collation rules.
options.collation (optional)stringThe collation type used for sorting
options.hourCycle (optional)LocaleHourCycleKeyThe time keeping format convention used by the locale.
options.language (optional)stringThe primary language subtag associated with the locale.
options.numberingSystem (optional)stringThe numeral system used by the locale.
options.numeric (optional)booleanFlag that defines whether the locale has special collation handling for numeric characters.
options.region (optional)stringThe region of the world (usually a country) associated with the locale. Possible values are region codes as defined by ISO 3166-1.
options.script (optional)stringThe script used for writing the particular language used in the locale. Possible values are script codes as defined by ISO 15924.

Returns

Locale

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale

View source


constructIntlNumberFormat

constructIntlNumberFormat(...constructorArguments): NumberFormat

Functional alias for new Intl.NumberFormat(), which enables language-sensitive number formatting.

Parameters

NameType
...constructorArguments[locales?: string | string[], options?: NumberFormatOptions]

Returns

NumberFormat

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

View source


constructIntlPluralRules

constructIntlPluralRules(...constructorArguments): PluralRules

Functional alias for new Intl.PluralRules(), which enables plural-sensitive formatting and plural-related language rules.

Parameters

NameType
...constructorArguments[locales?: string | string[], options?: PluralRulesOptions]

Returns

PluralRules

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules

View source


constructIntlRelativeTimeFormat

constructIntlRelativeTimeFormat(...constructorArguments): RelativeTimeFormat

Functional alias for new Intl.RelativeTimeFormat(), which enables language-sensitive relative time formatting.

Parameters

NameType
...constructorArguments[locales?: string | string[], options?: RelativeTimeFormatOptions]

Returns

RelativeTimeFormat

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat

View source


constructMap

constructMap<Key, Value>(iterable?): Map<Key, Value>

Functional alias for new Map().

Type parameters

Name
Key
Value

Parameters

NameType
iterable (optional)Iterable<readonly [Key, Value]>

Returns

Map<Key, Value>

View source


constructPromise

constructPromise<Value>(executor): Promise<Value>

Functional alias for new Promise().

Type parameters

Name
Value

Parameters

NameType
executor(resolve: (value: Value) => void, reject: (reason?: unknown) => void) => void

Returns

Promise<Value>

View source


constructProxy

constructProxy<Target>(target, handler): Target

Functional alias for new Proxy().

Type parameters

NameType
Targetextends object

Parameters

NameType
targetTarget
handlerProxyHandler<Target>

Returns

Target

View source


constructRangeError

constructRangeError(...constructorArguments): Error

Functional alias for new RangeError().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructReferenceError

constructReferenceError(...constructorArguments): Error

Functional alias for new ReferenceError().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructRegExp

constructRegExp(...constructorArguments): RegExp

Functional alias for new RegExp().

Parameters

NameType
...constructorArguments[pattern: string | RegExp, flags?: string]

Returns

RegExp

View source


constructSet

constructSet<Value>(iterable?): Set<Value>

Functional alias for new Set().

Type parameters

Name
Value

Parameters

NameType
iterable (optional)Iterable<Value>

Returns

Set<Value>

View source


constructSyntaxError

constructSyntaxError(...constructorArguments): Error

Functional alias for new SyntaxError().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructTypeError

constructTypeError(...constructorArguments): Error

Functional alias for new TypeError().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructURIError

constructURIError(...constructorArguments): Error

Functional alias for new URIError().

Parameters

NameType
...constructorArguments[message?: string, options?: ErrorOptions]

Returns

Error

View source


constructWeakMap

constructWeakMap<Key, Value>(iterable?): WeakMap<Key, Value>

Functional alias for new WeakMap().

Type parameters

NameType
Keyextends object
ValueValue

Parameters

NameType
iterable (optional)Iterable<readonly [Key, Value]>

Returns

WeakMap<Key, Value>

View source


constructWeakSet

constructWeakSet<Value>(iterable?): WeakSet<Value>

Functional alias for new WeakSet().

Type parameters

NameType
Valueextends object

Parameters

NameType
iterable (optional)Iterable<Value>

Returns

WeakSet<Value>

View source

Common

toString

toString<Input>(input): Input extends Strigifiable ? `${Input}` : Input extends { toString: () => string } ? ReturnType<Input["toString"]> : string

Parses given value to string, if number is given, then that’s used as a radix and a function is returned expecting an actual value.

Type parameters

NameType
Inputextends Strigifiable | { toString: () => string }

Parameters

NameType
inputInput

Returns

Input extends Strigifiable ? `${Input}` : Input extends { toString: () => string } ? ReturnType<Input["toString"]> : string

string or function expecting a value.

Example

const toDecimal = toString(10);
toDecimal(10); // "10"
toString(16)(16); // "10"
toString(2)(2); // "10"
toString({}); // "[object Object]"
toString({ toString: _ => "test" }); // "test"

View source

Conditions

when

when<Value, Predicated>(predicate): <TrueOutput>(truthyHandler: Unary<Predicated & Value, TrueOutput>) => <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: Value) => TrueOutput | FalseOutput

Curried conditional (like a functional ternary).

Type parameters

NameType
ValueValue
Predicatednever

Parameters

NameTypeDescription
predicateSingle<Predicated> extends Single<never> ? Filter<Value> : Predicate<Value, Predicated>Predicate to check value against.

Returns

fn

Curried function with predicate in context.

▸ <TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: Value) => TrueOutput | FalseOutput

Type parameters
Name
TrueOutput
Parameters
NameType
truthyHandlerUnary<Predicated & Value, TrueOutput>
Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: Value) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputValue extends Predicated ? Value : Predicated
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueValue
Returns

TrueOutput | FalseOutput

Example

const isEven = (value: number) => value % 2 === 0;
const whenIsEven = when(isEven);
const double = (value: number) => value * 2;
const doubleWhenEven = whenIsEven(double);
const triple = (value: number) => value * 3;
const tripleWhenOdd = whenIsEven(triple);
tripleWhenOdd(2); // 4
tripleWhenOdd(3); // 9

View source


whenFalsy

whenFalsy<Value, Predicated>(predicate): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: Value) => Predicated & Value | FalseOutput

Like when, but when is falsy the value is returned as is.

Type parameters

NameType
ValueValue
Predicatednever

Parameters

NameTypeDescription
predicateSingle<Predicated> extends Single<never> ? Filter<Value> : Predicate<Value, Predicated>Predicate to check value against.

Returns

fn

Curried function with predicate in context.

▸ <FalseInput, FalseOutput>(falsyHandler): (value: Value) => Predicated & Value | FalseOutput

Type parameters
NameType
FalseInputValue extends Predicated ? Value : Predicated
FalseOutputPredicated & Value
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): Predicated & Value | FalseOutput

Parameters
NameType
valueValue
Returns

Predicated & Value | FalseOutput

Example

const isEven = (value: number) => value % 2 === 0;
const whenIsOdd = whenFalsy(isEven);
const double = (value: number) => value * 2;
const doubleWhenOdd = whenIsOdd(double);
doubleWhenOdd(2); // 2
doubleWhenOdd(3); // 6

View source


whenIsArray

whenIsArray<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is an Array.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<ReadOnlyArray<unknown>, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputReadOnlyArray<unknown>
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsAsyncIterable

whenIsAsyncIterable<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is an AsyncIterable.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<{ [asyncIterator]: () => AsyncIterator<unknown, any, undefined> }, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInput{ [asyncIterator]: () => AsyncIterator<unknown, any, undefined> }
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsBigInt

whenIsBigInt<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a BigInt.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<bigint, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputbigint
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsBoolean

whenIsBoolean<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Boolean.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<boolean, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputboolean
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsDate

whenIsDate<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Date.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<Date, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputDate
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsFalsy

whenIsFalsy<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Falsy value.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<Falsy, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputFalsy
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsFunction

whenIsFunction<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Function.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<Function<ReadOnlyArray, unknown>, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputFunction<ReadOnlyArray, unknown>
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsIsomorphicIterable

whenIsIsomorphicIterable<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is an AsynchronousIterable.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<IsomorphicIterable<unknown>, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputIsomorphicIterable<unknown>
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsIterable

whenIsIterable<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is an Iterable.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<{ [iterator]: () => Iterator<unknown, any, undefined> }, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInput{ [iterator]: () => Iterator<unknown, any, undefined> }
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNull

whenIsNull<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is null.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<null, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputnull
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNullish

whenIsNullish<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Nullish value.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<Nullish, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputNullish
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNumber

whenIsNumber<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Number.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<number, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputnumber
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsObject

whenIsObject<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is an Object.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<object, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputobject
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPromise

whenIsPromise<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Promise.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<Promise<unknown>, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputPromise<unknown>
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPropertyKey

whenIsPropertyKey<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a PropertyKey.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<PropertyKey, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputPropertyKey
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPrototypeOfObject

whenIsPrototypeOfObject<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: object) => TrueOutput | FalseOutput

Conditional handler for when something has an Object prototype.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<never, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: object) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputnever
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueobject
Returns

TrueOutput | FalseOutput

View source


whenIsRegExp

whenIsRegExp<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a RegExp.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<RegExp, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputRegExp
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsSafeInteger

whenIsSafeInteger<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: number) => TrueOutput | FalseOutput

Conditional handler for when a number is a SafeInteger.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<never, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: number) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputnever
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valuenumber
Returns

TrueOutput | FalseOutput

View source


whenIsString

whenIsString<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a String.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<string, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputstring
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsSymbol

whenIsSymbol<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Symbol.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<symbol, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputsymbol
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsTruthy

whenIsTruthy<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is a Truthy value.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<unknown, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputunknown
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsUndefined

whenIsUndefined<TrueOutput>(truthyHandler): <FalseInput, FalseOutput>(falsyHandler: Unary<FalseInput, FalseOutput>) => (value: unknown) => TrueOutput | FalseOutput

Conditional handler for when something is undefined.

Type parameters

Name
TrueOutput

Parameters

NameType
truthyHandlerUnary<undefined, TrueOutput>

Returns

fn

▸ <FalseInput, FalseOutput>(falsyHandler): (value: unknown) => TrueOutput | FalseOutput

Type parameters
NameType
FalseInputundefined
FalseOutputTrueOutput
Parameters
NameType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

fn

▸ (value): TrueOutput | FalseOutput

Parameters
NameType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenTruthy

whenTruthy<Value, Predicated>(predicate): <Output>(truthyHandler: Unary<Predicated & Value, Output>) => (value: Value) => Value | Output

Like when, but when is falsy the value is returned as is.

Type parameters

NameType
ValueValue
Predicatednever

Parameters

NameTypeDescription
predicateSingle<Predicated> extends Single<never> ? Filter<Value> : Predicate<Value, Predicated>Predicate to check value against.

Returns

fn

Curried function with predicate in context.

▸ <Output>(truthyHandler): (value: Value) => Value | Output

Type parameters
Name
Output
Parameters
NameType
truthyHandlerUnary<Predicated & Value, Output>
Returns

fn

▸ (value): Value | Output

Parameters
NameType
valueValue
Returns

Value | Output

Example

const isEven = (value: number) => value % 2 === 0;
const whenIsEven = whenTruthy(isEven);
const double = (value: number) => value * 2;
const doubleWhenEven = whenIsEven(double);
doubleWhenEven(2); // 4
doubleWhenEven(3); // 3

View source

Functions

always

always<Input>(input): () => Input

Returns a function that always returns the same value.

Type parameters

Name
Input

Parameters

NameType
inputInput

Returns

fn

Function that always return the given value.

▸ (): Input

Returns

Input

Example

const alwaysFoo = always("foo");
const fillWithFoo = map(alwaysFoo);
fillWithFoo([0, 1, 2]); // ["foo", "foo", "foo"]

View source


applyTo

applyTo<Input>(input): <Output>(unary: Unary<Input, Output>) => Output

Takes a value and applies a function to it.

Type parameters

Name
Input

Parameters

NameType
inputInput

Returns

fn

Function that expects a function that will receive the input.

▸ <Output>(unary): Output

Type parameters
Name
Output
Parameters
NameType
unaryUnary<Input, Output>
Returns

Output

Example

const applyTo10 = applyTo(10);
applyTo10((value: number) => value * 2); // 20
applyTo10((value: number) => value / 2); // 5

View source


entryKey

entryKey<InputEntry>(entry): EntryKey<InputEntry>

Get key of an Entry.

Type parameters

NameType
InputEntryextends Entry

Parameters

NameTypeDescription
entryInputEntryEntry to get key from.

Returns

EntryKey<InputEntry>

Key of an Entry.

Example

entryKey(["key", "value"]); // "key"

View source


entryValue

entryValue<InputEntry>(entry): EntryValue<InputEntry>

Get value of an Entry.

Type parameters

NameType
InputEntryextends Entry

Parameters

NameTypeDescription
entryInputEntryEntry to get vale from.

Returns

EntryValue<InputEntry>

Value of an Entry.

Example

entryKey(["key", "value"]); // "value"

View source


flip

flip<UnaryToFlip>(unary): (…secondArgument: Parameters<UnaryOutput<UnaryToFlip>>) => (…firstArgument: Parameters<UnaryToFlip>) => UnaryOutput<UnaryOutput<UnaryToFlip>>

Flips arguments of a curried function.

Type parameters

NameType
UnaryToFlipextends Unary<never, Unary<never, unknown>>

Parameters

NameType
unaryUnaryToFlip

Returns

fn

Curried function with unary in context.

▸ (...secondArgument): (…firstArgument: Parameters<UnaryToFlip>) => UnaryOutput<UnaryOutput<UnaryToFlip>>

Parameters
NameType
...secondArgumentParameters<UnaryOutput<UnaryToFlip>>
Returns

fn

▸ (...firstArgument): UnaryOutput<UnaryOutput<UnaryToFlip>>

Parameters
NameType
...firstArgumentParameters<UnaryToFlip>
Returns

UnaryOutput<UnaryOutput<UnaryToFlip>>

Example

const concat = (first: string) => (second: string) => `${first}${second}`;
const flippedConcat = flip(concat);
flippedConcat("1")("2"); // "21"

View source


identity

identity<Input>(input): Input

Identity function.

Type parameters

Name
Input

Parameters

NameType
inputInput

Returns

Input

Same value given.

Example

identity("foo"); // "foo"

View source


memoize

memoize<MemoizedFunction>(unary): MemoizedFunction

Memoize function return values for expensive operations.

Type parameters

NameType
MemoizedFunctionextends Unary<never, unknown>

Parameters

NameTypeDescription
unaryMemoizedFunctionFunction to memoize.

Returns

MemoizedFunction

Curried function with unary in context.

Example

const expensiveOperation = (value: number) => value * 2;
const memoizedOperation = memoize(expensiveOperation);
memoizedOperation(2); // 4
memoizedOperation(2); // 4 (cached)

View source


negate

negate<Item, Predicated>(predicate): Single<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Exclude<Item, Predicated>>

Takes a function and returns a copy with negated output.

Type parameters

NameType
ItemItem
Predicatednever

Parameters

NameTypeDescription
predicateSingle<Predicated> extends Single<never> ? Filter<Item> : Predicate<Item, Predicated>Function to be negated.

Returns

Single<Predicated> extends Single<never> ? Unary<Item, boolean> : Predicate<Item, Exclude<Item, Predicated>>

Source function with negated output.

Example

const isPositive = (value: number) => value >= 0;
const isNegative = not(isPositive);
isPositive(1); // true
isNegative(1); // false

View source


once

once<OnceFunction>(unary): (value: UnaryInput<OnceFunction>) => UnaryOutput<OnceFunction> | UnaryOutput<OnceFunction>

Runs a function once, and after that returns always the same value.

Type parameters

NameType
OnceFunctionextends Unary<never, unknown>

Parameters

NameTypeDescription
unaryOnceFunctionFunction to run once.

Returns

fn

Curried function with unary in context.

▸ (value): UnaryOutput<OnceFunction> | UnaryOutput<OnceFunction>

Parameters
NameType
valueUnaryInput<OnceFunction>
Returns

UnaryOutput<OnceFunction> | UnaryOutput<OnceFunction>

Example

const expensiveOperation = (value: number) => value * 2;
const onceOperation = once(expensiveOperation);
onceOperation(2); // 4
onceOperation(3); // 4 (cached)

View source


tap

tap<Input>(tapper): <Tapped>(tapped: Tapped) => Tapped

Tap into a value before calling a function.

Type parameters

Name
Input

Parameters

NameTypeDescription
tapperUnary<Input, unknown>Tapper function to be called with the value.

Returns

fn

Curried function with tapper in context.

▸ <Tapped>(tapped): Tapped

Type parameters
NameType
Tappedextends Unary<Input, unknown>
Parameters
NameType
tappedTapped
Returns

Tapped

Example

const log = tap(console.log);
const double = (value: number) => value * 2;
const doubleAndLog = log(double);
doubleAndLog(2); // 4 (returns and logs 4)

View source


thunk

thunk<Input, Output>(unary): (input: Input) => () => Output

Delayed evaluation function.

Type parameters

Name
Input
Output

Parameters

NameType
unaryUnary<Input, Output>

Returns

fn

Function that will run the given function when called.

▸ (input): () => Output

Parameters
NameType
inputInput
Returns

fn

▸ (): Output

Returns

Output

Example

const always = thunk(id);
const alwaysFoo = always("foo");
alwaysFoo(); // "foo"

View source

Objects

awaitableHandler

awaitableHandler<Value, Output>(handler): <AwaitableValue>(awaitable: AwaitableValue) => AwaitableValue extends Promise<Value> ? Promise<Output> : Output

If the given value is a promise, the handler is called when is resolved, otherwise the handler is called directly with the value.

Type parameters

Name
Value
Output

Parameters

NameTypeDescription
handler(awaitable: Value) => OutputHandler function to be called with the value.

Returns

fn

Curried function with handler in context.

▸ <AwaitableValue>(awaitable): AwaitableValue extends Promise<Value> ? Promise<Output> : Output

Type parameters
Name
AwaitableValue
Parameters
NameType
awaitableAwaitableValue
Returns

AwaitableValue extends Promise<Value> ? Promise<Output> : Output

Example

const double = maybePromiseHandler((value: number) => value * 2);
double(2); // 4
double(Promise.resolve(2)); // Promise<4>

View source


get

get<Key>(key): <Source>(object: Source) => Source[Key & keyof Source]

Get the value of a property in an object.

Type parameters

NameType
Keyextends PropertyKey

Parameters

NameType
keyKey

Returns

fn

Curried function with key in context.

▸ <Source>(object): Source[Key & keyof Source]

Type parameters
NameType
Sourceextends readonly [] | readonly [unknown, unknown] | Record<Key, unknown> | Readonly<ReadonlyMap<unknown, unknown>> | Readonly<ReadonlySet<unknown>> | readonly unknown[] | Function & Record<Key, unknown> | { readonly [Property in PropertyKey]: ReadOnly<Record<Key, unknown>[Property]> }
Parameters
NameType
objectSource
Returns

Source[Key & keyof Source]

Example

const getFoo = get("foo");
getFoo({ foo: "bar" }); // "bar"
getFoo({}); // undefined

View source


mutate

mutate<Update>(update): <Target>(target: Target) => Target & Update

Function to encapsulate object mutations.

Type parameters

NameType
Updateextends object

Parameters

NameTypeDescription
updateUpdateUpdate to apply to given target.

Returns

fn

Curried function with update in context.

▸ <Target>(target): Target & Update

Type parameters
NameType
Targetextends object
Parameters
NameType
targetTarget
Returns

Target & Update

Example

const state = { a: 1 };
mutate(set("a")(2))(state);
console.log(state); // { a: 2 }

View source


omit

omit<Properties>(...properties): <Source>(object: Source) => Omit<Source, Properties[number]>

Omits given properties from an object.

Type parameters

NameType
Propertiesextends ReadOnlyArray<PropertyKey>

Parameters

NameTypeDescription
...propertiesPropertiesProperties to omit.

Returns

fn

Curried function with properties in context.

▸ <Source>(object): Omit<Source, Properties[number]>

Type parameters
NameType
Sourceextends Object
Parameters
NameType
objectSource
Returns

Omit<Source, Properties[number]>

Example

const object = { a: 1, b: 2, c: 3 };
const omitAC = omit("a", "c");
const result = omitAC(object); // { b: 2 }

View source


pick

pick<Properties>(...properties): <Source>(object: Source) => Pick<Source, Properties[number]>

Picks given properties from an object.

Type parameters

NameType
Propertiesextends ReadOnlyArray<PropertyKey>

Parameters

NameTypeDescription
...propertiesPropertiesProperties to pick.

Returns

fn

Curried function with properties in context.

▸ <Source>(object): Pick<Source, Properties[number]>

Type parameters
NameType
Sourceextends Object
Parameters
NameType
objectSource
Returns

Pick<Source, Properties[number]>

Example

const object = { a: 1, b: 2, c: 3 };
const pickAC = pick("a", "c");
const result = pickAC(object); // { a: 1, c: 3 }

View source


set

set<Key>(key): <Value>(value: Value) => <Source>(object: Source) => Omit<Source, Key> & ReadOnlyRecord<Key, Value>

Set the value of a property in an object (read only).

Type parameters

NameType
Keyextends PropertyKey

Parameters

NameType
keyKey

Returns

fn

Curried function with key in context.

▸ <Value>(value): <Source>(object: Source) => Omit<Source, Key> & ReadOnlyRecord<Key, Value>

Type parameters
Name
Value
Parameters
NameType
valueValue
Returns

fn

▸ <Source>(object): Omit<Source, Key> & ReadOnlyRecord<Key, Value>

Type parameters
NameType
Sourceextends object
Parameters
NameType
objectSource
Returns

Omit<Source, Key> & ReadOnlyRecord<Key, Value>

Example

const setFoo = set("foo");
setFoo("baz")({ foo: "bar" }); // { foo: "baz" }
setFoo("baz")({ bar: "foo" }); // { bar: "foo", foo: "baz" }
setFoo("baz")({}); // { foo: "baz" }

View source

Promises

otherwise

otherwise<Output>(handler): <PromisedValue>(promise: Promise<PromisedValue>) => Promise<Output | PromisedValue>

Sets a promise catch handler.

Type parameters

Name
Output

Parameters

NameType
handlerUnary<unknown, Output | PromiseLike<Output>>

Returns

fn

Curried function with handler set in context.

▸ <PromisedValue>(promise): Promise<Output | PromisedValue>

Type parameters
Name
PromisedValue
Parameters
NameType
promisePromise<PromisedValue>
Returns

Promise<Output | PromisedValue>

Example

const otherwiseLog = otherwise(console.error);
otherwiseLog(fetch("/api")); // Logs on error.

View source


then

then<HandlerInput, Output>(handler): <PromiseInstance>(promise: PromiseInstance) => Promise<Output>

Sets a promise then handler.

Type parameters

Name
HandlerInput
Output

Parameters

NameType
handlerUnary<HandlerInput, Output | PromiseLike<Output>>

Returns

fn

Curried function with handler set in context.

▸ <PromiseInstance>(promise): Promise<Output>

Type parameters
NameType
PromiseInstanceextends Promise<HandlerInput>
Parameters
NameType
promisePromiseInstance
Returns

Promise<Output>

Example

const thenJson = then((response: Response) => response.json());
thenJson(fetch("/api")); // Parsed response

View source