-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathor.js
More file actions
185 lines (176 loc) · 5.21 KB
/
or.js
File metadata and controls
185 lines (176 loc) · 5.21 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const promiseAll = require('./_internal/promiseAll')
const isPromise = require('./_internal/isPromise')
const areAnyValuesPromises = require('./_internal/areAnyValuesPromises')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const curry3 = require('./_internal/curry3')
const curryArgs2 = require('./_internal/curryArgs2')
const thunkConditional = require('./_internal/thunkConditional')
const areAllValuesNonfunctions = require('./_internal/areAllValuesNonfunctions')
const thunkify2 = require('./_internal/thunkify2')
const thunkify3 = require('./_internal/thunkify3')
const always = require('./_internal/always')
/**
* @name areAnyNonfunctionsTruthy
*
* @synopsis
* ```coffeescript [specscript]
* areAnyNonfunctionsTruthy(predicates Array<value>) -> Promise|boolean
* ```
*/
const areAnyNonfunctionsTruthy = function (predicates, index) {
const length = predicates.length
while (++index < length) {
const predicate = predicates[index]
if (isPromise(predicate)) {
return predicate.then(curry3(
thunkConditional,
__,
always(true),
thunkify2(areAnyNonfunctionsTruthy, predicates, index),
))
}
if (predicate) {
return true
}
}
return false
}
/**
* @name asyncAreAnyPredicatesTruthy
*
* @synopsis
* ```coffeescript [specscript]
* asyncAreAnyPredicatesTruthy(
* args Array,
* predicates Array<predicate function|nonfunction>,
* index number,
* ) -> allTruthy boolean
* ```
*/
const asyncAreAnyPredicatesTruthy = async function (args, predicates, index) {
const length = predicates.length
while (++index < length) {
let predicate = predicates[index]
if (typeof predicate == 'function') {
predicate = predicate(...args)
}
if (isPromise(predicate)) {
predicate = await predicate
}
if (predicate) {
return true
}
}
return false
}
// areAnyPredicatesTruthy(args Array, predicates Array<function>) -> Promise|boolean
const areAnyPredicatesTruthy = function (args, predicates) {
const length = predicates.length
let index = -1
while (++index < length) {
let predicate = predicates[index]
if (typeof predicate == 'function') {
predicate = predicate(...args)
}
if (isPromise(predicate)) {
return predicate.then(curry3(
thunkConditional,
__,
always(true),
thunkify3(asyncAreAnyPredicatesTruthy, args, predicates, index),
))
}
if (predicate) {
return true
}
}
return false
}
/**
* @name or
*
* @synopsis
* ```coffeescript [specscript]
* args Array<any>
* argsOrPromises Array<Promise|any>
*
* type SyncOrAsyncPredicate = (...args)=>Promise|boolean|any
*
* predicatesOrValues Array<SyncOrAsyncPredicate|boolean|any>
*
* or(values Array<boolean|any>) -> result boolean
* or(...argsOrPromises, predicatesOrValues) -> Promise|boolean
* or(predicatesOrValues)(...args) -> Promise|boolean
* ```
*
* @description
* Function equivalent to the [Logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR) operator. Tests arrays of predicate functions, promises, values, or a mix thereof.
*
* If provided an array of boolean values, `or` returns true if any boolean values are truthy.
*
* ```javascript [playground]
* const oneIsLessThanZero = 1 < 0
* const oneIsGreaterThanTwo = 1 > 2
* const threeIsNotEqualToThree = 3 !== 3
*
* const condition = or([
* oneIsLessThanZero,
* oneIsGreaterThanTwo,
* threeIsNotEqualToThree
* ])
* console.log(condition) // false
* ```
*
* If any predicate functions are provided in the array, `or` returns an aggregate predicate function that returns true for a given set of arguments if any provided predicate functions test true. If any provided predicate functions are asynchronous, the aggregate predicate function becomes asynchronous.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
* const isNegative = number => number < 0
* const asyncIsGreaterThan3 = async number => number > 3
*
* const aggregatePredicate = or([
* false,
* isOdd,
* isNegative,
* asyncIsGreaterThan3,
* ])
*
* const condition = await aggregatePredicate(2)
* console.log(condition) // false
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* or(Promise.resolve('aaa'), [
* s => s.startsWith('b'),
* s => s.endsWith('a'),
* ]).then(console.log) // true
* ```
*
* See also:
* * [some](/docs/some)
* * [and](/docs/and)
* * [not](/docs/not)
* * [eq](/docs/eq)
*
* @execution series
*
* @note ...args slows down here by an order of magnitude
*/
const or = function (...args) {
const predicatesOrValues = args.pop()
if (areAllValuesNonfunctions(predicatesOrValues)) {
return areAnyNonfunctionsTruthy(predicatesOrValues, -1)
}
if (args.length == 0) {
return curryArgs2(areAnyPredicatesTruthy, __, predicatesOrValues)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args)
.then(curry2(areAnyPredicatesTruthy, __, predicatesOrValues))
}
return areAnyPredicatesTruthy(args, predicatesOrValues)
}
module.exports = or