How to use the when function from ramda

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

39
40
41
42
43
44
45
46
47
48
}

/**
 * Appends authorization header when supplied with "user" option.
 */
const coerceUserOption = R.when(
  R.propSatisfies(R.complement(R.isNil), 'user'),
  R.compose(
    R.dissoc('user'),
    R.over(
fork icon303
star icon0
watch icon2

+ 9 other calls in file

72
73
74
75
76
77
78
79
80
    return R.zipWith(aEqB, ks, vs).join(separator);
  };

  const tableClause = tableSql(schema, table);
  const updateClause = sqlZip(',', update);
  const whereClause = R.when(Boolean, R.concat('WHERE '), sqlZip(' AND ', where));
  const queryName = options.returning ? '_update_returning' : '_update';
  return _prepare(sqlFormatter, queries, queryName, [tableClause, updateClause, whereClause]);
};
fork icon1
star icon6
watch icon0

+ 3 other calls in file

57
58
59
60
61
62
63
64
65
66
67
)


// toString :: Link -> String
const toString = R.compose(
  JSON.stringify,
  R.when(
    R.compose(R.equals(0), R.view(lens.attempts)),
    R.omit(['attempts'])
  ),
  R.filter(isUseful)
fork icon0
star icon4
watch icon0

154
155
156
157
158
159
160
161
162
163
164


exports.selectOperators = selectOperators;
var getSearchProgressByPercent = (0, _reselect.createSelector)(selectOperators(), function (operators) {
  return R.call(R.pipe(R.values, R.filter(Boolean), R.length, function (doneOperatorsCount) {
    return doneOperatorsCount * 100 / R.keys(operators).length;
  }, R.when(function (count) {
    return !count;
  }, R.always(0))), operators);
});
exports.getSearchProgressByPercent = getSearchProgressByPercent;
fork icon0
star icon3
watch icon4

+ 41 other calls in file

27
28
29
30
31
32
33
34
35
36
37
38
39
40
const incMoves = () => over("moves", limitMoves(inc));






const markSelected = id => assignBy(propEq('id', id), {selected: true})
const assignBy = (pred, obj) => when(pred, assign(obj));
const over = (key, fn) => modify(mapProps({ [key]: fn }));


const selectCard = id => over('cards', map(markSelected(id)))

fork icon0
star icon1
watch icon0

+ 3 other calls in file

431
432
433
434
435
436
437
438
439
440
441
 * @returns {Object[]} a possibly empty array of the `umm` properties of the
 *    `items` in the specified response object
 */
const transformUMMJSONResponse = R.pipe(
  R.tryCatch(JSON.parse, (_parseError, response) => { throw new Error(response) }),
  R.when(R.hasIn("items"), R.pipe(R.prop("items"), R.map(R.prop("umm")))),
);


/**
 * Response transformer used by {@link findConcepts} when called with the
fork icon1
star icon0
watch icon0

+ 4 other calls in file

3
4
5
6
7
8
9
10
11
12
if (filename) {
  const maxLength = 24;
  const extension = R.compose(R.last, R.split('.'))(filename);
  const filenameNoExt = R.compose(R.join('.'), R.dropLast(1), R.split('.'))(filename);

  const cleanFileName = R.when(
    R.compose(R.gte(R.__, maxLength), R.length),
    () => R.join('….', [R.slice(0, maxLength, filenameNoExt), extension])
  )(filename);
  return cleanFileName;
fork icon0
star icon0
watch icon0

5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
* @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'
fork icon0
star icon0
watch icon0

+ 17 other calls in file

26
27
28
29
30
31
32
33
34
35
36
    )(tree)  
})


const takeTrees = R.curry((takeFn, split) => R.compose(
    R.length, 
    R.when(R.compose(R.not, R.equals(R.length(split)), R.length), R.append(1)),
    takeFn
  )(split)
)

fork icon0
star icon0
watch icon0

+ 2 other calls in file

36
37
38
39
40
41
42
43
44
45
46
47
initGen(item_list)


const cloneGen = () => {};


const getOneAtRandom = () => candidates[Math.random() * 100 % candidates.length];
const mute1 = R.when(() => Math.random() - 0.95 > 0, getOneAtRandom());
const mute2 = () => {};
const fixPop = () => {};
const scoreOne = R.pipe(R.slice(0, 5), R.pluck('value'),)
const scorePop = R.pipe(R.map(scoreOne), R.sum);
fork icon0
star icon0
watch icon0

2
3
4
5
6
7
8
9
10
11
12
const panic = (error) => {
    throw error
}


const makeError = (reason) => new Error(`${reason}`)
const toError = when(complement(is(Error)), makeError)
const descriptionOrMessage = either(
    pick(['message', 'errors', 'data']),
    path(['props', 'description'])
)
fork icon0
star icon0
watch icon0

12
13
14
15
16
17
18
19
20
21
22
23


const bandsOnDate = (date) => {
  const month = moment.utc(date, 'YYYY-MM-DD');


  return R.compose(
    R.when(R.isNil, () => {
      throw noBandsError(date);
    }),
    R.prop('bands'),
    R.last,
fork icon0
star icon0
watch icon0

14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
* @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'
fork icon0
star icon0
watch icon2

+ 3 other calls in file

67
68
69
70
71
72
73
74
75
76
77
78
  R.prop('players'),
  R.reject(Player.isDead),
  R.length(),
  R.equals(1));


Game.checkOver = R.when(Game.isOver, R.set(State.lens, {id: State.GAME_OVER}));


Game.nextTurn = game => R.pipe(
  R.evolve({whoseTurn: turn => (turn + 1) % game.players.length}),
  R.set(State.lens, {id: State.START_OF_TURN})
fork icon0
star icon0
watch icon0

+ 3 other calls in file

10
11
12
13
14
15
16
17
18
19
  R.reject(R.isEmpty),
  R.split(','),
  R.pathOr('', ['params', 'services'])
)(req);

const maybeFilterByServices = R.when(
  () => services.length,
  R.filter(R.compose(
    R.includes(R.__, services),
    R.prop('serviceName')
fork icon0
star icon0
watch icon0

10
11
12
13
14
15
16
17
18
19
20
21
            [T, pipe(always(otherwise), reject)],
        ])(dispatchTable)
    })


const makeDefault = (fn) => assoc('default', fn, {})
const makeDispatchTable = when(pipe(is(Object), not), makeDefault)


module.exports = (dispatchTable) => {
    const callables = makeDispatchTable(dispatchTable)

fork icon0
star icon0
watch icon0

34
35
36
37
38
39
40
41
42
43

const series = R.compose(
  R.map(R.dropLast(1)),
  R.map(R.map(s => invert ? R.adjust(invertDecimal, 1, s) : s)),
  normalizeMax(granularity),
  R.when(R.always(cumulative), R.map(runningTotal)),
)(newSeries);

const mapIndexed = R.addIndex(R.map);
const fold = f => l => R.reduce(f, R.head(l), R.tail(l));
fork icon0
star icon0
watch icon1

Other functions in ramda

Sorted by popularity

function icon

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