_getTag.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var DataView = require('./_DataView'),
  2. Map = require('./_Map'),
  3. Promise = require('./_Promise'),
  4. Set = require('./_Set'),
  5. WeakMap = require('./_WeakMap'),
  6. baseGetTag = require('./_baseGetTag'),
  7. toSource = require('./_toSource');
  8. /** `Object#toString` result references. */
  9. var mapTag = '[object Map]',
  10. objectTag = '[object Object]',
  11. promiseTag = '[object Promise]',
  12. setTag = '[object Set]',
  13. weakMapTag = '[object WeakMap]';
  14. var dataViewTag = '[object DataView]';
  15. /** Used to detect maps, sets, and weakmaps. */
  16. var dataViewCtorString = toSource(DataView),
  17. mapCtorString = toSource(Map),
  18. promiseCtorString = toSource(Promise),
  19. setCtorString = toSource(Set),
  20. weakMapCtorString = toSource(WeakMap);
  21. /**
  22. * Gets the `toStringTag` of `value`.
  23. *
  24. * @private
  25. * @param {*} value The value to query.
  26. * @returns {string} Returns the `toStringTag`.
  27. */
  28. var getTag = baseGetTag;
  29. // Fallback for data views, maps, sets, and weak maps in IE 11 and promises in Node.js < 6.
  30. if ((DataView && getTag(new DataView(new ArrayBuffer(1))) != dataViewTag) ||
  31. (Map && getTag(new Map) != mapTag) ||
  32. (Promise && getTag(Promise.resolve()) != promiseTag) ||
  33. (Set && getTag(new Set) != setTag) ||
  34. (WeakMap && getTag(new WeakMap) != weakMapTag)) {
  35. getTag = function(value) {
  36. var result = baseGetTag(value),
  37. Ctor = result == objectTag ? value.constructor : undefined,
  38. ctorString = Ctor ? toSource(Ctor) : '';
  39. if (ctorString) {
  40. switch (ctorString) {
  41. case dataViewCtorString: return dataViewTag;
  42. case mapCtorString: return mapTag;
  43. case promiseCtorString: return promiseTag;
  44. case setCtorString: return setTag;
  45. case weakMapCtorString: return weakMapTag;
  46. }
  47. }
  48. return result;
  49. };
  50. }
  51. module.exports = getTag;