blob: e31cad27445fffd6e9d8ca9d3cb7ce8f12d872dd [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var callBound = require('call-bind/callBound');
6
7var $SyntaxError = GetIntrinsic('%SyntaxError%');
8var getGlobalSymbolDescription = GetIntrinsic('%Symbol.keyFor%', true);
9var thisSymbolValue = callBound('%Symbol.prototype.valueOf%', true);
10var symToStr = callBound('Symbol.prototype.toString', true);
11
12var getInferredName = require('./getInferredName');
13
14/* eslint-disable consistent-return */
15module.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};