-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support Symbol objects #29
base: main
Are you sure you want to change the base?
Conversation
index.js
Outdated
const wellKnownSymbols = [ | ||
Symbol.asyncIterator, | ||
Symbol.hasInstance, | ||
Symbol.isConcatSpreadable, | ||
Symbol.iterator, | ||
Symbol.match, | ||
Symbol.matchAll, | ||
Symbol.replace, | ||
Symbol.search, | ||
Symbol.species, | ||
Symbol.split, | ||
Symbol.toPrimitive, | ||
Symbol.toStringTag, | ||
Symbol.unscopables, | ||
].filter (x => typeof x === 'symbol'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be a Set
index.js
Outdated
@@ -114,6 +135,11 @@ const show = x => { | |||
'new String (' + show (x.valueOf ()) + ')' : | |||
JSON.stringify (x); | |||
|
|||
case '[object Symbol]': | |||
for (const s of wellKnownSymbols) if (s === x) return x.description; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then this is just if (wellKnownSymbols.has(x)) return x.description
. Performance should remain the same.
xs.z = 0; | ||
xs[Symbol ('y')] = 0; | ||
xs[Symbol ('x')] = 0; | ||
eq (show (xs), '["foo", "bar", "baz", "z": 0, [Symbol ("x")]: 0, [Symbol ("y")]: 0]'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This representation doesn't evaluate, though. An alternative would be to output Object.assign(["foo", "bar", "baz"], {"z": 0, [Symbol ("x")]: 0, [Symbol ("y")]: 0})
or something. Maybe not worth it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're quite right. This is, though, consistent with existing behaviour:
> show (/x/.exec ('xyz'))
'["x", "groups": undefined, "index": 0, "input": "xyz"]'
We could change the representation of non-index properties, but that would be an orthogonal change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Feel free to merge this PR by the way, I just wanted to note it. I'm not sure if it warrants an issue and the consequent work. I can already see the edge cases now, so maybe not open this can. :p
This pull request changes the way Symbol objects are shown:
show (Symbol.iterator)
'<object Symbol>'
'Symbol.iterator'
show (Symbol ())
'<object Symbol>'
'Symbol ()'
show (Symbol ('x'))
'<object Symbol>'
'Symbol ("x")'
show (Symbol.for ('x'))
'<object Symbol>'
'Symbol ("x")'
:\Additionally,
Array#@@show
andObject#@@show
now include symbol properties (seeObject.getOwnPropertySymbols
).