mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Consolidate invert modules.
This commit is contained in:
@@ -1,21 +0,0 @@
|
|||||||
import baseForOwn from './baseForOwn.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The base implementation of `invert` and `invertBy` which inverts
|
|
||||||
* `object` with values transformed by `iteratee` and set by `setter`.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Object} object The object to iterate over.
|
|
||||||
* @param {Function} setter The function to set `accumulator` values.
|
|
||||||
* @param {Function} iteratee The iteratee to transform values.
|
|
||||||
* @param {Object} accumulator The initial inverted object.
|
|
||||||
* @returns {Function} Returns `accumulator`.
|
|
||||||
*/
|
|
||||||
function baseInverter(object, setter, iteratee, accumulator) {
|
|
||||||
baseForOwn(object, (value, key, object) => {
|
|
||||||
setter(accumulator, iteratee(value), key, object);
|
|
||||||
});
|
|
||||||
return accumulator;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default baseInverter;
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import baseInverter from './.internal/baseInverter.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a function like `invertBy`.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
* @param {Function} setter The function to set accumulator values.
|
|
||||||
* @returns {Function} Returns the new inverter function.
|
|
||||||
*/
|
|
||||||
function createInverter(setter) {
|
|
||||||
return (object, iteratee) => baseInverter(object, setter, iteratee, {});
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createInverter;
|
|
||||||
14
invert.js
14
invert.js
@@ -1,6 +1,4 @@
|
|||||||
import constant from './constant.js';
|
import baseForOwn from './baseForOwn.js';
|
||||||
import createInverter from './.internal/createInverter.js';
|
|
||||||
import identity from './identity.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an object composed of the inverted keys and values of `object`.
|
* Creates an object composed of the inverted keys and values of `object`.
|
||||||
@@ -18,8 +16,12 @@ import identity from './identity.js';
|
|||||||
* invert(object);
|
* invert(object);
|
||||||
* // => { '1': 'c', '2': 'b' }
|
* // => { '1': 'c', '2': 'b' }
|
||||||
*/
|
*/
|
||||||
const invert = createInverter((result, value, key) => {
|
function invert(object) {
|
||||||
result[value] = key;
|
const result = {};
|
||||||
});
|
baseForOwn(object, (value, key) => {
|
||||||
|
result[value] = key;
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export default invert;
|
export default invert;
|
||||||
|
|||||||
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. */
|
/** Used to check objects for own properties. */
|
||||||
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||||
@@ -22,12 +22,17 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|||||||
* invertBy(object, value => `group${ value }`);
|
* invertBy(object, value => `group${ value }`);
|
||||||
* // => { 'group1': ['a', 'c'], 'group2': ['b'] }
|
* // => { 'group1': ['a', 'c'], 'group2': ['b'] }
|
||||||
*/
|
*/
|
||||||
const invertBy = createInverter((result, value, key) => {
|
function invertBy(object, iteratee) {
|
||||||
if (hasOwnProperty.call(result, value)) {
|
const result = {};
|
||||||
result[value].push(key);
|
baseForOwn(object, (value, key) => {
|
||||||
} else {
|
value = iteratee(value);
|
||||||
result[value] = [key];
|
if (hasOwnProperty.call(result, value)) {
|
||||||
}
|
result[value].push(key);
|
||||||
});
|
} else {
|
||||||
|
result[value] = [key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
export default invertBy;
|
export default invertBy;
|
||||||
|
|||||||
Reference in New Issue
Block a user