Polyfill for Maps and Sets
There's multiple polyfills for Map, Set, and WeakMap (WeakMap not being fully implementable, but close). Here's one: benvie.github.com/harmony-collections.
It is a good point that the specified keys(), values(), and items() return iterators rather than arrays, which makes them a little less friendly from a shimming perspective.
I made a few opinionated deviations from spec in Montage’s Collections package.
I went with iterate() instead of iterator(). I feel the tide rolling over me on that one and will probably back off.
keys(), values(), and items() return arrays. I didn’t even realize this was against the spec. I would have expected iterateKeys(), iterateValues(), and iterateItems() to return iterators and keys(), values(), and items() to return Array snapshots.
That said, I would also expect the Iterator prototype to be pretty rich, so this would work regardless of whether keys() returns an Array or Iterator. In either case, it is straightforward to shim.
map.keys().forEach(function () { });
Kris Kowal
On Thu, Jan 17, 2013 at 7:58 PM, Brandon Benvie <brandon at brandonbenvie.com> wrote:
There's multiple polyfills for Map, Set, and WeakMap (WeakMap not being fully implementable, but close). Here's one: benvie.github.com/harmony-collections.
This polyfill isn't exact in the same way that I described. The difference is, instead of shamelessly adding 'getKeys' in the way that I suggested, it shamelessly adds 'forEach', which isn't in the spec.
I'd be happy with any of those, although, again, I believe that 'getKeys' would prove more useful in practice.
Dude, do you even read the spec?
On Thu, Jan 17, 2013 at 8:12 PM, Domenic Denicola <domenic at domenicdenicola.com> wrote:
Dude, do you even read the spec?
So this is just the wiki lacking some updates?
Thaddee Tyl wrote:
On Thu, Jan 17, 2013 at 8:12 PM, Domenic Denicola <domenic at domenicdenicola.com> wrote:
Dude, do you even read the spec?
So this is just the wiki lacking some updates?
Yes.
The wiki will lag the spec. I think it may pay to update the wiki, but it's unlikely everyone will do this diligently (cc'ing rwaldron in case he is motivated in this forEach case). So: always check Allen's latest draft. It trumps the wiki, unless there's a bug (and it may be hard to know without reading es-discuss or just asking here, cc'ing Allen).
It doesn't seem reasonable to maintain two versions of the proposals as they become specifications.
My first thought is that the simplest possible strategy is to update harmony proposal pages (on the wiki) with a line at the top that indicates that the proposal is now in the spec draft. This is low effort-cost to convey that everyone should be looking at the latest and greatest of each proposal as they become part of the ES6 draft revisions and progress towards finalization therein.
As it turns out, my suggestion above had already been used (at least) once before—Allen marked the destructuring proposal similarly. I've gone ahead and updated the following:
harmony:spread, harmony:rest_parameters, harmony:destructuring, harmony:weak_maps, harmony:simple_maps_and_sets, harmony:arrow_function_syntax, harmony:iterators, harmony:generators, harmony:generator_expressions, harmony:array_comprehensions, harmony:object_literals, strawman:maximally_minimal_classes, harmony:quasis
...to all reflect the same consistent message and linkage to the draft spec and bugzilla pages.
I believe it is impossible to do even a partially functional polyfill for Maps and Sets in ES5. Indeed, the only way to iterate through it is with the 'for (… of …)' construct, which is sure to break in ES5.
I wish there was a way to polyfill it. Something like 'Map.prototype.getKeys' could return an array of key entries, for instance. Obviously, Sets would benefit from the same treatment.
In the case of Sets especially, I can see this method as being something that everybody would end up writing many times manually, similar to how they now write the boilerplate to treat 'arguments' like an Array, over and over again.
Could we consider something like that, please?