Files
lodash/partition.js
2017-01-09 17:38:33 -08:00

30 lines
1023 B
JavaScript

import createAggregator from './_createAggregator.js';
/**
* Creates an array of elements split into two groups, the first of which
* contains elements `predicate` returns truthy for, the second of which
* contains elements `predicate` returns falsey for. The predicate is
* invoked with one argument: (value).
*
* @static
* @since 3.0.0
* @category Collection
* @param {Array|Object} collection The collection to iterate over.
* @param {Function} predicate The function invoked per iteration.
* @returns {Array} Returns the array of grouped elements.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36, 'active': false },
* { 'user': 'fred', 'age': 40, 'active': true },
* { 'user': 'pebbles', 'age': 1, 'active': false }
* ];
*
* partition(users, function(o) { return o.active; });
* // => objects for [['fred'], ['barney', 'pebbles']]
*/
const partition = createAggregator((result, value, key) =>
result[key ? 0 : 1].push(value), () => [[], []]);
export default partition;