1 var arrayReduce = require('./internal/arrayReduce'),
2 keys = require('./keys');
4 /** Used for built-in method references. */
5 var objectProto = global.Object.prototype;
7 /** Used to check objects for own properties. */
8 var hasOwnProperty = objectProto.hasOwnProperty;
11 * Creates an object composed of the inverted keys and values of `object`.
12 * If `object` contains duplicate values, subsequent values overwrite property
13 * assignments of previous values unless `multiVal` is `true`.
18 * @param {Object} object The object to invert.
19 * @param {boolean} [multiVal] Allow multiple values per key.
20 * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
21 * @returns {Object} Returns the new inverted object.
24 * var object = { 'a': 1, 'b': 2, 'c': 1 };
27 * // => { '1': 'c', '2': 'b' }
30 * _.invert(object, true);
31 * // => { '1': ['a', 'c'], '2': ['b'] }
33 function invert(object, multiVal, guard) {
34 return arrayReduce(keys(object), function(result, key) {
35 var value = object[key];
36 if (multiVal && !guard) {
37 if (hasOwnProperty.call(result, value)) {
38 result[value].push(key);
40 result[value] = [key];
50 module.exports = invert;