[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
[79a0317] | 5 | var callBound = require('call-bound');
|
---|
[d565449] | 6 |
|
---|
| 7 | var $SyntaxError = require('es-errors/syntax');
|
---|
| 8 | var getGlobalSymbolDescription = GetIntrinsic('%Symbol.keyFor%', true);
|
---|
[79a0317] | 9 | /** @type {undefined | ((thisArg: symbol | Symbol) => symbol)} */
|
---|
[d565449] | 10 | var thisSymbolValue = callBound('%Symbol.prototype.valueOf%', true);
|
---|
[79a0317] | 11 | /** @type {undefined | ((thisArg: symbol | Symbol) => string)} */
|
---|
[d565449] | 12 | var symToStr = callBound('Symbol.prototype.toString', true);
|
---|
[79a0317] | 13 | /** @type {(thisArg: string, start?: number, end?: number) => string} */
|
---|
[d565449] | 14 | var $strSlice = callBound('String.prototype.slice');
|
---|
| 15 |
|
---|
| 16 | var getInferredName = require('./getInferredName');
|
---|
| 17 |
|
---|
[79a0317] | 18 | /** @type {import('.')} */
|
---|
[d565449] | 19 | /* eslint-disable consistent-return */
|
---|
| 20 | module.exports = callBound('%Symbol.prototype.description%', true) || function getSymbolDescription(symbol) {
|
---|
| 21 | if (!thisSymbolValue) {
|
---|
| 22 | throw new $SyntaxError('Symbols are not supported in this environment');
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | // will throw if not a symbol primitive or wrapper object
|
---|
| 26 | var sym = thisSymbolValue(symbol);
|
---|
| 27 |
|
---|
| 28 | if (getInferredName) {
|
---|
| 29 | var name = getInferredName(sym);
|
---|
| 30 | if (name === '') {
|
---|
| 31 | return;
|
---|
| 32 | }
|
---|
| 33 | return name.slice(1, -1); // name.slice('['.length, -']'.length);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | var desc;
|
---|
| 37 | if (getGlobalSymbolDescription) {
|
---|
| 38 | desc = getGlobalSymbolDescription(sym);
|
---|
| 39 | if (typeof desc === 'string') {
|
---|
| 40 | return desc;
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[79a0317] | 44 | // eslint-disable-next-line no-extra-parens
|
---|
| 45 | desc = $strSlice(/** @type {NonNullable<typeof symToStr>} */ (symToStr)(sym), 7, -1); // str.slice('Symbol('.length, -')'.length);
|
---|
[d565449] | 46 | if (desc) {
|
---|
| 47 | return desc;
|
---|
| 48 | }
|
---|
| 49 | };
|
---|