UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 1.67 kB
{"version":3,"file":"constant.cjs","names":[],"sources":["../src/constant.ts"],"sourcesContent":["/**\n * A function that takes any arguments and returns the provided `value` on every\n * invocation. This is useful to provide trivial implementations for APIs or in\n * combination with a ternary or other conditional execution to allow to short-\n * circuit more complex implementations for a specific case.\n *\n * Notice that this is a dataLast impl where the function needs to be invoked\n * to get the \"do nothing\" function.\n *\n * See also:\n * `doNothing` - A function that doesn't return anything.\n * `identity` - A function that returns the first argument it receives.\n *\n * @param value - The constant value that would be returned on every invocation.\n * The value is not copied/cloned on every invocation so care should be taken\n * with mutable objects (like arrays, objects, Maps, etc...).\n * @signature\n * R.constant(value);\n * @example\n * R.map([1, 2, 3], R.constant('a')); // => ['a', 'a', 'a']\n * R.map(\n * [1, 2, 3],\n * isDemoMode ? R.add(1) : R.constant(0),\n * ); // => [2, 3, 4] or [0, 0, 0]\n * @dataLast\n * @category Function\n */\nexport function constant<const T>(\n value: T,\n): // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- There is no other way to make typescript infer the function arguments \"backwards\" in data-last invocations without the Args type parameter. @see: https://github.com/typescript-eslint/typescript-eslint/issues/9887\n<Args extends ReadonlyArray<unknown>>(...args: Args) => T {\n return () => value;\n}\n"],"mappings":"AA2BA,SAAgB,EACd,EAEwD,CACxD,UAAa"}