| 1 | // `Symbol.prototype.description` getter
|
|---|
| 2 | // https://tc39.es/ecma262/#sec-symbol.prototype.description
|
|---|
| 3 | 'use strict';
|
|---|
| 4 | var $ = require('../internals/export');
|
|---|
| 5 | var DESCRIPTORS = require('../internals/descriptors');
|
|---|
| 6 | var globalThis = require('../internals/global-this');
|
|---|
| 7 | var call = require('../internals/function-call');
|
|---|
| 8 | var uncurryThis = require('../internals/function-uncurry-this');
|
|---|
| 9 | var hasOwn = require('../internals/has-own-property');
|
|---|
| 10 | var isCallable = require('../internals/is-callable');
|
|---|
| 11 | var isPrototypeOf = require('../internals/object-is-prototype-of');
|
|---|
| 12 | var toString = require('../internals/to-string');
|
|---|
| 13 | var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
|
|---|
| 14 | var copyConstructorProperties = require('../internals/copy-constructor-properties');
|
|---|
| 15 |
|
|---|
| 16 | var NativeSymbol = globalThis.Symbol;
|
|---|
| 17 | var SymbolPrototype = NativeSymbol && NativeSymbol.prototype;
|
|---|
| 18 |
|
|---|
| 19 | if (DESCRIPTORS && isCallable(NativeSymbol) && (!('description' in SymbolPrototype) ||
|
|---|
| 20 | // Safari 12 bug
|
|---|
| 21 | NativeSymbol().description !== undefined
|
|---|
| 22 | )) {
|
|---|
| 23 | var EmptyStringDescriptionStore = {};
|
|---|
| 24 | // wrap Symbol constructor for correct work with undefined description
|
|---|
| 25 | var SymbolWrapper = function Symbol() {
|
|---|
| 26 | var description = arguments.length < 1 || arguments[0] === undefined ? undefined : toString(arguments[0]);
|
|---|
| 27 | var result = isPrototypeOf(SymbolPrototype, this)
|
|---|
| 28 | // eslint-disable-next-line sonarjs/inconsistent-function-call -- ok
|
|---|
| 29 | ? new NativeSymbol(description)
|
|---|
| 30 | // in Edge 13, String(Symbol(undefined)) === 'Symbol(undefined)'
|
|---|
| 31 | : description === undefined ? NativeSymbol() : NativeSymbol(description);
|
|---|
| 32 | if (description === '') EmptyStringDescriptionStore[result] = true;
|
|---|
| 33 | return result;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | copyConstructorProperties(SymbolWrapper, NativeSymbol);
|
|---|
| 37 | // wrap Symbol.for for correct handling of empty string descriptions
|
|---|
| 38 | var nativeFor = SymbolWrapper['for'];
|
|---|
| 39 | SymbolWrapper['for'] = { 'for': function (key) {
|
|---|
| 40 | var stringKey = toString(key);
|
|---|
| 41 | var symbol = call(nativeFor, this, stringKey);
|
|---|
| 42 | if (stringKey === '') EmptyStringDescriptionStore[symbol] = true;
|
|---|
| 43 | return symbol;
|
|---|
| 44 | } }['for'];
|
|---|
| 45 | SymbolWrapper.prototype = SymbolPrototype;
|
|---|
| 46 | SymbolPrototype.constructor = SymbolWrapper;
|
|---|
| 47 |
|
|---|
| 48 | var NATIVE_SYMBOL = String(NativeSymbol('description detection')) === 'Symbol(description detection)';
|
|---|
| 49 | var thisSymbolValue = uncurryThis(SymbolPrototype.valueOf);
|
|---|
| 50 | var symbolDescriptiveString = uncurryThis(SymbolPrototype.toString);
|
|---|
| 51 | var regexp = /^Symbol\((.*)\)[^)]+$/;
|
|---|
| 52 | var replace = uncurryThis(''.replace);
|
|---|
| 53 | var stringSlice = uncurryThis(''.slice);
|
|---|
| 54 |
|
|---|
| 55 | defineBuiltInAccessor(SymbolPrototype, 'description', {
|
|---|
| 56 | configurable: true,
|
|---|
| 57 | get: function description() {
|
|---|
| 58 | var symbol = thisSymbolValue(this);
|
|---|
| 59 | if (hasOwn(EmptyStringDescriptionStore, symbol)) return '';
|
|---|
| 60 | var string = symbolDescriptiveString(symbol);
|
|---|
| 61 | var desc = NATIVE_SYMBOL ? stringSlice(string, 7, -1) : replace(string, regexp, '$1');
|
|---|
| 62 | return desc === '' ? undefined : desc;
|
|---|
| 63 | }
|
|---|
| 64 | });
|
|---|
| 65 |
|
|---|
| 66 | $({ global: true, constructor: true, forced: true }, {
|
|---|
| 67 | Symbol: SymbolWrapper
|
|---|
| 68 | });
|
|---|
| 69 | }
|
|---|