react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / object / invert.js
80742 viewsvar isIterateeCall = require('../internal/isIterateeCall'),1keys = require('./keys');23/** Used for native method references. */4var objectProto = Object.prototype;56/** Used to check objects for own properties. */7var hasOwnProperty = objectProto.hasOwnProperty;89/**10* Creates an object composed of the inverted keys and values of `object`.11* If `object` contains duplicate values, subsequent values overwrite property12* assignments of previous values unless `multiValue` is `true`.13*14* @static15* @memberOf _16* @category Object17* @param {Object} object The object to invert.18* @param {boolean} [multiValue] Allow multiple values per key.19* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.20* @returns {Object} Returns the new inverted object.21* @example22*23* var object = { 'a': 1, 'b': 2, 'c': 1 };24*25* _.invert(object);26* // => { '1': 'c', '2': 'b' }27*28* // with `multiValue`29* _.invert(object, true);30* // => { '1': ['a', 'c'], '2': ['b'] }31*/32function invert(object, multiValue, guard) {33if (guard && isIterateeCall(object, multiValue, guard)) {34multiValue = null;35}36var index = -1,37props = keys(object),38length = props.length,39result = {};4041while (++index < length) {42var key = props[index],43value = object[key];4445if (multiValue) {46if (hasOwnProperty.call(result, value)) {47result[value].push(key);48} else {49result[value] = [key];50}51}52else {53result[value] = key;54}55}56return result;57}5859module.exports = invert;606162