constant
util
Creates a function that returns value.
Installation
Import
import { constant } from '@tulx/utils';Source Code
Implementation
/**
* Creates a function that returns value.
*
* @param value - The value to return from the new function.
* @returns Returns the new constant function.
*
* @example
* ```ts
* const objects = times(2, constant({ 'a': 1 }));
* console.log(objects); // [{ 'a': 1 }, { 'a': 1 }]
* console.log(objects[0] === objects[1]); // true
* ```
*/
export function constant<T>(value: T): () => T {
return () => value;
}
Example
import { constant } from '@tulx/utils';
const objects = times(2, constant({ 'a': 1 }));
console.log(objects); // [{ 'a': 1 }, { 'a': 1 }]
console.log(objects[0] === objects[1]); // trueRelated 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.
defaultTo
Checks value to determine whether a default value should be returned in its place.
flow
Creates a function that returns the result of invoking the given functions with the this binding of the created function.