1 of 8

Bradley Farias

2 of 8

Problems

  • Arbitrary nested Maps / creation of Composite keys is rather difficult
  • We don't have a way for Maps to have synthetic keys so manual keying must happen outside of the Map operations

3 of 8

  • Generic API for composite keys
  • Usable on Objects
    • Symbol.for converts to String
  • Order of arguments matters
  • Duplicate values accepted
  • Does not allow getting values back from Symbol
    • Allows values to be collected, does not cause problems with WeakMaps

4 of 8

Symbol.compositeKey(...values)

A = {};

B = {};

Key = Symbol.compositeKey(A, B);

O = { � [Key]: 1�}

A = null;

5 of 8

Symbol.compositeKey(...values)

Key = Symbol.compositeKey('foo');

O = { � [Key]: 1�}

O[Symbol.for('foo')] === O[Key] // seems reasonable?

6 of 8

Map/Set rekey() parameter

  • Generic API for having custom keyed collections
  • Returns any value, could be reference to an Object
  • Replaces key before being placed in collection entries
    • #.prototype.entries / #.prototype.keys show result of this function
  • Not on Weak collections
    • Use cases?
    • Would have to return a Object

7 of 8

Map/Set rekey() parameter

new Map([], {� rekey(user) {� if (!Object.getOwnPropertyDescriptor(user, 'email')) {� throw TypeError('key must contain "email" property');� }� return user.email;� }�}

8 of 8

Map/Set rekey() parameter

new Map([], {� rekey: ([...parts]) => Symbol.compositeKey(...parts)�}

new Map([], {� rekey: ({x,y}) => Symbol.compositeKey(x,y)�}