forked from a-synchronous/rubico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gte.js
76 lines (73 loc) · 2.38 KB
/
gte.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const spread2 = require('./_internal/spread2')
const isPromise = require('./_internal/isPromise')
const promiseAll = require('./_internal/promiseAll')
const greaterThanOrEqual = require('./_internal/greaterThanOrEqual')
const curry2 = require('./_internal/curry2')
const always = require('./_internal/always')
const __ = require('./_internal/placeholder')
/**
* @name gte
*
* @synopsis
* ```coffeescript [specscript]
* var value any,
* leftCompare any,
* rightCompare any,
* left (value=>Promise|leftCompare)|leftCompare,
* right (value=>Promise|rightCompare)|rightCompare
*
* gte(left, right)(value) -> Promise|boolean
* ```
*
* @description
* Test if a left value is greater than or equal (`>=`) to a right value. Either parameter may be an actual value.
*
* ```javascript [playground]
* const identity = value => value
*
* const isAtLeast100 = gte(identity, 100)
*
* console.log(isAtLeast100(99)) // false
* console.log(isAtLeast100(100)) // true
* console.log(isAtLeast100(101)) // true
* ```
*/
const gte = function (left, right) {
const isLeftResolver = typeof left == 'function',
isRightResolver = typeof right == 'function'
if (isLeftResolver && isRightResolver) {
return function greaterThanOrEqualBy(value) {
const leftResolve = left(value),
rightResolve = right(value)
const isLeftPromise = isPromise(leftResolve),
isRightPromise = isPromise(rightResolve)
if (isLeftPromise && isRightPromise) {
return promiseAll(
[leftResolve, rightResolve]).then(spread2(greaterThanOrEqual))
} else if (isLeftPromise) {
return leftResolve.then(curry2(greaterThanOrEqual, __, rightResolve))
} else if (isRightPromise) {
return rightResolve.then(curry2(greaterThanOrEqual, leftResolve, __))
}
return leftResolve >= rightResolve
}
}
if (isLeftResolver) {
return function greaterThanOrEqualBy(value) {
const leftResolve = left(value)
return isPromise(leftResolve)
? leftResolve.then(curry2(greaterThanOrEqual, __, right))
: leftResolve >= right
}
}
if (isRightResolver) {
return function strictEqualBy(value) {
const rightResolve = right(value)
return isPromise(rightResolve)
? rightResolve.then(curry2(greaterThanOrEqual, left, __))
: left >= rightResolve
}
}
return always(left >= right)
}
module.exports = gte