mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Add partitionInitializer helper.
This commit is contained in:
30
lodash.js
30
lodash.js
@@ -463,6 +463,16 @@
|
|||||||
return typeof value.toString != 'function' && typeof (value + '') == 'string';
|
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
|
* A fallback implementation of `String#trim` to remove leading and trailing
|
||||||
* whitespace or specified characters from `string`.
|
* whitespace or specified characters from `string`.
|
||||||
@@ -1979,22 +1989,22 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function that aggregates a collection, creating an object or
|
* Creates a function that aggregates a collection, creating an accumulator
|
||||||
* array composed from the results of running each element in the collection
|
* 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
|
* 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
|
* @private
|
||||||
* @param {Function} setter The setter function.
|
* @param {Function} setter The function to set keys and values of the accumulator object.
|
||||||
* @param {boolean} [retArray=false] A flag to indicate that the aggregator
|
* @param {Function} [initializer] The function to initialize the accumulator object.
|
||||||
* function should return an array.
|
|
||||||
* @returns {Function} Returns the new aggregator function.
|
* @returns {Function} Returns the new aggregator function.
|
||||||
*/
|
*/
|
||||||
function createAggregator(setter, retArray) {
|
function createAggregator(setter, initializer) {
|
||||||
return function(collection, callback, thisArg) {
|
return function(collection, callback, thisArg) {
|
||||||
var result = retArray ? [[], []] : {};
|
var result = initializer ? initializer() : {};
|
||||||
|
|
||||||
callback = lodash.createCallback(callback, thisArg, 3);
|
callback = lodash.createCallback(callback, thisArg, 3);
|
||||||
|
|
||||||
if (isArray(collection)) {
|
if (isArray(collection)) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = collection.length;
|
length = collection.length;
|
||||||
@@ -4364,7 +4374,7 @@
|
|||||||
*/
|
*/
|
||||||
var partition = createAggregator(function(result, value, key) {
|
var partition = createAggregator(function(result, value, key) {
|
||||||
result[key ? 0 : 1].push(value);
|
result[key ? 0 : 1].push(value);
|
||||||
}, true);
|
}, partitionInitializer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the value of a specified property from all elements in the collection.
|
* Retrieves the value of a specified property from all elements in the collection.
|
||||||
|
|||||||
Reference in New Issue
Block a user