mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 16:47:49 +00:00
Consolidate aggregate modules.
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
/**
|
||||
* A specialized version of `baseAggregator` for arrays.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} [array] The array to iterate over.
|
||||
* @param {Function} setter The function to set `accumulator` values.
|
||||
* @param {Function} iteratee The iteratee to transform keys.
|
||||
* @param {Object} accumulator The initial aggregated object.
|
||||
* @returns {Function} Returns `accumulator`.
|
||||
*/
|
||||
function arrayAggregator(array, setter, iteratee, accumulator) {
|
||||
let index = -1;
|
||||
const length = array == null ? 0 : array.length;
|
||||
|
||||
while (++index < length) {
|
||||
const value = array[index];
|
||||
setter(accumulator, value, iteratee(value), array);
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
export default arrayAggregator;
|
||||
@@ -1,21 +0,0 @@
|
||||
import baseEach from './baseEach.js';
|
||||
|
||||
/**
|
||||
* Aggregates elements of `collection` on `accumulator` with keys transformed
|
||||
* by `iteratee` and values set by `setter`.
|
||||
*
|
||||
* @private
|
||||
* @param {Array|Object} collection The collection to iterate over.
|
||||
* @param {Function} setter The function to set `accumulator` values.
|
||||
* @param {Function} iteratee The iteratee to transform keys.
|
||||
* @param {Object} accumulator The initial aggregated object.
|
||||
* @returns {Function} Returns `accumulator`.
|
||||
*/
|
||||
function baseAggregator(collection, setter, iteratee, accumulator) {
|
||||
baseEach(collection, (value, key, collection) => {
|
||||
setter(accumulator, value, iteratee(value), collection);
|
||||
});
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
export default baseAggregator;
|
||||
@@ -1,20 +0,0 @@
|
||||
import arrayAggregator from './.internal/arrayAggregator.js';
|
||||
import baseAggregator from './.internal/baseAggregator.js';
|
||||
|
||||
/**
|
||||
* Creates a function like `groupBy`.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} setter The function to set accumulator values.
|
||||
* @param {Function} [initializer] The accumulator object initializer.
|
||||
* @returns {Function} Returns the new aggregator function.
|
||||
*/
|
||||
function createAggregator(setter, initializer) {
|
||||
return (collection, iteratee) => {
|
||||
const func = Array.isArray(collection) ? arrayAggregator : baseAggregator;
|
||||
const accumulator = initializer ? initializer() : {};
|
||||
return func(collection, setter, iteratee, accumulator);
|
||||
};
|
||||
}
|
||||
|
||||
export default createAggregator;
|
||||
19
countBy.js
19
countBy.js
@@ -1,5 +1,5 @@
|
||||
import baseAssignValue from './.internal/baseAssignValue.js';
|
||||
import createAggregator from './.internal/createAggregator.js';
|
||||
import reduce from './reduce.js';
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
@@ -20,12 +20,15 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
* countBy([6.1, 4.2, 6.3], Math.floor);
|
||||
* // => { '4': 1, '6': 2 }
|
||||
*/
|
||||
const countBy = createAggregator((result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
++result[key];
|
||||
} else {
|
||||
baseAssignValue(result, key, 1);
|
||||
}
|
||||
});
|
||||
function countBy(collection, iteratee) {
|
||||
return reduce(collection, (result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
++result[key];
|
||||
} else {
|
||||
baseAssignValue(result, key, 1);
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export default countBy;
|
||||
|
||||
19
groupBy.js
19
groupBy.js
@@ -1,5 +1,5 @@
|
||||
import baseAssignValue from './.internal/baseAssignValue.js';
|
||||
import createAggregator from './.internal/createAggregator.js';
|
||||
import reduce from './reduce.js';
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
@@ -21,12 +21,15 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
* groupBy([6.1, 4.2, 6.3], Math.floor);
|
||||
* // => { '4': [4.2], '6': [6.1, 6.3] }
|
||||
*/
|
||||
const groupBy = createAggregator((result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
result[key].push(value);
|
||||
} else {
|
||||
baseAssignValue(result, key, [value]);
|
||||
}
|
||||
});
|
||||
function groupBy(collection, iteratee) {
|
||||
return reduce(collection, (result, value, key) => {
|
||||
if (hasOwnProperty.call(result, key)) {
|
||||
result[key].push(value);
|
||||
} else {
|
||||
baseAssignValue(result, key, [value]);
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
|
||||
export default groupBy;
|
||||
|
||||
17
keyBy.js
17
keyBy.js
@@ -1,5 +1,5 @@
|
||||
import baseAssignValue from './.internal/baseAssignValue.js';
|
||||
import createAggregator from './.internal/createAggregator.js';
|
||||
import reduce from './reduce.js';
|
||||
|
||||
/**
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
@@ -19,16 +19,13 @@ import createAggregator from './.internal/createAggregator.js';
|
||||
* { 'dir': 'right', 'code': 100 }
|
||||
* ];
|
||||
*
|
||||
* keyBy(array, function(o) {
|
||||
* return String.fromCharCode(o.code);
|
||||
* });
|
||||
* keyBy(array, ({ code }) => String.fromCharCode(code));
|
||||
* // => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
|
||||
*
|
||||
* keyBy(array, 'dir');
|
||||
* // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
|
||||
*/
|
||||
const keyBy = createAggregator((result, value, key) => {
|
||||
baseAssignValue(result, key, value);
|
||||
});
|
||||
function keyBy(collection, iteratee) {
|
||||
return reduce(collection, (result, value, key) => (
|
||||
baseAssignValue(result, key, value)
|
||||
), {});
|
||||
}
|
||||
|
||||
export default keyBy;
|
||||
|
||||
11
partition.js
11
partition.js
@@ -1,4 +1,4 @@
|
||||
import createAggregator from './.internal/createAggregator.js';
|
||||
import reduce from './reduce.js';
|
||||
|
||||
/**
|
||||
* Creates an array of elements split into two groups, the first of which
|
||||
@@ -19,10 +19,13 @@ import createAggregator from './.internal/createAggregator.js';
|
||||
* { 'user': 'pebbles', 'age': 1, 'active': false }
|
||||
* ];
|
||||
*
|
||||
* partition(users, o => o.active);
|
||||
* partition(users, ({ active }) => active);
|
||||
* // => objects for [['fred'], ['barney', 'pebbles']]
|
||||
*/
|
||||
const partition = createAggregator((result, value, key) =>
|
||||
result[key ? 0 : 1].push(value), () => [[], []]);
|
||||
function partition(collection, predicate) {
|
||||
return reduce(collection, (result, value, key) => (
|
||||
result[key ? 0 : 1].push(value)
|
||||
), [[], []]);
|
||||
}
|
||||
|
||||
export default partition;
|
||||
|
||||
Reference in New Issue
Block a user