⚒️ Lou’s Code TypeScript curried
functional utils.
Usage
📦 Node
Install @lou.codes/utils
as a dependency:
Import it and use it:
🦕 Deno
Import @lou.codes/utils
using the npm:
prefix, and use it directly:
🌎 Browser
Import @lou.codes/utils
using esm.sh, and use it directly:
Useful links
Common
toString()
Parses given value to string.
Type parameters
Type parameter |
---|
Input extends Strigifiable | object |
Parameters
Returns
Input
extends Strigifiable
? `${Input<Input>}` : Input
extends
object
? ReturnType
<Input
<Input
>["toString"
]> : string
stringified value.
Example
View source
Conditions
when()
Curried conditional (like a functional ternary).
Type parameters
Type parameter | Value |
---|
Value | - |
Predicated | never |
Parameters
Parameter | Type | Description |
---|
predicate | Single <Predicated > extends Single <never > ? Filter <Value > : Predicate <Value , Predicated > | Predicate to check value against. |
Returns
Function
Curried function with predicate
in context.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Predicated & Value , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Value extends Predicated ? Value <Value > : Predicated |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Returns
TrueOutput
| FalseOutput
Example
View source
whenFalsy()
Like when
, but when is falsy the value is returned as is.
Type parameters
Type parameter | Value |
---|
Value | - |
Predicated | never |
Parameters
Parameter | Type | Description |
---|
predicate | Single <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 | Value |
---|
FalseInput | Value extends Predicated ? Value <Value > : Predicated |
FalseOutput | Predicated & Value |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Returns
Predicated
& Value
| FalseOutput
Example
View source
whenIsArray()
Conditional handler for when something is an Array
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <ReadOnlyArray <unknown >, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | ReadOnlyArray <unknown > |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsAsyncIterable()
Conditional handler for when something is an AsyncIterable
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Readonly <AsyncIterable <unknown >>, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Readonly <AsyncIterable <unknown >> |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsBigInt()
Conditional handler for when something is a BigInt
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <bigint , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | bigint |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsBoolean()
Conditional handler for when something is a Boolean
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <boolean , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | boolean |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsDate()
Conditional handler for when something is a Date
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Date , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Date |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsFalsy()
Conditional handler for when something is a Falsy
value.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Falsy , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Falsy |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsFunction()
Conditional handler for when something is a Function
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Function , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Function |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsIsomorphicIterable()
Conditional handler for when something is an AsynchronousIterable
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <IsomorphicIterable <unknown >, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | IsomorphicIterable <unknown > |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsIterable()
Conditional handler for when something is an Iterable
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Readonly <Iterable <unknown >>, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Readonly <Iterable <unknown >> |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsNull()
Conditional handler for when something is null
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <null , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | null |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsNullish()
Conditional handler for when something is a Nullish
value.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Nullish , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Nullish |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsNumber()
Conditional handler for when something is a Number
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <number , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | number |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsObject()
Conditional handler for when something is an Object
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <object , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | object |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsPromise()
Conditional handler for when something is a Promise
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Promise <unknown >, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Promise <unknown > |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsPropertyKey()
Conditional handler for when something is a PropertyKey
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <PropertyKey , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | PropertyKey |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsPrototypeOfObject()
Conditional handler for when something has an Object
prototype.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <never , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | never |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Returns
TrueOutput
| FalseOutput
View source
whenIsRegExp()
Conditional handler for when something is a RegExp
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <RegExp , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | RegExp |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsSafeInteger()
Conditional handler for when a number is a SafeInteger
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <never , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | never |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Returns
TrueOutput
| FalseOutput
View source
whenIsString()
Conditional handler for when something is a String
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <string , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | string |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsSymbol()
Conditional handler for when something is a Symbol
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <symbol , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | symbol |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsTruthy()
Conditional handler for when something is a Truthy
value.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Readonly <unknown >, TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | Readonly <unknown > |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenIsUndefined()
Conditional handler for when something is undefined
.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <undefined , TrueOutput > |
Returns
Function
Type parameters
Type parameter | Value |
---|
FalseInput | undefined |
FalseOutput | TrueOutput |
Parameters
Parameter | Type |
---|
falsyHandler | Unary <FalseInput , FalseOutput > |
Returns
Function
Parameters
Parameter | Type |
---|
value | unknown |
Returns
TrueOutput
| FalseOutput
View source
whenTruthy()
Like when
, but when is falsy the value is returned as is.
Type parameters
Type parameter | Value |
---|
Value | - |
Predicated | never |
Parameters
Parameter | Type | Description |
---|
predicate | Single <Predicated > extends Single <never > ? Filter <Value > : Predicate <Value , Predicated > | Predicate to check value against. |
Returns
Function
Curried function with predicate
in context.
Type parameters
Parameters
Parameter | Type |
---|
truthyHandler | Unary <Predicated & Value , Output > |
Returns
Function
Parameters
Returns
Value
| Output
Example
View source
Functions
always()
Returns a function that always returns the same value.
Type parameters
Parameters
Returns
Function
Function that always return the given value.
Returns
Input
Example
View source
applyTo()
Takes a value and applies a function to it.
Type parameters
Parameters
Returns
Function
Function that expects a function that will receive the input
.
Type parameters
Parameters
Parameter | Type |
---|
unary | Unary <Input , Output > |
Returns
Output
Example
View source
entryKey()
Get key of an Entry
.
Type parameters
Type parameter |
---|
InputEntry extends Entry |
Parameters
Parameter | Type | Description |
---|
entry | InputEntry | Entry to get key from. |
Returns
EntryKey
<InputEntry
>
Key of an Entry
.
Example
View source
entryValue()
Get value of an Entry
.
Type parameters
Type parameter |
---|
InputEntry extends Entry |
Parameters
Parameter | Type | Description |
---|
entry | InputEntry | Entry to get vale from. |
Returns
EntryValue
<InputEntry
>
Value of an Entry
.
Example
View source
flip()
Flips arguments of a curried function.
Type parameters
Type parameter |
---|
UnaryToFlip extends Unary <never , Unary <never , unknown >> |
Parameters
Parameter | Type |
---|
unary | UnaryToFlip |
Returns
Function
Curried function with unary
in context.
Parameters
Parameter | Type |
---|
…secondArgument | Parameters <UnaryOutput <UnaryToFlip >> |
Returns
Function
Parameters
Parameter | Type |
---|
…firstArgument | Parameters <UnaryToFlip > |
Returns
UnaryOutput
<UnaryOutput
<UnaryToFlip
>>
Example
View source
identity()
Identity function.
Type parameters
Parameters
Returns
Input
Same value given.
Example
View source
memoize()
Memoize function return values for expensive operations.
Type parameters
Type parameter |
---|
MemoizedFunction extends Unary <never , unknown > |
Parameters
Parameter | Type | Description |
---|
unary | MemoizedFunction | Function to memoize. |
Returns
MemoizedFunction
Curried function with unary
in context.
Example
View source
negate()
Takes a function and returns a copy with negated output.
Type parameters
Type parameter | Value |
---|
Item | - |
Predicated | never |
Parameters
Parameter | Type | Description |
---|
predicate | Single <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
View source
once()
Runs a function once, and after that returns always the same value.
Type parameters
Type parameter |
---|
OnceFunction extends Unary <never , unknown > |
Parameters
Parameter | Type | Description |
---|
unary | OnceFunction | Function to run once. |
Returns
Function
Curried function with unary
in context.
Parameters
Parameter | Type |
---|
value | UnaryInput <OnceFunction > |
Returns
UnaryOutput
<OnceFunction
> | UnaryOutput
<OnceFunction
>
Example
View source
tap()
Tap into a value before calling a function.
Type parameters
Parameters
Parameter | Type | Description |
---|
tapper | Unary <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
Parameter | Type |
---|
tapped | Tapped |
Returns
Tapped
Example
View source
thunk()
Delayed evaluation function.
Type parameters
Type parameter |
---|
Input |
Output |
Parameters
Parameter | Type |
---|
unary | Unary <Input , Output > |
Returns
Function
Function that will run the given function when called.
Parameters
Returns
Function
Returns
Output
Example
View source
Numbers
cryptoNumber()
Generates number
using SubtleCrypto#digest
and the given seed.
⚠️ IMPORTANT: This only works in secure contexts (HTTPS/Node).
Parameters
Parameter | Type | Description |
---|
seed | string | Numeric | Seed to be used to generate random numbers. |
Returns
Promise
<number
>
Pseudo-random number from seed.
Example
See
SubtleCrypto#digest
View source
Objects
awaitableHandler()
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
Parameter | Type | Description |
---|
handler | (awaitable : Value ) => Output | Handler function to be called with the value. |
Returns
Function
Curried function with handler
in context.
Type parameters
Type parameter |
---|
AwaitableValue |
Parameters
Parameter | Type |
---|
awaitable | AwaitableValue |
Returns
AwaitableValue
extends Promise
<Value
> ? Promise
<Output
> :
Output
Example
View source
get()
Get the value of a property in an object.
Type parameters
Type parameter |
---|
Key extends PropertyKey |
Parameters
Returns
Function
Curried function with key
in context.
Type parameters
Type parameter |
---|
Source extends Readonly <Record <Key , unknown >> |
Parameters
Parameter | Type |
---|
object | Source |
Returns
Source
[Key
& keyof Source
]
Example
View source
mutate()
Function to encapsulate object mutations.
Type parameters
Type parameter |
---|
Update extends object |
Parameters
Parameter | Type | Description |
---|
update | Update | Update to apply to given target. |
Returns
Function
Curried function with update
in context.
Type parameters
Type parameter |
---|
Target extends object |
Parameters
Parameter | Type |
---|
target | Target |
Returns
Target
& Update
Example
View source
omit()
Omits given properties from an object.
Type parameters
Type parameter |
---|
Properties extends ReadOnlyArray <PropertyKey > |
Parameters
Parameter | Type | Description |
---|
…properties | Properties | Properties to omit. |
Returns
Function
Curried function with properties
in context.
Type parameters
Type parameter |
---|
Source extends Readonly <Record <PropertyKey , unknown >> |
Parameters
Parameter | Type |
---|
object | Source |
Returns
Omit
<Source
, Properties
[number
]>
Example
View source
pick()
Picks given properties from an object.
Type parameters
Type parameter |
---|
Properties extends ReadOnlyArray <PropertyKey > |
Parameters
Parameter | Type | Description |
---|
…properties | Properties | Properties to pick. |
Returns
Function
Curried function with properties
in context.
Type parameters
Type parameter |
---|
Source extends Readonly <Record <PropertyKey , unknown >> |
Parameters
Parameter | Type |
---|
object | Source |
Returns
Pick
<Source
, Properties
[number
]>
Example
View source
set()
Set the value of a property in an object (read only).
Type parameters
Type parameter |
---|
Key extends PropertyKey |
Parameters
Returns
Function
Curried function with key
in context.
Type parameters
Parameters
Returns
Function
Type parameters
Type parameter |
---|
Source extends object |
Parameters
Parameter | Type |
---|
object | Source |
Returns
Omit
<Source
, Key
> & Readonly
<Record
<Key
, Value
>>
Example
View source
Promises
otherwise()
Sets a promise catch
handler.
Type parameters
Parameters
Parameter | Type |
---|
handler | Unary <unknown , Output | PromiseLike <Output >> |
Returns
Function
Curried function with handler
set in context.
Type parameters
Type parameter |
---|
PromisedValue |
Parameters
Parameter | Type |
---|
promise | Promise <PromisedValue > |
Returns
Promise
<Output
| PromisedValue
>
Example
View source
then()
Sets a promise then
handler.
Type parameters
Type parameter |
---|
HandlerInput |
Output |
Parameters
Parameter | Type |
---|
handler | Unary <HandlerInput , Output | PromiseLike <Output >> |
Returns
Function
Curried function with handler
set in context.
Type parameters
Type parameter |
---|
PromiseInstance extends Promise <HandlerInput > |
Parameters
Parameter | Type |
---|
promise | PromiseInstance |
Returns
Promise
<Output
>
Example
View source