Permalink
Browse files

Fixing an edge case for forEach

  • Loading branch information...
1 parent 3907d45 commit 2e2846b5df829c1c4bdfa10699b5a02fd5394091 @eriwen committed Nov 16, 2012
Showing with 36 additions and 11 deletions.
  1. +2 −2 component.json
  2. +19 −6 es6-map-shim.js
  3. +1 −1 package.json
  4. +14 −2 spec/es6-map-shim-test.js
View
4 component.json
@@ -4,12 +4,12 @@
"description": "A forward polyfill for ES6 Maps",
"author": "Eric Wendelin",
"homepage": "http://eriwen.github.com/es6-map-shim/",
- "version": "0.2",
+ "version": "0.2.1",
"keywords": ["es6", "harmony", "map", "shim", "polyfill"],
"dependencies": {},
"development": {},
"license": "MIT",
"scripts": [
"es6-map-shim.js"
]
-}
+}
View
25 es6-map-shim.js
@@ -90,7 +90,7 @@
throw new TypeError('Invalid Map');
}
- return Object.create({}, {
+ return Object.create(MapPrototype, {
items:{
value:function() {
return [].slice.call(_items);
@@ -150,11 +150,22 @@
throw new TypeError('Invalid callback function given to forEach');
}
- for (var i = 0; i < _keys.length; i++) {
- if (this.has(_keys[i])) {
- callbackfn.apply(arguments[1], [_values[i], _keys[i], this]);
+ function tryNext() {
+ try {
+ return iter.next();
+ } catch(e) {
+ return undefined;
}
}
+
+ var iter = this.iterator();
+ var current = tryNext();
+ var next = tryNext();
+ while(current !== undefined) {
+ callbackfn.apply(arguments[1], [current[1], current[0], this]);
+ current = next;
+ next = tryNext();
+ }
}
},
iterator:{
@@ -170,10 +181,12 @@
});
}
- Map.prototype = MapPrototype = Map();
-
var notInNode = module == 'undefined';
var window = notInNode ? this : global;
var module = notInNode ? {} : exports;
+ var MapPrototype = Map.prototype;
+
+ Map.prototype = MapPrototype = Map();
+
window.Map = module.Map = window.Map || Map;
}.call(this, typeof exports));
View
2 package.json
@@ -2,7 +2,7 @@
"name": "es6-map-shim",
"description": "A polyfill ES6 Map that closely follows the latest spec",
"author": "Eric Wendelin <me@eriwen.com> (http://eriwen.com)",
- "version": "0.2.0",
+ "version": "0.2.1",
"keywords": ["es6", "harmony", "map", "shim", "polyfill"],
"homepage": "http://eriwen.github.com/es6-map-shim/",
"repository": {
View
16 spec/es6-map-shim-test.js
@@ -246,15 +246,27 @@ describe('es6-map-shim', function() {
});
it('should skip items that are deleted before being visited', function() {
- var map = new Map([['foo', 'bar'], [65, 42]]);
+ var map = new Map([['foo', 'bar'], [65, 42], [null, function(){}]]);
var sums = [];
map.forEach(function(value, key, mapRef) {
mapRef.delete(65);
sums.push(value + key);
}, {'foo': function(){}});
- expect(sums.length).toBe(1);
+ expect(sums.length).toBe(2);
expect(sums[0]).toEqual('barfoo');
});
+
+ it('should not skip anything if items are deleted inline', function() {
+ var map = new Map([['foo', 'bar'], [65, 42]]);
+ var sums = [];
+ map.forEach(function(value, key, mapRef) {
+ mapRef.delete(key);
+ sums.push(value + key);
+ });
+ expect(sums.length).toBe(2);
+ expect(sums[0]).toEqual('barfoo');
+ expect(sums[1]).toEqual(107);
+ });
});
describe('Map::iterator', function() {

0 comments on commit 2e2846b

Please sign in to comment.