getSymbolDescription.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 === '') { return; }
  19. return name.slice(1, -1); // name.slice('['.length, -']'.length);
  20. }
  21. var desc;
  22. if (getGlobalSymbolDescription) {
  23. desc = getGlobalSymbolDescription(sym);
  24. if (typeof desc === 'string') {
  25. return desc;
  26. }
  27. }
  28. desc = symToStr(sym).slice(7, -1); // str.slice('Symbol('.length, -')'.length);
  29. if (desc) {
  30. return desc;
  31. }
  32. };