UNPKG

2.6 kBJavaScriptView Raw
1// `Symbol.prototype.description` getter
2// https://tc39.es/ecma262/#sec-symbol.prototype.description
3'use strict';
4var $ = require('../internals/export');
5var DESCRIPTORS = require('../internals/descriptors');
6var global = require('../internals/global');
7var uncurryThis = require('../internals/function-uncurry-this');
8var hasOwn = require('../internals/has-own-property');
9var isCallable = require('../internals/is-callable');
10var isPrototypeOf = require('../internals/object-is-prototype-of');
11var toString = require('../internals/to-string');
12var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
13var copyConstructorProperties = require('../internals/copy-constructor-properties');
14
15var NativeSymbol = global.Symbol;
16var SymbolPrototype = NativeSymbol && NativeSymbol.prototype;
17
18if (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 ? new NativeSymbol(description)
28 // in Edge 13, String(Symbol(undefined)) === 'Symbol(undefined)'
29 : description === undefined ? NativeSymbol() : NativeSymbol(description);
30 if (description === '') EmptyStringDescriptionStore[result] = true;
31 return result;
32 };
33
34 copyConstructorProperties(SymbolWrapper, NativeSymbol);
35 SymbolWrapper.prototype = SymbolPrototype;
36 SymbolPrototype.constructor = SymbolWrapper;
37
38 var NATIVE_SYMBOL = String(NativeSymbol('description detection')) === 'Symbol(description detection)';
39 var thisSymbolValue = uncurryThis(SymbolPrototype.valueOf);
40 var symbolDescriptiveString = uncurryThis(SymbolPrototype.toString);
41 var regexp = /^Symbol\((.*)\)[^)]+$/;
42 var replace = uncurryThis(''.replace);
43 var stringSlice = uncurryThis(''.slice);
44
45 defineBuiltInAccessor(SymbolPrototype, 'description', {
46 configurable: true,
47 get: function description() {
48 var symbol = thisSymbolValue(this);
49 if (hasOwn(EmptyStringDescriptionStore, symbol)) return '';
50 var string = symbolDescriptiveString(symbol);
51 var desc = NATIVE_SYMBOL ? stringSlice(string, 7, -1) : replace(string, regexp, '$1');
52 return desc === '' ? undefined : desc;
53 }
54 });
55
56 $({ global: true, constructor: true, forced: true }, {
57 Symbol: SymbolWrapper
58 });
59}