forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheither.ts
More file actions
25 lines (24 loc) · 881 Bytes
/
either.ts
File metadata and controls
25 lines (24 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { either } from '../typings/types';
import curryN from './curryN';
/**
* A function wrapping calls to the two functions in an `||` operation,
* returning the result of the first function if it is truth-y and the result
* of the second function otherwise. Note that this is short-circuited,
* meaning that the second function will not be invoked if the first returns a
* truth-y value.
*
* @param {Function} f a predicate
* @param {Function} g another predicate
* @return {Function} a function that applies its arguments to `f` and `g` and `||`s their outputs together.
* @example
*
* var gt10 = x => x > 10;
* var even = x => x % 2 === 0;
* var f = either(gt10, even);
* f(101); //=> true
* f(8); //=> true
* f(3); //=> false
*/
export default curryN(2, (f, g) =>
(...args) => f(...args) || g(...args)
) as typeof either