mirror of
https://github.com/swc-project/swc.git
synced 2024-12-20 20:22:26 +03:00
39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/when.js
|
||
|
|
||
|
|
||
|
import _curry3 from './internal/_curry3.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Tests the final argument by passing it to the given predicate function. If
|
||
|
* the predicate is satisfied, the function will return the result of calling
|
||
|
* the `whenTrueFn` function with the same argument. If the predicate is not
|
||
|
* satisfied, the argument is returned as is.
|
||
|
*
|
||
|
* @func
|
||
|
* @memberOf R
|
||
|
* @since v0.18.0
|
||
|
* @category Logic
|
||
|
* @sig (a -> Boolean) -> (a -> a) -> a -> a
|
||
|
* @param {Function} pred A predicate function
|
||
|
* @param {Function} whenTrueFn A function to invoke when the `condition`
|
||
|
* evaluates to a truthy value.
|
||
|
* @param {*} x An object to test with the `pred` function and
|
||
|
* pass to `whenTrueFn` if necessary.
|
||
|
* @return {*} Either `x` or the result of applying `x` to `whenTrueFn`.
|
||
|
* @see R.ifElse, R.unless, R.cond
|
||
|
* @example
|
||
|
*
|
||
|
* // truncate :: String -> String
|
||
|
* const truncate = R.when(
|
||
|
* R.propSatisfies(R.gt(R.__, 10), 'length'),
|
||
|
* R.pipe(R.take(10), R.append('…'), R.join(''))
|
||
|
* );
|
||
|
* truncate('12345'); //=> '12345'
|
||
|
* truncate('0123456789ABC'); //=> '0123456789…'
|
||
|
*/
|
||
|
var when = _curry3(function when(pred, whenTrueFn, x) {
|
||
|
return pred(x) ? whenTrueFn(x) : x;
|
||
|
});
|
||
|
export default when;
|