WebReflection Ltd
webreflection / es6-collections Goto Github PK
View Code? Open in Web Editor NEWMap, WeakMap, and Set fast/simple shim for Harmony collections
License: MIT License
Map, WeakMap, and Set fast/simple shim for Harmony collections
License: MIT License
ES6 Specification: Map.forEach
The argument order for the callback should be (value, key, obj), but the tests have the order as (key, value, obj). The issue seems to be the implementation of sharedForEach, which has the incorrect order.
Safari 7.1 and 8 have native support for Set and Map, so es6-collections isn't used in those browsers. The Safari implementation, however, is seriously crippled because the Iterator returned by the keys
, values
and entries
functions does not support next()
. Because these are common functions, the native implementation can not be used reliably:
m = new Map();
m.set(1, "test");
m.set("2", false);
it = m.values(); // returns Map Iterator
it.next(); // TypeError: undefined is not a function
In this case, the es6-collections implementation should be used over the native one, or at least the option to force it should be present.
from an es6 point of view, the following line is not correct as _values
is a private property
https://github.com/WebReflection/es6-collections/blob/master/test/index.js#L284
So instead of doing
assert(JSON.stringify(o._values) === JSON.stringify([1, 3, 0]));
it should rather use Array.from
(es6, so requires a polyfill on some browser), that allows to create a new Array instance from an array-like or iterable object. :
assert(JSON.stringify(Array.from(o.values())) === JSON.stringify([1, 3, 0]));
but then, the problem then is that Array.from
does not work with the "fake" operator returned by o.values()
.
any chance you could look at it ?
Just wondering if these were available anywhere. Thanks!
Francisco
There’s described exactly the way by which current es6-collections are implemented: MDN weakmap. It points that if object is being removed, weak map keeps reference on it, and that causes leaks.
I think that maybe it’s reasonable to use obtrusive polymer-weakmap approach instead.
Releases should not only be pushed to npm but also be tagged accordingly.
Hi,
I'm not sure if this is the right place, but here goes..
I'm running Aurelia beta 1 and have added
github:webreflection/es6-collections@master and github:polymer/mutationobservers.
also added script references in index.html..
But when I run i IE, I get several errors ..
Out of stack space
Evaluating http://localhost/Webfragt2.Webclient/jspm_packages/npm/[email protected]/modules/es6.map.js
Potentially unhandled rejection [2] Error: Expected object
Error loading http://localhost/Webfragt2.Webclient/jspm_packages/npm/[email protected]/modules/es6.map.js
Unhandled promise rejection Error: Out of stack space
Error loading http://localhost/Webfragt2.Webclient/jspm_packages/npm/[email protected]
I have just checked draft from 18th of July and these two are still in there, so why you have removed them ?
23.1.3.8 Map.prototype.keys ( )
The following steps are taken:
1. Let M be the this value.
2. Return the result of calling the CreateMapIterator abstract operation with arguments M and "key".
23.1.3.11 Map.prototype.values ( )
The following steps are taken:
1. Let M be the this value.
2. Return the result of calling the CreateMapIterator abstract operation with arguments M and "value".
_keys variable is not initialized after running clear method .
var m = new Map();
m.set("a", 1);
m.set("b", 2);
m.clear();
//{_itp: Array[0], _keys: Array[2], _values: Array[0], objectOnly: undefined, size: 0}
The basic use-case I have is to copy a Set, but according to the docs it should work for any Iterable:
> require("es6-shim");
{}
> var a = new Set(new Set([1,2]));
undefined
> a.has(1);
true
> require("es6-collections");
{}
> var a = new Set(new Set([1,2]));
undefined
> a.has(1);
false
IE11 has map/set, but they are pretty crippled (set/add don't return the map/set, the constructor does nothing when you pass an iterable, etc).
I would like to simply replace the build-in versions in IE11 by this ones. Maybe you can run a feature check (ex: if Set exists, do an .add over it and check that method returns the same Set. Also, pass an array with one value on the ctor and check that that value is indeed on the Set).
Unfortunately, Object.is doesn't exist on PhantomJS nor IE or Safari, leading to runtime errors:
It's also not correct according to the semantics of ES6 collections:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality
vs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#Description
It would be better to define a function like:
is = function(a,b) { return isNaN(a)? isNaN(b): a === b; }
As far there's no WeakSet in the build.
I hoped to polyfill it in lifecycle-events, but found with surprise that it's not covered.
I had to read docs more carefully, but still ).
For now I use stupid workaround:
var WeakSet = typeof WeakSet === 'undefined' ? Set : WeakSet;
It works in es6-shim, and as described in MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
However, in version 0.2.1 of es6-collections this does not work:
> require("es6-collections");
{ WeakMap: [Function: WeakMap],
Map: [Function: Map],
Set: [Function: Set] }
> var a = new Set([1,2]);
undefined
> a.has(1);
false
by contrast:
> require("es6-shim");
{}
> var a = new Set([1,2]);
undefined
> a.has(1)
true
@WebReflection could you add a webhook for testling via this manual, please.
other point we identified that is different from the latest es6 spec, is that Map#set
expects 0
and -0
keys to be unique, which was only valid in an earlier version of the standard
@see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality
In earlier versions of the ECMAScript 6 draft -0 and +0 were considered distinct (even though -0 === +0), this has been changed in later versions and has been adapted in Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26) (bug 952870) and a recent nightly Chrome.
https://gist.github.com/1269991
Or implement something equivelant.
For example, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
An iterator is just an object with a next() method, as per:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol
Chrome 36 implements WeakMap
, but not Map
or Set
. The entire polyfill doesn't run because of the initial check: "WeakMap" in this
which passes.
Suggest simply changing the initial check to:
"WeakMap" in this && "Set" in this && "Map" in this || ...
Everything else should work, as you're already checking ... = window.Map || Map
during assignment.
Hi,
Just wondering what versions of Internet Explorer this library supports.
Thanks
According to MDN, size is a property not a function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set (same for Map, etc)
es6-shim implements this correctly, but es6-collections does not:
> require("es6-collections");
{}
> var a = new Set([1,2,3]);
undefined
> a.size
[Function: sharedSize]
> a.size()
3
> require("es6-shim");
{}
> var a = new Set([1,2,3]);
undefined
> a.size()
TypeError: Property 'size' of object #<Set> is not a function
at repl:1:4
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
> a.size
3
The following check for "Map" will throw exception because IE11 has defined "Map" without "values()":
"undefined"!=typeof Map&&(new Map).values().next
reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values:
Maybe it should check if function "values()" exists at first.
WeakMap is not completely polyfillable but its core feature is that data can be garbage collected once an object is collected. Implementing this feature should be possible by using a non-enumerable rare key (e.g. a random large string) to which UID of a specific WeakMap would be appended.
Am I missing sth?
Try this on IE10:
var map = new Map();
map.set('d', 2);
map.set('e', 3);
map.set('f', 4);
map.set(NaN, 'Not number'); // overwrites f => 4
console.log(map.get('f')); // outputs: 'Not number'
It seems that NaN
key will overwrite the last key on the list, because set()
thinks that NaN
is in the list.
Related:
var map = new Map();
map.set('a', 1);
console.log(map.has(NaN)); // outputs: true
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.