Files
lodash/every.js
2017-04-15 23:08:47 -05:00

35 lines
1000 B
JavaScript

/**
* 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