index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  5. var getGlobalSymbolDescription = GetIntrinsic('%Symbol.keyFor%', true);
  6. var thisSymbolValue = callBound('%Symbol.prototype.valueOf%', true);
  7. var symToStr = callBound('Symbol.prototype.toString', true);
  8. var getInferredName = require('./getInferredName');
  9. /* eslint-disable consistent-return */
  10. module.exports = callBound('%Symbol.prototype.description%', true) || function getSymbolDescription(symbol) {
  11. if (!thisSymbolValue) {
  12. throw new $SyntaxError('Symbols are not supported in this environment');
  13. }
  14. // will throw if not a symbol primitive or wrapper object
  15. var sym = thisSymbolValue(symbol);
  16. if (getInferredName) {
  17. var name = getInferredName(sym);
  18. if (name === '') {
  19. return;
  20. }
  21. return name.slice(1, -1); // name.slice('['.length, -']'.length);
  22. }
  23. var desc;
  24. if (getGlobalSymbolDescription) {
  25. desc = getGlobalSymbolDescription(sym);
  26. if (typeof desc === 'string') {
  27. return desc;
  28. }
  29. }
  30. desc = symToStr(sym).slice(7, -1); // str.slice('Symbol('.length, -')'.length);
  31. if (desc) {
  32. return desc;
  33. }
  34. };