@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
29 lines (28 loc) • 918 B
TypeScript
import { type Falsy, type Truthy } from '@augment-vir/assert';
/**
* Checks an input for truthiness then calls the respective callback, returning the callback's
* output.
*
* @category Function
* @category Package : @augment-vir/common
* @example
*
* ```ts
* import {ifTruthy} from '@augment-vir/common';
*
* const result1 = ifTruthy(
* true,
* () => 1,
* () => 2,
* ); // result1 is `1`
* const result2 = ifTruthy(
* false,
* () => 1,
* () => 2,
* ); // result2 is `2`
* ```
*
* @returns The called callback's output.
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export declare function ifTruthy<const InputType, IfTruthyType, IfFalsyType>(checkThis: InputType, ifTruthyCallback: (truthyInput: Truthy<InputType>) => IfTruthyType, ifFalsyCallback: (truthyInput: Falsy<InputType>) => IfFalsyType): IfTruthyType | IfFalsyType;