The Wayback Machine - https://web.archive.org/web/20201220221248/https://github.com/lodash/lodash/blob/master/invert.js
Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
31 lines (29 sloc) 786 Bytes
const toString = Object.prototype.toString
/**
* 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.
*
* @since 0.7.0
* @category Object
* @param {Object} object The object to invert.
* @returns {Object} Returns the new inverted object.
* @example
*
* const object = { 'a': 1, 'b': 2, 'c': 1 }
*
* invert(object)
* // => { '1': 'c', '2': 'b' }
*/
function invert(object) {
const result = {}
Object.keys(object).forEach((key) => {
let value = object[key]
if (value != null && typeof value.toString !== 'function') {
value = toString.call(value)
}
result[value] = key
})
return result
}
export default invert
You can’t perform that action at this time.