Avoid using the values toString method in _.invert if it’s not a function. [closes #3260]

This commit is contained in:
John-David Dalton
2017-07-12 07:42:33 -07:00
parent 534296bba4
commit 01148a1df8

View File

@@ -1,3 +1,5 @@
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
@@ -17,6 +19,9 @@
function invert(object) {
const result = {}
Object.keys(object).forEach((value, key) => {
if (value != null && typeof value.toString != 'function') {
value = toString.call(value)
}
result[value] = key
})
return result