How to use the either function from ramda

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

ramda.either returns a function that returns the result of the first function if the argument passes the condition, otherwise returns the result of the second function.

12
13
14
15
16
17
18
19
20
21
22
23
  [R.T, R.always(DEFAULT_TTL)],
]);


const pathIsString = R.pathSatisfies(R.is(String));


const containsValidRecipients = R.either(
  pathIsString(['recipients', 'to']),
  pathIsString(['recipients', 'condition'])
);

fork icon117
star icon518
watch icon0

77
78
79
80
81
82
83
84
85
86
87
88
)
` : sql``}
`;


// should be moved to util.js or we already have similar func somewhere?
const isNilOrEmpty = either(isNil, isEmpty);


const _createOrMerge = (dataset, fields, acteeId, publish) => sql`
${_insertDatasetDef(dataset, acteeId, true, publish)}
${isNilOrEmpty(fields) ? sql`` : _insertProperties(fields, publish)}
fork icon50
star icon43
watch icon9

+ 59 other calls in file

How does ramda.either work?

ramda.either is a higher-order function that takes two functions as arguments and returns a new function. The returned function takes an input and returns the result of applying one of the two functions to the input based on whether the input satisfies a given predicate function. If the predicate function returns a truthy value, the first function is applied to the input, otherwise the second function is applied.

105
106
107
108
109
110
111
112
113
114

```
var gt10 = x => x > 10;
var even = x => x % 2 === 0;

var f = R.either(gt10, even);
f(101) // true
f(8) // true
```
`both`:接受两个函数作为参数,只有它们都返回`true`,才返回`true`,否则返回`false`,相当于&&运算。
fork icon0
star icon1
watch icon0

70
71
72
73
74
75
76
77
78
79
    ],
    [R.equals('error'), R.always('error')],
    [R.equals('silent'), R.always('silent')],
    [R.T, R.always('warn')],
  ]),
  R.either(R.prop('l'), R.prop('level'))
);

/**
 * Coerces the given deprecated value of the "level" option
fork icon303
star icon0
watch icon2

+ 3 other calls in file

Ai Example

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

// create two functions to pass to `ramda.either`
const isEven = (n) => n % 2 === 0;
const isGreaterThanTen = (n) => n > 10;

// create a function that checks if a number is even or greater than 10
const checkNumber = R.either(isEven, isGreaterThanTen);

console.log(checkNumber(8)); // true
console.log(checkNumber(13)); // true
console.log(checkNumber(7)); // false

In this example, ramda.either is used to create a new function called checkNumber. This function takes a number as an argument and returns true if the number is even or greater than 10, and false otherwise. The ramda.either function takes two functions as arguments and returns a new function that will return true if either of the original functions return true.

65
66
67
68
69
70
71
72
73
74
  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)
        ),
        Either.Right,
fork icon1
star icon0
watch icon12

+ 419 other calls in file

201
202
203
204
205
206
207
208
209
210
  "Content-Type": "application/echo10+xml",
  "Echo-Token": process.env.CMR_ECHO_TOKEN || "",
},
raxConfig: {
  retry: 6,
  shouldRetry: R.either(isParentChangeError, RAX.shouldRetryRequest),
  onRetryAttempt: (error) => {
    if (isParentChangeError(error)) {
      console.log(`Changing parent collection for ${granuleUR}`);
      return unpublishGranule(granuleUR);
fork icon1
star icon0
watch icon0

3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
* @category Logic
* @sig * -> * -> *
* @param {Boolean} a A boolean value
* @param {Boolean} b A boolean value
* @return {Boolean} `true` if one or both arguments are `true`, `false` otherwise
* @see R.either
* @example
*
*      R.or(true, true); //=> true
*      R.or(true, false); //=> true
fork icon0
star icon0
watch icon0

+ 35 other calls in file

18
19
20
21
22
23
24
25
26
27
28


const isVisible = R.curry((tree, index) => {
  const [x,y] = indexToCoordinates(index)
  const horizontalSplits = splits(x, row(forest, y))
  const verticalSplits = splits(y, column(forest, x))
  return R.either(
      isVisibleFromEitherDirection(horizontalSplits), 
      isVisibleFromEitherDirection(verticalSplits)
    )(tree)  
})
fork icon0
star icon0
watch icon0

+ 2 other calls in file

3
4
5
6
7
8
9
10
11
12
13
    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'])
)
const conditionOf = pathOr('', ['props', 'condition'])
fork icon0
star icon0
watch icon0

3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
* @see R.or
* @example
*
*      const gt10 = x => x > 10;
*      const even = x => x % 2 === 0;
*      const f = R.either(gt10, even);
*      f(101); //=> true
*      f(8); //=> true
*
*      R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
fork icon0
star icon0
watch icon2

+ 11 other calls in file

15
16
17
18
19
20
21
22
23
24
25
26
const CONCURRENCY = 3; // is this ok ?
if (process.env.debug) {
  curlirize(axiosRaw);
}


const isNilOrEmpty = R.either(R.isNil, R.isEmpty);


const getHeaders = ({ apiKey }) => ({
  'X-API-KEY': apiKey,
  'X-API-VERSION': '2020-05-04',
fork icon0
star icon0
watch icon0

Other functions in ramda

Sorted by popularity

function icon

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