dropRightWhile.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var baseCallback = require('../internal/baseCallback'),
  2. baseWhile = require('../internal/baseWhile');
  3. /**
  4. * Creates a slice of `array` excluding elements dropped from the end.
  5. * Elements are dropped until `predicate` returns falsey. The predicate is
  6. * bound to `thisArg` and invoked with three arguments: (value, index, array).
  7. *
  8. * If a property name is provided for `predicate` the created `_.property`
  9. * style callback returns the property value of the given element.
  10. *
  11. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  12. * style callback returns `true` for elements that have a matching property
  13. * value, else `false`.
  14. *
  15. * If an object is provided for `predicate` the created `_.matches` style
  16. * callback returns `true` for elements that match the properties of the given
  17. * object, else `false`.
  18. *
  19. * @static
  20. * @memberOf _
  21. * @category Array
  22. * @param {Array} array The array to query.
  23. * @param {Function|Object|string} [predicate=_.identity] The function invoked
  24. * per iteration.
  25. * @param {*} [thisArg] The `this` binding of `predicate`.
  26. * @returns {Array} Returns the slice of `array`.
  27. * @example
  28. *
  29. * _.dropRightWhile([1, 2, 3], function(n) {
  30. * return n > 1;
  31. * });
  32. * // => [1]
  33. *
  34. * var users = [
  35. * { 'user': 'barney', 'active': true },
  36. * { 'user': 'fred', 'active': false },
  37. * { 'user': 'pebbles', 'active': false }
  38. * ];
  39. *
  40. * // using the `_.matches` callback shorthand
  41. * _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
  42. * // => ['barney', 'fred']
  43. *
  44. * // using the `_.matchesProperty` callback shorthand
  45. * _.pluck(_.dropRightWhile(users, 'active', false), 'user');
  46. * // => ['barney']
  47. *
  48. * // using the `_.property` callback shorthand
  49. * _.pluck(_.dropRightWhile(users, 'active'), 'user');
  50. * // => ['barney', 'fred', 'pebbles']
  51. */
  52. function dropRightWhile(array, predicate, thisArg) {
  53. return (array && array.length)
  54. ? baseWhile(array, baseCallback(predicate, thisArg, 3), true, true)
  55. : [];
  56. }
  57. module.exports = dropRightWhile;