-
Notifications
You must be signed in to change notification settings - Fork 5
/
every.js
34 lines (32 loc) · 1000 Bytes
/
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
/**
* Checks if `predicate` returns truthy for **all** elements of `array`.
* Iteration is stopped once `predicate` returns falsey. The predicate is
* invoked with three arguments: (value, index, array).
*
* **Note:** This method returns `true` for
* [empty arrays](https://en.wikipedia.org/wiki/Empty_set) because
* [everything is true](https://en.wikipedia.org/wiki/Vacuous_truth) of
* elements of empty arrays.
*
* @since 5.0.0
* @category Array
* @param {Array} array The array to iterate over.
* @param {Function} predicate The function invoked per iteration.
* @returns {boolean} Returns `true` if all elements pass the predicate check,
* else `false`.
* @example
*
* every([true, 1, null, 'yes'], Boolean)
* // => false
*/
function every(array, predicate) {
let index = -1
const length = array == null ? 0 : array.length
while (++index < length) {
if (!predicate(array[index], index, array)) {
return false
}
}
return true
}
export default every