Consolidate invert modules.

This commit is contained in:
John-David Dalton
2017-01-11 10:39:30 -08:00
parent dcded33f58
commit 36927bff8b
4 changed files with 21 additions and 49 deletions

View File

@@ -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;