forked from a-synchronous/rubico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lte.js
76 lines (73 loc) · 2.35 KB
/
lte.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 lessThanOrEqual = require('./_internal/lessThanOrEqual')
const curry2 = require('./_internal/curry2')
const always = require('./_internal/always')
const __ = require('./_internal/placeholder')
/**
* @name lte
*
* @synopsis
* ```coffeescript [specscript]
* var value any,
* leftCompare any,
* rightCompare any,
* left (value=>Promise|leftCompare)|leftCompare,
* right (value=>Promise|rightCompare)|rightCompare
*
* lte(left, right)(value) -> Promise|boolean
* ```
*
* @description
* Test if a left value is less than or equal (`<=`) to a right value. Either parameter may be an actual value.
*
* ```javascript [playground]
* const identity = value => value
*
* const isLessThanOrEqualTo3 = lte(identity, 3)
*
* console.log(isLessThanOrEqualTo3(1), true)
* console.log(isLessThanOrEqualTo3(3), true)
* console.log(isLessThanOrEqualTo3(5), false)
* ```
*/
const lte = function (left, right) {
const isLeftResolver = typeof left == 'function',
isRightResolver = typeof right == 'function'
if (isLeftResolver && isRightResolver) {
return function lessThanBy(value) {
const leftResolve = left(value),
rightResolve = right(value)
const isLeftPromise = isPromise(leftResolve),
isRightPromise = isPromise(rightResolve)
if (isLeftPromise && isRightPromise) {
return promiseAll(
[leftResolve, rightResolve]).then(spread2(lessThanOrEqual))
} else if (isLeftPromise) {
return leftResolve.then(curry2(lessThanOrEqual, __, rightResolve))
} else if (isRightPromise) {
return rightResolve.then(curry2(lessThanOrEqual, leftResolve, __))
}
return leftResolve <= rightResolve
}
}
if (isLeftResolver) {
return function lessThanBy(value) {
const leftResolve = left(value)
return isPromise(leftResolve)
? leftResolve.then(curry2(lessThanOrEqual, __, right))
: leftResolve <= right
}
}
if (isRightResolver) {
return function strictEqualBy(value) {
const rightResolve = right(value)
return isPromise(rightResolve)
? rightResolve.then(curry2(lessThanOrEqual, left, __))
: left <= rightResolve
}
}
return always(left <= right)
}
module.exports = lte