Skip to content

Utils Reference

Coverage License NPM Version Open Issues Size

⚒️ Lou’s Code TypeScript curried functional utils.

Usage

📦 Node

Install @lou.codes/utils as a dependency:

Terminal window
1
pnpm add @lou.codes/utils
2
# or
3
npm install @lou.codes/utils
4
# or
5
yarn add @lou.codes/utils

Import it and use it:

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

🦕 Deno

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

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

🌎 Browser

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

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

Common

toString()

1
function toString<Input>(input: Input): Input extends Strigifiable ?
2
`${Input<Input>}`
3
: Input extends object ? ReturnType<Input<Input>["toString"]>
4
: string;

Parses given value to string.

Type parameters

Type parameter
Input extends Strigifiable | object

Parameters

ParameterType
inputInput

Returns

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

stringified value.

Example

1
toString(10); // "10"
2
toString({}); // "[object Object]"
3
toString({ toString: _ => "test" }); // "test"

View source

Conditions

when()

1
function when<Value, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Filter<Value>
3
: Predicate<Value, Predicated>,
4
): <TrueOutput>(
5
truthyHandler: Unary<Predicated & Value, TrueOutput>,
6
) => <FalseInput, FalseOutput>(
7
falsyHandler: Unary<FalseInput, FalseOutput>,
8
) => (value: Value) => TrueOutput | FalseOutput;

Curried conditional (like a functional ternary).

Type parameters

Type parameterValue
Value-
Predicatednever

Parameters

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

Returns

Function

Curried function with predicate in context.

Type parameters
Type parameter
TrueOutput
Parameters
ParameterType
truthyHandlerUnary<Predicated & Value, TrueOutput>
Returns

Function

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

Function

Parameters
ParameterType
valueValue
Returns

TrueOutput | FalseOutput

Example

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

View source


whenFalsy()

1
function whenFalsy<Value, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Filter<Value>
3
: Predicate<Value, Predicated>,
4
): <FalseInput, FalseOutput>(
5
falsyHandler: Unary<FalseInput, FalseOutput>,
6
) => (value: Value) => (Predicated & Value) | FalseOutput;

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

Type parameters

Type parameterValue
Value-
Predicatednever

Parameters

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

Returns

Function

Curried function with predicate in context.

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

Function

Parameters
ParameterType
valueValue
Returns

Predicated & Value | FalseOutput

Example

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

View source


whenIsArray()

