UNPKG

1.43 kBJavaScriptView Raw
1import baseIteratee from './_baseIteratee';
2import baseWhile from './_baseWhile';
3
4/**
5 * Creates a slice of `array` excluding elements dropped from the end.
6 * Elements are dropped until `predicate` returns falsey. The predicate is
7 * invoked with 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 * _.dropRightWhile(users, function(o) { return !o.active; });
26 * // => objects for ['barney']
27 *
28 * // The `_.matches` iteratee shorthand.
29 * _.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
30 * // => objects for ['barney', 'fred']
31 *
32 * // The `_.matchesProperty` iteratee shorthand.
33 * _.dropRightWhile(users, ['active', false]);
34 * // => objects for ['barney']
35 *
36 * // The `_.property` iteratee shorthand.
37 * _.dropRightWhile(users, 'active');
38 * // => objects for ['barney', 'fred', 'pebbles']
39 */
40function dropRightWhile(array, predicate) {
41 return (array && array.length)
42 ? baseWhile(array, baseIteratee(predicate, 3), true, true)
43 : [];
44}
45
46export default dropRightWhile;