time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

invert.js (1128B)


      1 var constant = require('./constant'),
      2     createInverter = require('./_createInverter'),
      3     identity = require('./identity');
      4 
      5 /** Used for built-in method references. */
      6 var 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  */
     13 var 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  */
     33 var 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 
     42 module.exports = invert;