UNPKG

1.39 kBJavaScriptView Raw
1import baseIteratee from './_baseIteratee';
2import baseWhile from './_baseWhile';
3
4/**
5 * Creates a slice of `array` with elements taken from the end. Elements are
6 * taken until `predicate` returns falsey. The predicate is invoked with
7 * three arguments: (value, index, array).
8 *
9 * @static
10 * @memberOf _
11 * @since 3.0.0
12 * @category Array
13 * @param {Array} array The array to query.
14 * @param {Array|Function|Object|string} [predicate=_.identity]
15 * The function invoked per iteration.
16 * @returns {Array} Returns the slice of `array`.
17 * @example
18 *
19 * var users = [
20 * { 'user': 'barney', 'active': true },
21 * { 'user': 'fred', 'active': false },
22 * { 'user': 'pebbles', 'active': false }
23 * ];
24 *
25 * _.takeRightWhile(users, function(o) { return !o.active; });
26 * // => objects for ['fred', 'pebbles']
27 *
28 * // The `_.matches` iteratee shorthand.
29 * _.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
30 * // => objects for ['pebbles']
31 *
32 * // The `_.matchesProperty` iteratee shorthand.
33 * _.takeRightWhile(users, ['active', false]);
34 * // => objects for ['fred', 'pebbles']
35 *
36 * // The `_.property` iteratee shorthand.
37 * _.takeRightWhile(users, 'active');
38 * // => []
39 */
40function takeRightWhile(array, predicate) {
41 return (array && array.length)
42 ? baseWhile(array, baseIteratee(predicate, 3), false, true)
43 : [];
44}
45
46export default takeRightWhile;