You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB

4 years ago
  1. var createAggregator = require('./_createAggregator');
  2. /**
  3. * Creates an array of elements split into two groups, the first of which
  4. * contains elements `predicate` returns truthy for, the second of which
  5. * contains elements `predicate` returns falsey for. The predicate is
  6. * invoked with one argument: (value).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.0.0
  11. * @category Collection
  12. * @param {Array|Object} collection The collection to iterate over.
  13. * @param {Function} [predicate=_.identity] The function invoked per iteration.
  14. * @returns {Array} Returns the array of grouped elements.
  15. * @example
  16. *
  17. * var users = [
  18. * { 'user': 'barney', 'age': 36, 'active': false },
  19. * { 'user': 'fred', 'age': 40, 'active': true },
  20. * { 'user': 'pebbles', 'age': 1, 'active': false }
  21. * ];
  22. *
  23. * _.partition(users, function(o) { return o.active; });
  24. * // => objects for [['fred'], ['barney', 'pebbles']]
  25. *
  26. * // The `_.matches` iteratee shorthand.
  27. * _.partition(users, { 'age': 1, 'active': false });
  28. * // => objects for [['pebbles'], ['barney', 'fred']]
  29. *
  30. * // The `_.matchesProperty` iteratee shorthand.
  31. * _.partition(users, ['active', false]);
  32. * // => objects for [['barney', 'pebbles'], ['fred']]
  33. *
  34. * // The `_.property` iteratee shorthand.
  35. * _.partition(users, 'active');
  36. * // => objects for [['fred'], ['barney', 'pebbles']]
  37. */
  38. var partition = createAggregator(function(result, value, key) {
  39. result[key ? 0 : 1].push(value);
  40. }, function() { return [[], []]; });
  41. module.exports = partition;