Files
lodash/takeWhile.js
2017-01-09 17:38:33 -08:00

32 lines
874 B
JavaScript

import baseWhile from './_baseWhile.js';
/**
* Creates a slice of `array` with elements taken from the beginning. Elements
* are taken until `predicate` returns falsey. The predicate is invoked with
* three arguments: (value, index, array).
*
* @static
* @since 3.0.0
* @category Array
* @param {Array} array The array to query.
* @param {Function} predicate The function invoked per iteration.
* @returns {Array} Returns the slice of `array`.
* @example
*
* var users = [
* { 'user': 'barney', 'active': false },
* { 'user': 'fred', 'active': false },
* { 'user': 'pebbles', 'active': true }
* ];
*
* takeWhile(users, function(o) { return !o.active; });
* // => objects for ['barney', 'fred']
*/
function takeWhile(array, predicate) {
return (array && array.length)
? baseWhile(array, predicate)
: [];
}
export default takeWhile;