UNPKG

1.13 kBJavaScriptView Raw
1var constant = require('./constant'),
2 createInverter = require('./_createInverter'),
3 identity = require('./identity');
4
5/** Used for built-in method references. */
6var objectProto = Object.prototype;
7
8/**
9 * Used to resolve the
10 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
11 * of values.
12 */
13var nativeObjectToString = objectProto.toString;
14
15/**
16 * Creates an object composed of the inverted keys and values of `object`.
17 * If `object` contains duplicate values, subsequent values overwrite
18 * property assignments of previous values.
19 *
20 * @static
21 * @memberOf _
22 * @since 0.7.0
23 * @category Object
24 * @param {Object} object The object to invert.
25 * @returns {Object} Returns the new inverted object.
26 * @example
27 *
28 * var object = { 'a': 1, 'b': 2, 'c': 1 };
29 *
30 * _.invert(object);
31 * // => { '1': 'c', '2': 'b' }
32 */
33var invert = createInverter(function(result, value, key) {
34 if (value != null &&
35 typeof value.toString != 'function') {
36 value = nativeObjectToString.call(value);
37 }
38
39 result[value] = key;
40}, constant(identity));
41
42module.exports = invert;