JAVASCRIPT   10

dropRightWhile.js

Guest on 11th June 2021 03:46:37 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.