mirror of https://github.com/lukechilds/docs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.3 KiB
34 lines
1.3 KiB
var _curry3 = require('./internal/_curry3');
|
|
|
|
|
|
/**
|
|
* 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
|
|
* @example
|
|
*
|
|
* // truncate :: String -> String
|
|
* var 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…'
|
|
*/
|
|
module.exports = _curry3(function when(pred, whenTrueFn, x) {
|
|
return pred(x) ? whenTrueFn(x) : x;
|
|
});
|
|
|