1
function whenIsArray<TrueOutput>(
2
truthyHandler: Unary<ReadOnlyArray<unknown>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is an Array.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<ReadOnlyArray<unknown>, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsAsyncIterable()

1
function whenIsAsyncIterable<TrueOutput>(
2
truthyHandler: Unary<Readonly<AsyncIterable<unknown>>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is an AsyncIterable.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Readonly<AsyncIterable<unknown>>, TrueOutput>

Returns

Function

Type parameters
Type parameterValue
FalseInputReadonly<AsyncIterable<unknown>>
FalseOutputTrueOutput
Parameters
ParameterType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsBigInt()

1
function whenIsBigInt<TrueOutput>(
2
truthyHandler: Unary<bigint, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a BigInt.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<bigint, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsBoolean()

1
function whenIsBoolean<TrueOutput>(
2
truthyHandler: Unary<boolean, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Boolean.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<boolean, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsDate()

1
function whenIsDate<TrueOutput>(
2
truthyHandler: Unary<Date, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Date.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Date, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsFalsy()

1
function whenIsFalsy<TrueOutput>(
2
truthyHandler: Unary<Falsy, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Falsy value.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Falsy, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsFunction()

1
function whenIsFunction<TrueOutput>(
2
truthyHandler: Unary<Function, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Function.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Function, TrueOutput>

Returns

Function

Type parameters
Type parameterValue
FalseInputFunction
FalseOutputTrueOutput
Parameters
ParameterType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsIsomorphicIterable()

1
function whenIsIsomorphicIterable<TrueOutput>(
2
truthyHandler: Unary<IsomorphicIterable<unknown>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is an AsynchronousIterable.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<IsomorphicIterable<unknown>, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsIterable()

1
function whenIsIterable<TrueOutput>(
2
truthyHandler: Unary<Readonly<Iterable<unknown>>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is an Iterable.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Readonly<Iterable<unknown>>, TrueOutput>

Returns

Function

Type parameters
Type parameterValue
FalseInputReadonly<Iterable<unknown>>
FalseOutputTrueOutput
Parameters
ParameterType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNull()

1
function whenIsNull<TrueOutput>(
2
truthyHandler: Unary<null, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is null.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<null, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNullish()

1
function whenIsNullish<TrueOutput>(
2
truthyHandler: Unary<Nullish, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Nullish value.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Nullish, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsNumber()

1
function whenIsNumber<TrueOutput>(
2
truthyHandler: Unary<number, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Number.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<number, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsObject()

1
function whenIsObject<TrueOutput>(
2
truthyHandler: Unary<object, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is an Object.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<object, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPromise()

1
function whenIsPromise<TrueOutput>(
2
truthyHandler: Unary<Promise<unknown>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Promise.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Promise<unknown>, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPropertyKey()

1
function whenIsPropertyKey<TrueOutput>(
2
truthyHandler: Unary<PropertyKey, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a PropertyKey.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<PropertyKey, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsPrototypeOfObject()

1
function whenIsPrototypeOfObject<TrueOutput>(
2
truthyHandler: Unary<never, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: object) => TrueOutput | FalseOutput;

Conditional handler for when something has an Object prototype.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<never, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueobject
Returns

TrueOutput | FalseOutput

View source


whenIsRegExp()

1
function whenIsRegExp<TrueOutput>(
2
truthyHandler: Unary<RegExp, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a RegExp.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<RegExp, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsSafeInteger()

1
function whenIsSafeInteger<TrueOutput>(
2
truthyHandler: Unary<never, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: number) => TrueOutput | FalseOutput;

Conditional handler for when a number is a SafeInteger.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<never, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valuenumber
Returns

TrueOutput | FalseOutput

View source


whenIsString()

1
function whenIsString<TrueOutput>(
2
truthyHandler: Unary<string, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a String.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<string, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsSymbol()

1
function whenIsSymbol<TrueOutput>(
2
truthyHandler: Unary<symbol, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Symbol.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<symbol, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsTruthy()

1
function whenIsTruthy<TrueOutput>(
2
truthyHandler: Unary<Readonly<unknown>, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is a Truthy value.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<Readonly<unknown>, TrueOutput>

Returns

Function

Type parameters
Type parameterValue
FalseInputReadonly<unknown>
FalseOutputTrueOutput
Parameters
ParameterType
falsyHandlerUnary<FalseInput, FalseOutput>
Returns

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenIsUndefined()

1
function whenIsUndefined<TrueOutput>(
2
truthyHandler: Unary<undefined, TrueOutput>,
3
): <FalseInput, FalseOutput>(
4
falsyHandler: Unary<FalseInput, FalseOutput>,
5
) => (value: unknown) => TrueOutput | FalseOutput;

Conditional handler for when something is undefined.

Type parameters

Type parameter
TrueOutput

Parameters

ParameterType
truthyHandlerUnary<undefined, TrueOutput>

Returns

Function

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

Function

Parameters
ParameterType
valueunknown
Returns

TrueOutput | FalseOutput

View source


whenTruthy()

1
function whenTruthy<Value, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Filter<Value>
3
: Predicate<Value, Predicated>,
4
): <Output>(
5
truthyHandler: Unary<Predicated & Value, Output>,
6
) => (value: Value) => Value | Output;

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

Type parameters

Type parameterValue
Value-
Predicatednever

Parameters

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

Returns

Function

Curried function with predicate in context.

Type parameters
Type parameter
Output
Parameters
ParameterType
truthyHandlerUnary<Predicated & Value, Output>
Returns

Function

Parameters
ParameterType
valueValue
Returns

Value | Output

Example

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

View source

Functions

always()

1
function always<Input>(input: Input): () => Input;

Returns a function that always returns the same value.

Type parameters

Type parameter
Input

Parameters

ParameterType
inputInput

Returns

Function

Function that always return the given value.

Returns

Input

Example

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

View source


applyTo()

1
function applyTo<Input>(
2
input: Input,
3
): <Output>(unary: Unary<Input, Output>) => Output;

Takes a value and applies a function to it.

Type parameters

Type parameter
Input

Parameters

ParameterType
inputInput

Returns

Function

Function that expects a function that will receive the input.

Type parameters
Type parameter
Output
Parameters
ParameterType
unaryUnary<Input, Output>
Returns

Output

Example

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

View source


entryKey()

1
function entryKey<InputEntry>(entry: InputEntry): EntryKey<InputEntry>;

Get key of an Entry.

Type parameters

Type parameter
InputEntry extends Entry

Parameters

ParameterTypeDescription
entryInputEntryEntry to get key from.

Returns

EntryKey<InputEntry>

Key of an Entry.

Example

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

View source


entryValue()

1
function entryValue<InputEntry>(entry: InputEntry): EntryValue<InputEntry>;

Get value of an Entry.

Type parameters

Type parameter
InputEntry extends Entry

Parameters

ParameterTypeDescription
entryInputEntryEntry to get vale from.

Returns

EntryValue<InputEntry>

Value of an Entry.

Example

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

View source


flip()

1
function flip<UnaryToFlip>(
2
unary: UnaryToFlip,
3
): (
4
...secondArgument: Parameters<UnaryOutput<UnaryToFlip>>
5
) => (
6
...firstArgument: Parameters<UnaryToFlip>
7
) => UnaryOutput<UnaryOutput<UnaryToFlip>>;

Flips arguments of a curried function.

Type parameters

Type parameter
UnaryToFlip extends Unary<never, Unary<never, unknown>>

Parameters

ParameterType
unaryUnaryToFlip

Returns

Function

Curried function with unary in context.

Parameters
ParameterType
secondArgumentParameters<UnaryOutput<UnaryToFlip>>
Returns

Function

Parameters
ParameterType
firstArgumentParameters<UnaryToFlip>
Returns

UnaryOutput<UnaryOutput<UnaryToFlip>>

Example

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

View source


identity()

1
function identity<Input>(input: Input): Input;

Identity function.

Type parameters

Type parameter
Input

Parameters

ParameterType
inputInput

Returns

Input

Same value given.

Example

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

View source


memoize()

1
function memoize<MemoizedFunction>(unary: MemoizedFunction): MemoizedFunction;

Memoize function return values for expensive operations.

Type parameters

Type parameter
MemoizedFunction extends Unary<never, unknown>

Parameters

ParameterTypeDescription
unaryMemoizedFunctionFunction to memoize.

Returns

MemoizedFunction

Curried function with unary in context.

Example

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

View source


negate()

1
function negate<Item, Predicated>(
2
predicate: Single<Predicated> extends Single<never> ? Filter<Item>
3
: Predicate<Item, Predicated>,
4
): Single<Predicated> extends Single<never> ? Unary<Item, boolean>
5
: Predicate<Item, Exclude<Item, Predicated>>;

Takes a function and returns a copy with negated output.

Type parameters

Type parameterValue
Item-
Predicatednever

Parameters

ParameterTypeDescription
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

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

View source


once()

1
function once<OnceFunction>(
2
unary: OnceFunction,
3
): (
4
value: UnaryInput<OnceFunction>,
5
) => UnaryOutput<OnceFunction> | UnaryOutput<OnceFunction>;

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

Type parameters

Type parameter
OnceFunction extends Unary<never, unknown>

Parameters

ParameterTypeDescription
unaryOnceFunctionFunction to run once.

Returns

Function

Curried function with unary in context.

Parameters
ParameterType
valueUnaryInput<OnceFunction>
Returns

UnaryOutput<OnceFunction> | UnaryOutput<OnceFunction>

Example

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

View source


tap()

1
function tap<Input>(
2
tapper: Unary<Input, unknown>,
3
): <Tapped>(tapped: Tapped) => Tapped;

Tap into a value before calling a function.

Type parameters

Type parameter
Input

Parameters

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

Returns

Function

Curried function with tapper in context.

Type parameters
Type parameter
Tapped extends Unary<Input, unknown>
Parameters
ParameterType
tappedTapped
Returns

Tapped

Example

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

View source


thunk()

1
function thunk<Input, Output>(
2
unary: Unary<Input, Output>,
3
): (input: Input) => () => Output;

Delayed evaluation function.

Type parameters

Type parameter
Input
Output

Parameters

ParameterType
unaryUnary<Input, Output>

Returns

Function

Function that will run the given function when called.

Parameters
ParameterType
inputInput
Returns

Function

Returns

Output

Example

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

View source

Numbers

cryptoNumber()

1
function cryptoNumber(seed: string | Numeric): Promise<number>;

Generates number using SubtleCrypto#digest and the given seed.

⚠️ IMPORTANT: This only works in secure contexts (HTTPS/Node).

Parameters

ParameterTypeDescription
seedstring | NumericSeed to be used to generate random numbers.

Returns

Promise<number>

Pseudo-random number from seed.

Example

1
const seededRandom1 = await random("some seed");
2
const seededRandom2 = await random("some seed");
3
4
seededRandom1 === seededRandom2; // true because it has the same seed

See

SubtleCrypto#digest

View source

Objects

awaitableHandler()

1
function awaitableHandler<Value, Output>(
2
handler: (awaitable: Value) => Output,
3
): <AwaitableValue>(
4
awaitable: AwaitableValue,
5
) => 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

Type parameter
Value
Output

Parameters

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

Returns

Function

Curried function with handler in context.

Type parameters
Type parameter
AwaitableValue
Parameters
ParameterType
awaitableAwaitableValue
Returns

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

Example

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

View source


get()

1
function get<Key>(
2
key: Key,
3
): <Source>(object: Source) => Source[Key & keyof Source];

Get the value of a property in an object.

Type parameters

Type parameter
Key extends PropertyKey

Parameters

ParameterType
keyKey

Returns

Function

Curried function with key in context.

Type parameters
Type parameter
Source extends Readonly<Record<Key, unknown>>
Parameters
ParameterType
objectSource
Returns

Source[Key & keyof Source]

Example

1
const getFoo = get("foo");
2
3
getFoo({ foo: "bar" }); // "bar"
4
getFoo({}); // undefined

View source


mutate()

1
function mutate<Update>(
2
update: Update,
3
): <Target>(target: Target) => Target & Update;

Function to encapsulate object mutations.

Type parameters

Type parameter
Update extends object

Parameters

ParameterTypeDescription
updateUpdateUpdate to apply to given target.

Returns

Function

Curried function with update in context.

Type parameters
Type parameter
Target extends object
Parameters
ParameterType
targetTarget
Returns

Target & Update

Example

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

View source


omit()

1
function omit<Properties>(
2
...properties: Properties
3
): <Source>(object: Source) => Omit<Source, Properties[number]>;

Omits given properties from an object.

Type parameters

Type parameter
Properties extends ReadOnlyArray<PropertyKey>

Parameters

ParameterTypeDescription
propertiesPropertiesProperties to omit.

Returns

Function

Curried function with properties in context.

Type parameters
Type parameter
Source extends Readonly<Record<PropertyKey, unknown>>
Parameters
ParameterType
objectSource
Returns

Omit<Source, Properties[number]>

Example

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

View source


pick()

1
function pick<Properties>(
2
...properties: Properties
3
): <Source>(object: Source) => Pick<Source, Properties[number]>;

Picks given properties from an object.

Type parameters

Type parameter
Properties extends ReadOnlyArray<PropertyKey>

Parameters

ParameterTypeDescription
propertiesPropertiesProperties to pick.

Returns

Function

Curried function with properties in context.

Type parameters
Type parameter
Source extends Readonly<Record<PropertyKey, unknown>>
Parameters
ParameterType
objectSource
Returns

Pick<Source, Properties[number]>

Example

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

View source


set()

1
function set<Key>(
2
key: Key,
3
): <Value>(
4
value: Value,
5
) => <Source>(
6
object: Source,
7
) => Omit<Source, Key> & Readonly<Record<Key, Value>>;

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

Type parameters

Type parameter
Key extends PropertyKey

Parameters

ParameterType
keyKey

Returns

Function

Curried function with key in context.

Type parameters
Type parameter
Value
Parameters
ParameterType
valueValue
Returns

Function

Type parameters
Type parameter
Source extends object
Parameters
ParameterType
objectSource
Returns

Omit<Source, Key> & Readonly<Record<Key, Value>>

Example

1
const setFoo = set("foo");
2
3
setFoo("baz")({ foo: "bar" }); // { foo: "baz" }
4
setFoo("baz")({ bar: "foo" }); // { bar: "foo", foo: "baz" }
5
setFoo("baz")({}); // { foo: "baz" }

View source

Promises

otherwise()

1
function otherwise<Output>(
2
handler: Unary<unknown, Output | PromiseLike<Output>>,
3
): <PromisedValue>(
4
promise: Promise<PromisedValue>,
5
) => Promise<Output | PromisedValue>;

Sets a promise catch handler.

Type parameters

Type parameter
Output

Parameters

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

Returns

Function

Curried function with handler set in context.

Type parameters
Type parameter
PromisedValue
Parameters
ParameterType
promisePromise<PromisedValue>
Returns

Promise<Output | PromisedValue>

Example

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

View source


then()

1
function then<HandlerInput, Output>(
2
handler: Unary<HandlerInput, Output | PromiseLike<Output>>,
3
): <PromiseInstance>(promise: PromiseInstance) => Promise<Output>;

Sets a promise then handler.

Type parameters

Type parameter
HandlerInput
Output

Parameters

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

Returns

Function

Curried function with handler set in context.

Type parameters
Type parameter
PromiseInstance extends Promise<HandlerInput>
Parameters
ParameterType
promisePromiseInstance
Returns

Promise<Output>

Example

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

View source