Add partitionInitializer helper.

This commit is contained in:
John-David Dalton
2014-04-11 23:23:20 -07:00
parent b3f5375283
commit 0243e7ba8b

View File

@@ -463,6 +463,16 @@
return typeof value.toString != 'function' && typeof (value + '') == 'string';
}
/**
* Used by `_.partition` to create partitioned arrays.
*
* @private
* @returns {Array} Returns the new array.
*/
function partitionInitializer() {
return [[], []];
}
/**
* A fallback implementation of `String#trim` to remove leading and trailing
* whitespace or specified characters from `string`.
@@ -1979,22 +1989,22 @@
}
/**
* Creates a function that aggregates a collection, creating an object or
* array composed from the results of running each element in the collection
* Creates a function that aggregates a collection, creating an accumulator
* object composed from the results of running each element in the collection
* through a callback. The given setter function sets the keys and values of
* the composed object or array.
* the accumulator object. If `initializer` is provided will be used to
* initialize the accumulator object.
*
* @private
* @param {Function} setter The setter function.
* @param {boolean} [retArray=false] A flag to indicate that the aggregator
* function should return an array.
* @param {Function} setter The function to set keys and values of the accumulator object.
* @param {Function} [initializer] The function to initialize the accumulator object.
* @returns {Function} Returns the new aggregator function.
*/
function createAggregator(setter, retArray) {
function createAggregator(setter, initializer) {
return function(collection, callback, thisArg) {
var result = retArray ? [[], []] : {};
var result = initializer ? initializer() : {};
callback = lodash.createCallback(callback, thisArg, 3);
if (isArray(collection)) {
var index = -1,
length = collection.length;
@@ -4364,7 +4374,7 @@
*/
var partition = createAggregator(function(result, value, key) {
result[key ? 0 : 1].push(value);
}, true);
}, partitionInitializer);
/**
* Retrieves the value of a specified property from all elements in the collection.