forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
invert.js
31 lines (29 loc) · 786 Bytes
/
invert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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