defaultTo
util
Checks value to determine whether a default value should be returned in its place.
Installation
Import
import { defaultTo } from '@tulx/utils';Source Code
Implementation
/**
* Checks value to determine whether a default value should be returned in its place.
*
* @param value - The value to check.
* @param defaultValue - The default value.
* @returns Returns the resolved value.
*
* @example
* ```ts
* defaultTo(1, 10); // 1
* defaultTo(undefined, 10); // 10
* ```
*/
export function defaultTo<T>(value: T | null | undefined, defaultValue: T): T {
return value === null || value === undefined ? defaultValue : value;
}
Example
import { defaultTo } from '@tulx/utils';
defaultTo(1, 10); // 1
defaultTo(undefined, 10); // 10Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
flow
Creates a function that returns the result of invoking the given functions with the this binding of the created function.