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