How to use the defaultTo function from ramda

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

ramda.defaultTo is a function that returns its first argument if it is not null or undefined, otherwise it returns its second argument.

43
44
45
46
47
48
49
50
51
52
53
  "none",
];
const colorSort = R.compose(
  tileColors.indexOf.bind(tileColors),
  R.prop("color"),
  R.defaultTo({ color: "other" })
);
const sortTiles = R.sortWith([R.ascend(colorSort)]);


setup();
fork icon52
star icon51
watch icon0

51
52
53
54
55
56
57
58
59
60
	valueProp,
	R.head,
	defaultTo('')
),
deprecated: pipe(prop('deprecated'), defaultTo('')),
description: pipe(prop('description'), R.defaultTo(''), marked),
example: pipe(prop('examples'), R.defaultTo(''), prettifyCode),
name: pipe(prop('name'), defaultTo('')),
params: pipe(
	prop('params'),
fork icon24
star icon163
watch icon0

How does ramda.defaultTo work?

ramda.defaultTo is a function that returns its second argument if the first argument is null or undefined, and the first argument otherwise. If the first argument is any other falsy value (e.g. 0, false, NaN, ''), it will return that falsy value instead.

13
14
15
16
17
18
19
20
21
22
23


/** Private */
const setDefaults = (eventData) => {
  const setDefaultProps = {
    status: R.defaultTo(eventStatuses.NOT_STARTED),
    archiveEvent: R.defaultTo(false),
    uncomposed: R.defaultTo(false),
  };
  return R.evolve(setDefaultProps, eventData);
};
fork icon12
star icon5
watch icon0

+ 2 other calls in file

15
16
17
18
19
20
21
22
23
24
25
  R.map(([k, _]) => [k, R.lensProp(k)]),
  R.toPairs
)(defaults)


const mergeString = R.unapply(R.compose(
  R.defaultTo(''),
  R.head(),
  R.reject(isUseless),
  R.reverse,
  R.drop(1)
fork icon0
star icon4
watch icon0

Ai Example

1
2
3
4
5
6
const ramda = require("ramda");

const foo = null;
const bar = ramda.defaultTo("default value", foo);

console.log(bar); // Output: 'default value'

In this example, ramda.defaultTo is used to provide a default value for the foo variable, which is null. Since foo is falsy, the value of bar will be the default value of 'default value'.

32
33
34
35
36
37
38
39
40
41
42
const takeSecondPartOfString = (index) => (arr) => R.pipe(
  take(index),
  R.propOr('', 'str'),
  R.split(':'),
  R.nth(1),
  R.defaultTo(''), // in case nothing is found, use a blank string
  R.trim,
)(arr);


const extractTextBlocks = (includedPages) => async (doc) => {
fork icon0
star icon1
watch icon0

109
110
111
112
113
114
115
116
117
118
119
120
export function isNotNil(obj) {
  return !R.isNil(obj);
}


export function setDefaultTo(defaultv = [], obj) {
  return R.defaultTo(defaultv, obj);
}


export function findfromArray(val, array = []) {
  let selectedDetail = {};
fork icon1
star icon0
watch icon0

+ 3 other calls in file

127
128
129
130
131
132
133
134
135
136
}, candidateTransactions);

console.info(`Selected ${selectedTransactions.length} candidate transactions with ${rejectedTransactions.length} being rejected.`);

// Get the first two avaliable transactions, if there aren't TRANSACTIONS_PER_BLOCK, it's empty
let transactions = R.defaultTo([], R.take(Config.TRANSACTIONS_PER_BLOCK, selectedTransactions));

// Add fee transaction (1 satoshi per transaction)        
if (transactions.length > 0) {
    let feeTransaction = Transaction.fromJson({
fork icon0
star icon1
watch icon0

36
37
38
39
40
41
42
43
44
45
function connect() {
  return new Promise(function (resolve, reject) {
    if (myDb === undefined) {
      console.log('[connect] - new connection')
      console.log('NODE ENV: %s', process.env.NODE_ENV);
      const env = R.defaultTo('dev', process.env.NODE_ENV);
      console.log('Environment: %s', env);
      const database = config.mongoURI[env];
      console.log('DATABASE: %s', database);
      const url = (env === 'dev' || env === 'automated_test') ? f('mongodb://%s', database) : connectionURL(env, database, config);
fork icon0
star icon0
watch icon1

1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
* @param {a} val The default value.
* @param {b} val The value to return if it is not null or undefined
* @return {*} The the second value or the default value
* @example
*
*      var defaultTo42 = R.defaultTo(42);
*
*      defaultTo42(null);  //=> 42
*      defaultTo42(undefined);  //=> 42
*      defaultTo42('Ramda');  //=> 'Ramda'
fork icon0
star icon0
watch icon0

+ 17 other calls in file

2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
* @param {a} default The default value.
* @param {b} val `val` will be returned instead of `default` unless `val` is `null`, `undefined` or `NaN`.
* @return {*} The second value if it is not `null`, `undefined` or `NaN`, otherwise the default value
* @example
*
*      const defaultTo42 = R.defaultTo(42);
*
*      defaultTo42(null);  //=> 42
*      defaultTo42(undefined);  //=> 42
*      defaultTo42(false);  //=> false
fork icon0
star icon0
watch icon2

+ 3 other calls in file

51
52
53
54
55
56
57
58
59
    return false;
}

mandatoryMasks = defaultTo([1])(mandatoryMasks);
additionalMasks = defaultTo([1])(additionalMasks);
negativeMasks = defaultTo([0])(negativeMasks);

const maskCheckPositiveFunc = userMask => gt(userMask & mask, 0);
const maskCheckNegativeFunc = userMask => equals(userMask & mask, 0);
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)