1 var isIterateeCall = require('../internal/isIterateeCall'),
2 keys = require('./keys');
4 /** Used for native method references. */
5 var objectProto = 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 `multiValue` is `true`.
18 * @param {Object} object The object to invert.
19 * @param {boolean} [multiValue] Allow multiple values per key.
20 * @param- {Object} [guard] Enables use as a callback 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' }
29 * // with `multiValue`
30 * _.invert(object, true);
31 * // => { '1': ['a', 'c'], '2': ['b'] }
33 function invert(object, multiValue, guard) {
34 if (guard && isIterateeCall(object, multiValue, guard)) {
35 multiValue = undefined;
39 length = props.length,
42 while (++index < length) {
43 var key = props[index],
47 if (hasOwnProperty.call(result, value)) {
48 result[value].push(key);
50 result[value] = [key];
60 module.exports = invert;