index.js 1.2 KB

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