mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 10:17:48 +00:00
Consolidate invert modules.
This commit is contained in:
21
invertBy.js
21
invertBy.js
@@ -1,4 +1,4 @@
|
||||
import createInverter from './.internal/createInverter.js';
|
||||
import baseForOwn from './baseForOwn.js';
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
@@ -22,12 +22,17 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
* invertBy(object, value => `group${ value }`);
|
||||
* // => { 'group1': ['a', 'c'], 'group2': ['b'] }
|
||||
*/
|
||||
const invertBy = createInverter((result, value, key) => {
|
||||
if (hasOwnProperty.call(result, value)) {
|
||||
result[value].push(key);
|
||||
} else {
|
||||
result[value] = [key];
|
||||
}
|
||||
});
|
||||
function invertBy(object, iteratee) {
|
||||
const result = {};
|
||||
baseForOwn(object, (value, key) => {
|
||||
value = iteratee(value);
|
||||
if (hasOwnProperty.call(result, value)) {
|
||||
result[value].push(key);
|
||||
} else {
|
||||
result[value] = [key];
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export default invertBy;
|
||||
|
||||
Reference in New Issue
Block a user