Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

support Symbol objects #29

wants to merge 2 commits into from

Conversation

davidchambers
Copy link
Member

This pull request changes the way Symbol objects are shown:

Expression Previously gave… Now gives…
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 and Object#@@show now include symbol properties (see Object.getOwnPropertySymbols).

index.js Outdated
Comment on lines 52 to 66
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');
Copy link
Member

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;
Copy link
Member

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]');
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants