-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevery.js
104 lines (100 loc) · 3.18 KB
/
every.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
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
const isPromise = require('./_internal/isPromise')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const isArray = require('./_internal/isArray')
const arrayEvery = require('./_internal/arrayEvery')
const iteratorEvery = require('./_internal/iteratorEvery')
const asyncIteratorEvery = require('./_internal/asyncIteratorEvery')
const objectValues = require('./_internal/objectValues')
const reducerEvery = require('./_internal/reducerEvery')
const symbolIterator = require('./_internal/symbolIterator')
const symbolAsyncIterator = require('./_internal/symbolAsyncIterator')
// _every(collection Array|Iterable|AsyncIterable|{ reduce: function }|Object, predicate function) -> Promise|boolean
const _every = function (collection, predicate) {
if (isArray(collection)) {
return arrayEvery(collection, predicate)
}
if (collection == null) {
return predicate(collection)
}
if (typeof collection[symbolIterator] == 'function') {
return iteratorEvery(collection[symbolIterator](), predicate)
}
if (typeof collection[symbolAsyncIterator] == 'function') {
return asyncIteratorEvery(
collection[symbolAsyncIterator](), predicate, new Set()
)
}
if (typeof collection.reduce == 'function') {
return collection.reduce(reducerEvery(predicate), true)
}
if (collection.constructor == Object) {
return arrayEvery(objectValues(collection), predicate)
}
return predicate(collection)
}
/**
* @name every
*
* @synopsis
* ```coffeescript [specscript]
* type Foldable = Array|Iterable|AsyncIterable|{ reduce: function }|Object
*
* every(collection Foldable, predicate function) -> result Promise|boolean
*
* every(predicate function)(collection Foldable) -> result Promise|boolean
* ```
*
* @description
* Test a predicate concurrently across all items of a collection, returning true if all predications are truthy.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* console.log(
* every([1, 2, 3, 4, 5], isOdd),
* ) // false
*
* console.log(
* every([1, 3, 5], isOdd),
* ) // true
* ```
*
* The collection can be any iterable, async iterable, or object values iterable collection. Below is an example of `every` accepting an async generator as the collection.
*
* ```javascript [playground]
* const asyncNumbers = async function* () {
* yield 1; yield 2; yield 3; yield 4; yield 5
* }
*
* every(asyncNumbers(), async number => number < 6).then(console.log) // true
* ```
*
* `every` supports a lazy API for composability.
*
* ```javascript [playground]
* pipe([1, 2, 3], [
* every(number => number < 5),
* console.log, // true
* ])
* ```
*
* 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]
* every(Promise.resolve([1, 2, 3, 4, 5]), n => n < 6).then(console.log) // true
* ```
*
* @execution concurrent
*
* @muxing
*/
const every = function (arg0, arg1) {
if (typeof arg0 == 'function') {
return curry2(_every, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_every, __, arg1))
: _every(arg0, arg1)
}
module.exports = every