TreasureTrails/node_modules/remeda/dist/when.cjs.map

1 line
6.4 KiB
Text
Raw Normal View History

2026-03-18 09:02:21 -05:00
{"version":3,"file":"when.cjs","names":[],"sources":["../src/when.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any --\n * function inference is stricter and doesn't work well when the arguments\n * aren't typed as `any` in the generic type declaration.\n */\n\nimport type { GuardType } from \"./internal/types/GuardType\";\n\n/**\n * Conditionally run a function based on a predicate, returning its result (similar to\n * the [`?:` (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator).)\n * If the optional `onFalse` function is not provided, the data will be passed\n * through in those cases.\n *\n * Supports type predicates to refine the types for both branches and the return\n * value.\n *\n * Additional arguments are passed to all functions. In data-first calls, they\n * are taken as variadic arguments; but in data-last calls, they are when the\n * curried function itself is called.\n *\n * For more complex cases check out `conditional`.\n *\n * @param predicate - Decides if the `onTrue` mapper should run or not. If it's\n * a type predicate it also narrows types for the mappers and the return value.\n * @param onTrue - Function to run when the predicate returns `true`.\n * @signature\n * when(predicate, onTrue)(data, ...extraArgs)\n * when(predicate, { onTrue, onFalse })(data, ...extraArgs)\n * @example\n * pipe(data, when(isNullish, constant(42)));\n * pipe(data, when((x) => x > 3, { onTrue: add(1), onFalse: multiply(2) }));\n * map(data, when(isNullish, (x, index) => x + index));\n * @dataLast\n * @category Function\n */\nexport function when<\n T,\n ExtraArgs extends any[],\n Predicate extends (data: T, ...extraArgs: ExtraArgs) => boolean,\n OnTrue extends (\n data: GuardType<Predicate, T>,\n ...extraArgs: ExtraArgs\n ) => unknown,\n>(\n predicate: Predicate,\n onTrue: OnTrue,\n): (\n data: T,\n ...extraArgs: ExtraArgs\n) => Exclude<T, GuardType<Predicate>> | ReturnType<OnTrue>;\nexport function when<\n T,\n ExtraArgs extends any[],\n Predicate extends (data: T, ...extraArgs: ExtraArgs) => boolean,\n OnTrue extends (\n data: GuardType<Predicate, T>,\n ...extraArgs: ExtraArgs\n ) => unknown,\n OnFalse extends (\n data: Exclude<T, GuardType<Predicate>>,\n ...extraArgs: ExtraArgs\n ) => unknown,\n>(\n predicate: Predicate,\n branches: {\n readonly onTrue: OnTrue;\n readonly onFalse: OnFalse;\n },\n): (\n data: T,\n ...extraArgs: ExtraArgs\n) => ReturnType<OnFalse> | ReturnType<OnTrue>;\n\n/**\n * Conditionally run a function based on a predicate, returning its result (similar to\n * the [`?:` (ternary) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator).)\n * If the optional `onFalse` function is not provided, the data will be passed\n * through in those cases.\n *\n * Supports type predicates to refine the types for both branches and the return\n * value.\n *\n * Additional arguments are passed to all functions. In data-first calls, they\n * are taken as variadic arguments; but in data-last calls, they are when the\n * curried function itself is called.\n *\n * For more complex cases check out `conditional`.\n *\n * @param data - The data to be passed to all functions, as the first param.\n * @param predicate - Decides if the `onTrue` mapper should run or not. If it's\n * a type predicate it also narrows types for the mappers and the return value.\n * @param onTrue - The function that would run when the predicate returns\n * `true`.\n * @param extraArgs - Additional arguments. These would be passed as is to the\n * `predicate`, `onTrue`, and `onFalse` functions.\n * @signature\n * when(data, predicate, onTrue, ...extraArgs)\n * when(data, predicate, { onTrue, onFalse }, ...extraArgs)\n * @example\n * when(data, isNullish, constant(42));\n * when(data, (x) => x > 3, { onTrue: add(1), onFalse: multiply(2) });\n * when(data, isString, (x, radix) => parseInt(x, radix), 10);\n * @dataFirst\n * @category Function\n