How to use the ifElse function from ramda

Find comprehensive JavaScript ramda.ifElse code examples handpicked from public code repositorys.

ramda.ifElse is a function in the Ramda library that conditionally applies one of two functions to a value based on a predicate.

73
74
75
76
77
78
79
80
81
82
 */
const renderTagsInlines = (width, options, tags) =>
  R.compose(
    R.flatten,
    R.map(
      R.ifElse(
        get(isTag)('example'),
        get(renderExampleTag)(R.__, width, options),
        get(renderTagInLine)(
          width,
fork icon1
star icon9
watch icon0

+ 2 other calls in file

375
376
377
378
379
380
381
382
383
384
      offerID = _ref23$offers[0];

  return R.mergeAll([hotels[hotelID], {
    offerID: offerID
  }]);
}, R.ifElse(R.prop('location'), function (_ref24) {
  var id = _ref24.id,
      location = _ref24.location,
      offerID = _ref24.offerID,
      stars = _ref24.stars;
fork icon0
star icon3
watch icon4

+ 41 other calls in file

How does ramda.ifElse work?

ramda.ifElse is a higher-order function that takes three arguments: a predicate function, a function to apply when the predicate returns true, and a function to apply when the predicate returns false. When called with a value, ramda.ifElse first applies the predicate function to that value. If the predicate returns true, ramda.ifElse applies the "if" function to the value and returns the result. If the predicate returns false, ramda.ifElse applies the "else" function to the value and returns the result. The ramda.ifElse function is useful for conditionally transforming data based on some criterion. It can be used to avoid writing conditional logic with if/else statements, and can make code more readable by separating the condition from the transformation. ramda.ifElse creates a new function that can be called with a value to apply the "if" or "else" function based on the predicate. This allows for easy composition with other Ramda functions, as well as with other functions that take functions as arguments.

4
5
6
7
8
9
10
11
12
13
14
  R.map(R.unary(R.prop)),
  R.allPass,
  R.ifElse(R.isNil, R.F)
)


module.exports.booleanFromString = R.ifElse(
  R.pipe(String, R.toLower, R.equals('true')),
  R.T,
  R.F
)
fork icon0
star icon3
watch icon0

64
65
66
67
68
69
70
71
72
73
  The result is wrapped in an Either monad.
  Signature:
  (version_getter_fn, decrypted_token) => Either(decrypted_token | TokenVersionError)
*/
const check_token_version = R.curry((versionGetter, version, token) => {
    return R.ifElse(
        R.either(
            R.compose(R.equals(version), versionGetter),
            R.compose(R.isNil, versionGetter)
        ),
fork icon1
star icon0
watch icon12

+ 629 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const R = require("ramda");

const isEven = (num) => num % 2 === 0;
const double = (num) => num * 2;
const decrement = (num) => num - 1;

const processNumber = R.ifElse(isEven, double, decrement);

console.log(processNumber(4)); // Output: 8
console.log(processNumber(5)); // Output: 4

In this example, processNumber is a new function created with ramda.ifElse. It takes a number as input, and applies the isEven predicate to determine whether to apply the double or decrement function. If the input number is even, processNumber applies the double function, otherwise it applies the decrement function. In this case, processNumber(4) returns 8, because 4 is even and double(4) equals 8. On the other hand, processNumber(5) returns 4, because 5 is odd and decrement(5) equals 4.

75
76
77
78
79
80
81
82
83
84
85


/*
    Signature:
        (key, Either(dict | Err)) => Either(dict | ConfigMalformatedError)
*/
const handle_json_config = (key, result) => R.ifElse(
    R.prop('isRight'),
    R.identity,
    () => Either.Left(new ConfigMalformatedError(key))
)(result)
fork icon1
star icon0
watch icon13

+ 361 other calls in file

2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
* @return {Function} A new unary function that will process either the `onTrue` or the `onFalse`
*                    function depending upon the result of the `condition` predicate.
* @see R.unless, R.when
* @example
*
*      var incCount = R.ifElse(
*        R.has('count'),
*        R.over(R.lensProp('count'), R.inc),
*        R.assoc('count', 1)
*      );
fork icon0
star icon0
watch icon0

+ 17 other calls in file

8
9
10
11
12
13
14
15
16
17
18
19
const reject = Promise.reject.bind(Promise);


const serialize = R.unary(JSON.stringify);
const deserialize = R.unary(JSON.parse);


const deserializeCacheValue = R.ifElse(
  R.not,
  R.always([]),
  deserialize
);
fork icon0
star icon0
watch icon0

87
88
89
90
91
92
93
94
95
96
97
const getDomainService = ({ cpanel }) => {
  const fetchZoneRecords = R.compose(then(R.map(zoneToRecord)), cpanel.zone.fetch);
  const fetchRedirections = R.compose(then(R.map(redirectionToRecord)), cpanel.redirection.fetch);


  const addZoneRecord = lazyTask(R.compose(
    R.ifElse(R.propEq('type', 'MX'),
      R.compose(cpanel.email.add, recordToEmailMx),
      cpanel.zone.add
    ),
    recordToZone,
fork icon0
star icon0
watch icon0

73
74
75
76
77
78
 * @function
 * @param {locale[]} locales
 * @param {string} day
 * @return {string}
 */
module.exports = ifElse(firstDayOrdinal, formatFirstDay, formatDay);
fork icon0
star icon0
watch icon0

101
102
103
104
105
106
107
108
109
110
  R.test(/^[a-z ]+$/i)
]))

let isValidMailAddress = R.pipe(R.prop("email"), R.allPass([isNotNil, R.contains("@")]))

let validateMailAddress = R.ifElse(isValidMailAddress, R.always(""), R.always("Invalid email address"))
let validateUserName = R.ifElse(isValidUserName, R.always(""), R.always("Invalid username"))

expect(isValidUserName(validName)).to.be.true
expect(isValidUserName({})).to.be.false
fork icon0
star icon0
watch icon0

+ 11 other calls in file

4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
* @return {Function} A new function that will process either the `onTrue` or the `onFalse`
*                    function depending upon the result of the `condition` predicate.
* @see R.unless, R.when, R.cond
* @example
*
*      const incCount = R.ifElse(
*        R.has('count'),
*        R.over(R.lensProp('count'), R.inc),
*        R.assoc('count', 1)
*      );
fork icon0
star icon0
watch icon2

+ 3 other calls in file

2
3
4
5
6
7
8
9
10
11
12
13
14


const content = R.split('\n', fs.readFileSync(`${__dirname}/input.txt`, {encoding: 'utf-8'}))


const parseInstruction = R.compose(
  R.drop(1),
  R.ifElse(R.compose(R.equals(2), R.length), R.over(R.lensIndex(1), R.compose(R.append(R.__, [R.identity]), R.add, Number)), R.append([R.identity])),
  R.split(' ')
)


const indices = R.map(R.compose(R.nth, R.dec), [20, 60, 100, 140, 180, 220])
fork icon0
star icon0
watch icon0

19
20
21
22
23
24
25
26
27
28
29
30
31


	return acc;
};


// Finding the first dupe
const findFirst = ( acc, x ) => R.ifElse( R.compose( R.contains( x ), R.tail ), R.compose( R.reduced, R.head ), R.tail)( acc );


const result = R.reduce(walk, { direction: 0, position: [ 0, 0 ], visits: [] }, input);
const firstDupe = R.reduce( findFirst, result.visits, result.visits );
const distance = R.sum(R.map(Math.abs, firstDupe.split(',')));
fork icon0
star icon0
watch icon0

56
57
58
59
60
61
62
63
64
65
negativeMasks = defaultTo([0])(negativeMasks);

const maskCheckPositiveFunc = userMask => gt(userMask & mask, 0);
const maskCheckNegativeFunc = userMask => equals(userMask & mask, 0);

const mandatoryMasksResult = ifElse(
    is(Object),
    userMasks => all(maskCheckPositiveFunc, userMasks),
    maskCheckPositiveFunc
)(mandatoryMasks);
fork icon0
star icon0
watch icon3

+ 5 other calls in file

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)