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

Adds support for symbols as keys in Maps. #1392

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,24 @@ describe('Map', () => {
expect(map.toString()).toEqual('Map { 2: 2 }');
});

it('supports symbols as keys', () => {
const symbolA = Symbol('A');
const symbolB = Symbol('B');
const symbolC = Symbol('C');
const m = Map({[symbolA]: 'A', [symbolB]: 'B', [symbolC]: 'C'});
expect(m.size).toBe(3);
expect(m.get(symbolA)).toBe('A');
expect(m.get(symbolB)).toBe('B');
expect(m.get(symbolC)).toBe('C');
});

it('symbol keys are unique', () => {
const symbolA = Symbol('FooBar');
const symbolB = Symbol('FooBar');
const m = Map({[symbolA]: 'FizBuz', [symbolB]: 'FooBar'});
expect(m.size).toBe(2);
expect(m.get(symbolA)).toBe('FizBuz');
expect(m.get(symbolB)).toBe('FooBar');
});

});
6 changes: 5 additions & 1 deletion src/Seq.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ export class ArraySeq extends IndexedSeq {

class ObjectSeq extends KeyedSeq {
constructor(object) {
const keys = Object.keys(object);
let symbols = [];
if (typeof Object.getOwnPropertySymbols === 'function') {
symbols = Object.getOwnPropertySymbols(object);
}
const keys = Object.keys(object).concat(symbols);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should at least avoid concatenating in the common case where there are no symbols defined in the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.

this._object = object;
this._keys = keys;
this.size = keys.length;
Expand Down