Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var arraySome = require('../internal/arraySome'),
2
baseCallback = require('../internal/baseCallback'),
3
baseSome = require('../internal/baseSome'),
4
isArray = require('../lang/isArray'),
5
isIterateeCall = require('../internal/isIterateeCall');
6
7
/**
8
* Checks if `predicate` returns truthy for **any** element of `collection`.
9
* The function returns as soon as it finds a passing value and does not iterate
10
* over the entire collection. The predicate is bound to `thisArg` and invoked
11
* with three arguments: (value, index|key, collection).
12
*
13
* If a property name is provided for `predicate` the created `_.property`
14
* style callback returns the property value of the given element.
15
*
16
* If a value is also provided for `thisArg` the created `_.matchesProperty`
17
* style callback returns `true` for elements that have a matching property
18
* value, else `false`.
19
*
20
* If an object is provided for `predicate` the created `_.matches` style
21
* callback returns `true` for elements that have the properties of the given
22
* object, else `false`.
23
*
24
* @static
25
* @memberOf _
26
* @alias any
27
* @category Collection
28
* @param {Array|Object|string} collection The collection to iterate over.
29
* @param {Function|Object|string} [predicate=_.identity] The function invoked
30
* per iteration.
31
* @param {*} [thisArg] The `this` binding of `predicate`.
32
* @returns {boolean} Returns `true` if any element passes the predicate check,
33
* else `false`.
34
* @example
35
*
36
* _.some([null, 0, 'yes', false], Boolean);
37
* // => true
38
*
39
* var users = [
40
* { 'user': 'barney', 'active': true },
41
* { 'user': 'fred', 'active': false }
42
* ];
43
*
44
* // using the `_.matches` callback shorthand
45
* _.some(users, { 'user': 'barney', 'active': false });
46
* // => false
47
*
48
* // using the `_.matchesProperty` callback shorthand
49
* _.some(users, 'active', false);
50
* // => true
51
*
52
* // using the `_.property` callback shorthand
53
* _.some(users, 'active');
54
* // => true
55
*/
56
function some(collection, predicate, thisArg) {
57
var func = isArray(collection) ? arraySome : baseSome;
58
if (thisArg && isIterateeCall(collection, predicate, thisArg)) {
59
predicate = null;
60
}
61
if (typeof predicate != 'function' || thisArg !== undefined) {
62
predicate = baseCallback(predicate, thisArg, 3);
63
}
64
return func(collection, predicate);
65
}
66
67
module.exports = some;
68
69