選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

invert.js 1.1KB

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