UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

36 lines (35 loc) 861 B
import { check } 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 function ifTruthy(checkThis, ifTruthyCallback, ifFalsyCallback) { if (check.isTruthy(checkThis)) { return ifTruthyCallback(checkThis); } else { return ifFalsyCallback(checkThis); } }