-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsome.js
More file actions
109 lines (106 loc) · 3.12 KB
/
some.js
File metadata and controls
109 lines (106 loc) · 3.12 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
const isPromise = require('./_internal/isPromise')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const isArray = require('./_internal/isArray')
const objectValues = require('./_internal/objectValues')
const arraySome = require('./_internal/arraySome')
const iteratorSome = require('./_internal/iteratorSome')
const asyncIteratorSome = require('./_internal/asyncIteratorSome')
const reducerSome = require('./_internal/reducerSome')
const symbolIterator = require('./_internal/symbolIterator')
const symbolAsyncIterator = require('./_internal/symbolAsyncIterator')
// _some(collection Array|Iterable|AsyncIterable|{ reduce: function }|Object, predicate function) -> Promise|boolean
const _some = function (collection, predicate) {
if (isArray(collection)) {
return arraySome(collection, predicate)
}
if (collection == null) {
return predicate(collection)
}
if (typeof collection[symbolIterator] == 'function') {
return iteratorSome(collection[symbolIterator](), predicate)
}
if (typeof collection[symbolAsyncIterator] == 'function') {
return asyncIteratorSome(
collection[symbolAsyncIterator](), predicate, new Set()
)
}
if (typeof collection.reduce == 'function') {
return collection.reduce(reducerSome(predicate), false)
}
if (collection.constructor == Object) {
return arraySome(objectValues(collection), predicate)
}
return predicate(collection)
}
/**
* @name some
*
* @synopsis
* ```coffeescript [specscript]
* type Foldable = Array|Set|Map|Generator|AsyncGenerator|{ reduce: function }|Object
* type UnarySyncOrAsyncPredicate = any=>Promise|boolean
*
* predicate UnarySyncOrAsyncPredicate
*
* some(foldable Promise|Foldable, predicate) -> testResult Promise|boolean
* some(predicate)(foldable Foldable) -> testResult Promise|boolean
* ```
*
* @description
* Tests a predicate concurrently across all items of a foldable. Returns true if any item tests true by the predicate.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* const array = [1, 2, 3, 4, 5]
*
* const arrayHasOddNumbers = some(array, isOdd)
*
* console.log(arrayHasOddNumbers)
* ```
*
* The following data types are considered to be foldables:
* * `array`
* * `set`
* * `map`
* * `generator`
* * `async generator`
* * `object with .reduce method`
* * `object`
*
* `some` supports a lazy interface for composability.
*
* ```javascript [playground]
* pipe([1, 2, 3], [
* some(number => number < 5),
* console.log,
* ])
* ```
*
* If the foldable is a promise, it is resolved for its value before further execution for the eager interface only.
*
* ```javascript [playground]
* some(Promise.resolve([1, 2, 3, 4, 5]), n => n > 6).then(console.log)
* ```
*
* See also:
* * [map](/docs/map)
* * [every](/docs/every)
* * [and](/docs/and)
*
* @execution concurrent
*
* @muxing
*
* @related or
*/
const some = function (arg0, arg1) {
if (typeof arg0 == 'function') {
return curry2(_some, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_some, __, arg1))
: _some(arg0, arg1)
}
module.exports = some