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