Back to Functions

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]); // true

Related Functions