| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
|---|
| 4 |
|
|---|
| 5 | var callBound = require('call-bound');
|
|---|
| 6 |
|
|---|
| 7 | var $SyntaxError = require('es-errors/syntax');
|
|---|
| 8 | var getGlobalSymbolDescription = GetIntrinsic('%Symbol.keyFor%', true);
|
|---|
| 9 | /** @type {undefined | ((thisArg: symbol | Symbol) => symbol)} */
|
|---|
| 10 | var thisSymbolValue = callBound('%Symbol.prototype.valueOf%', true);
|
|---|
| 11 | /** @type {undefined | ((thisArg: symbol | Symbol) => string)} */
|
|---|
| 12 | var symToStr = callBound('Symbol.prototype.toString', true);
|
|---|
| 13 | /** @type {(thisArg: string, start?: number, end?: number) => string} */
|
|---|
| 14 | var $strSlice = callBound('String.prototype.slice');
|
|---|
| 15 |
|
|---|
| 16 | var getInferredName = require('./getInferredName');
|
|---|
| 17 |
|
|---|
| 18 | /** @type {import('.')} */
|
|---|
| 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 |
|
|---|
| 44 | // eslint-disable-next-line no-extra-parens
|
|---|
| 45 | desc = $strSlice(/** @type {NonNullable<typeof symToStr>} */ (symToStr)(sym), 7, -1); // str.slice('Symbol('.length, -')'.length);
|
|---|
| 46 | if (desc) {
|
|---|
| 47 | return desc;
|
|---|
| 48 | }
|
|---|
| 49 | };
|
|---|