I wonder if there is a nice way to get the descriptions of a Symbol.
For example,
var s = Symbol(5);
The default implementation of the toString will simply print Symbol(5)
I would like to find a way to extract the actual description i.e. 5
I wonder if there is a nice way to get the descriptions of a Symbol.
For example,
var s = Symbol(5);
The default implementation of the toString will simply print Symbol(5)
I would like to find a way to extract the actual description i.e. 5
symbol-description
package on npmReturns description of provided symbol:
It was published by me just now after reading this answer here.
There is no neat way to do this. However, if you have registered a symbol in the global registry, you can use
Symbol.keyFor()
:This works because
String(symbol)
explicitly coerce the symbol into a string e.g.Symbol('test')
gets coerced into"Symbol(test)"
.Doing a splicing on the from 7 to -1 does a splicing between the two brackets, thus capturing the description
test
Note: that this may not work for object descriptions (e.g.
Symbol({test})
) as objects gets coerced into"[object Object]"