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