mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 00:27:50 +00:00
Bump to v4.1.0.
This commit is contained in:
38
invert.js
38
invert.js
@@ -1,23 +1,16 @@
|
||||
var arrayReduce = require('./internal/arrayReduce'),
|
||||
keys = require('./keys');
|
||||
|
||||
/** Used for built-in method references. */
|
||||
var objectProto = global.Object.prototype;
|
||||
|
||||
/** Used to check objects for own properties. */
|
||||
var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
var constant = require('./constant'),
|
||||
createInverter = require('./_createInverter'),
|
||||
identity = require('./identity');
|
||||
|
||||
/**
|
||||
* Creates an object composed of the inverted keys and values of `object`.
|
||||
* If `object` contains duplicate values, subsequent values overwrite property
|
||||
* assignments of previous values unless `multiVal` is `true`.
|
||||
* assignments of previous values.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to invert.
|
||||
* @param {boolean} [multiVal] Allow multiple values per key.
|
||||
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
|
||||
* @returns {Object} Returns the new inverted object.
|
||||
* @example
|
||||
*
|
||||
@@ -25,26 +18,9 @@ var hasOwnProperty = objectProto.hasOwnProperty;
|
||||
*
|
||||
* _.invert(object);
|
||||
* // => { '1': 'c', '2': 'b' }
|
||||
*
|
||||
* // with `multiVal`
|
||||
* _.invert(object, true);
|
||||
* // => { '1': ['a', 'c'], '2': ['b'] }
|
||||
*/
|
||||
function invert(object, multiVal, guard) {
|
||||
return arrayReduce(keys(object), function(result, key) {
|
||||
var value = object[key];
|
||||
if (multiVal && !guard) {
|
||||
if (hasOwnProperty.call(result, value)) {
|
||||
result[value].push(key);
|
||||
} else {
|
||||
result[value] = [key];
|
||||
}
|
||||
}
|
||||
else {
|
||||
result[value] = key;
|
||||
}
|
||||
return result;
|
||||
}, {});
|
||||
}
|
||||
var invert = createInverter(function(result, value, key) {
|
||||
result[value] = key;
|
||||
}, constant(identity));
|
||||
|
||||
module.exports = invert;
|
||||
|
||||
Reference in New Issue
Block a user