es6-collections
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -1,4 +0,5 @@ | ||
(function(e){function f(a,b){function c(a){if(!this||this.constructor!==c)return new c(a);this._keys=[];this._values=[];this.objectOnly=b;a&&u.call(this,a)}b||v(a,"size",{get:w});a.constructor=c;c.prototype=a;return c}function u(a){this.add?a.forEach(this.add,this):a.forEach(function(a){this.set(a[0],a[1])},this)}function g(a){this.has(a)&&(this._keys.splice(d,1),this._values.splice(d,1));return-1<d}function m(a){return this.has(a)?this._values[d]:void 0}function n(a,b){if(this.objectOnly&&b!==Object(b))throw new TypeError("Invalid value used as weak collection key"); | ||
if(b!=b||0===b)for(d=a.length;d--&&!x(a[d],b););else d=a.indexOf(b);return-1<d}function p(a){return n.call(this,this._values,a)}function q(a){return n.call(this,this._keys,a)}function r(a,b){this.has(a)?this._values[d]=b:this._values[this._keys.push(a)-1]=b;return this}function t(a){this.has(a)||this._values.push(a);return this}function h(){this._values.length=0}function y(){return k(this._keys)}function l(){return k(this._values)}function z(){return k(this._keys,this._values)}function A(){return k(this._values, | ||
this._values)}function k(a,b){var c=0,d=!1;return{next:function(){var e;!d&&c<a.length?(e=b?[a[c],b[c]]:a[c],c+=1):d=!0;return{done:d,value:e}}}}function w(){return this._values.length}function B(a,b){var c=this,d=c._values.slice();c._keys.slice().forEach(function(e,f){a.call(b,d[f],e,c)})}function C(a,b){var c=this;c._values.slice().forEach(function(d){a.call(b,d,d,c)})}var d,v=Object.defineProperty,x=function(a,b){return isNaN(a)?isNaN(b):a===b};"undefined"==typeof WeakMap&&(e.WeakMap=f({"delete":g, | ||
clear:h,get:m,has:q,set:r},!0));"undefined"==typeof Map&&(e.Map=f({"delete":g,has:q,get:m,set:r,keys:y,values:l,entries:z,forEach:B,clear:h}));"undefined"==typeof Set&&(e.Set=f({has:p,add:t,"delete":g,clear:h,keys:l,values:l,entries:A,forEach:C}));"undefined"==typeof WeakSet&&(e.WeakSet=f({"delete":g,add:t,clear:h,has:p},!0))})("undefined"!=typeof exports&&"undefined"!=typeof global?global:window); | ||
(function(e){function f(a,c){function b(a){if(!this||this.constructor!==b)return new b(a);this._keys=[];this._values=[];this._itp=[];this.objectOnly=c;a&&v.call(this,a)}c||w(a,"size",{get:x});a.constructor=b;b.prototype=a;return b}function v(a){this.add?a.forEach(this.add,this):a.forEach(function(a){this.set(a[0],a[1])},this)}function d(a){this.has(a)&&(this._keys.splice(b,1),this._values.splice(b,1),this._itp.forEach(function(a){b<a[0]&&a[0]--}));return-1<b}function m(a){return this.has(a)?this._values[b]: | ||
void 0}function n(a,c){if(this.objectOnly&&c!==Object(c))throw new TypeError("Invalid value used as weak collection key");if(c!=c||0===c)for(b=a.length;b--&&!y(a[b],c););else b=a.indexOf(c);return-1<b}function p(a){return n.call(this,this._values,a)}function q(a){return n.call(this,this._keys,a)}function r(a,c){this.has(a)?this._values[b]=c:this._values[this._keys.push(a)-1]=c;return this}function t(a){this.has(a)||this._values.push(a);return this}function h(){this._values.length=0}function z(){return k(this._itp, | ||
this._keys)}function l(){return k(this._itp,this._values)}function A(){return k(this._itp,this._keys,this._values)}function B(){return k(this._itp,this._values,this._values)}function k(a,c,b){var g=[0],e=!1;a.push(g);return{next:function(){var f,d=g[0];!e&&d<c.length?(f=b?[c[d],b[d]]:c[d],g[0]++):(e=!0,a.splice(a.indexOf(g),1));return{done:e,value:f}}}}function x(){return this._values.length}function u(a,c){for(var b=this.entries();;){var d=b.next();if(d.done)break;a.call(c,d.value[1],d.value[0], | ||
this)}}var b,w=Object.defineProperty,y=function(a,b){return isNaN(a)?isNaN(b):a===b};"undefined"==typeof WeakMap&&(e.WeakMap=f({"delete":d,clear:h,get:m,has:q,set:r},!0));"undefined"!=typeof Map&&(new Map).values().next||(e.Map=f({"delete":d,has:q,get:m,set:r,keys:z,values:l,entries:A,forEach:u,clear:h}));"undefined"!=typeof Set&&(new Set).values().next||(e.Set=f({has:p,add:t,"delete":d,clear:h,keys:l,values:l,entries:B,forEach:u}));"undefined"==typeof WeakSet&&(e.WeakSet=f({"delete":d,add:t,clear:h, | ||
has:p},!0))})("undefined"!=typeof exports&&"undefined"!=typeof global?global:window); |
49
index.js
@@ -24,3 +24,3 @@ (function (exports) {'use strict'; | ||
if (typeof Map == 'undefined') { | ||
if (typeof Map == 'undefined' || !(new Map).values().next) { | ||
exports.Map = createCollection({ | ||
@@ -49,3 +49,3 @@ // WeakMap#delete(key:void*):boolean | ||
if (typeof Set == 'undefined') { | ||
if (typeof Set == 'undefined' || !(new Set).values().next) { | ||
exports.Set = createCollection({ | ||
@@ -67,3 +67,3 @@ // Set#has(value:void*):boolean | ||
// Set#forEach(callback:Function, context:void*):void ==> callback.call(context, value, index) === not in specs | ||
forEach: sharedSetIterate | ||
forEach: sharedForEach | ||
}); | ||
@@ -95,2 +95,3 @@ } | ||
this._values = []; | ||
this._itp = []; // iteration pointers | ||
this.objectOnly = objectOnly; | ||
@@ -134,2 +135,4 @@ | ||
this._values.splice(i, 1); | ||
// update iteration pointers | ||
this._itp.forEach(function(p) { if (i < p[0]) p[0]--; }); | ||
} | ||
@@ -183,27 +186,29 @@ // Aurora here does it while Canary doesn't | ||
function sharedKeys() { | ||
return sharedIterator(this._keys); | ||
return sharedIterator(this._itp, this._keys); | ||
} | ||
function sharedValues() { | ||
return sharedIterator(this._values); | ||
return sharedIterator(this._itp, this._values); | ||
} | ||
function mapEntries() { | ||
return sharedIterator(this._keys, this._values); | ||
return sharedIterator(this._itp, this._keys, this._values); | ||
} | ||
function setEntries() { | ||
return sharedIterator(this._values, this._values); | ||
return sharedIterator(this._itp, this._values, this._values); | ||
} | ||
function sharedIterator(array, array2) { | ||
var j = 0, done = false; | ||
function sharedIterator(itp, array, array2) { | ||
var p = [0], done = false; | ||
itp.push(p); | ||
return { | ||
next: function() { | ||
var v; | ||
if (!done && j < array.length) { | ||
v = array2 ? [array[j], array2[j]]: array[j]; | ||
j += 1; | ||
var v, k = p[0]; | ||
if (!done && k < array.length) { | ||
v = array2 ? [array[k], array2[k]]: array[k]; | ||
p[0]++; | ||
} else { | ||
done = true; | ||
itp.splice(itp.indexOf(p), 1); | ||
} | ||
@@ -220,16 +225,10 @@ return { done: done, value: v }; | ||
function sharedForEach(callback, context) { | ||
var self = this; | ||
var values = self._values.slice(); | ||
self._keys.slice().forEach(function(key, n){ | ||
callback.call(context, values[n], key, self); | ||
}); | ||
var it = this.entries(); | ||
for (;;) { | ||
var r = it.next(); | ||
if (r.done) break; | ||
callback.call(context, r.value[1], r.value[0], this); | ||
} | ||
} | ||
function sharedSetIterate(callback, context) { | ||
var self = this; | ||
self._values.slice().forEach(function(value){ | ||
callback.call(context, value, value, self); | ||
}); | ||
} | ||
})(typeof exports != 'undefined' && typeof global != 'undefined' ? global : window ); |
{ | ||
"name": "es6-collections", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "ES6 Harmony like collections such Map, WeakMap, and Set", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/WebReflection/es6-collections", |
@@ -0,1 +1,8 @@ | ||
// test the polyfill | ||
(this.window || global).Map = undefined; | ||
(this.window || global).Set = undefined; | ||
(this.window || global).WeakMap = undefined; | ||
(this.window || global).WeakSet = undefined; | ||
var assert = require('better-assert'); | ||
@@ -261,2 +268,21 @@ // require('../index'); | ||
it("Map#forEach with mutations", function () { | ||
var o = new Map([["0", 0], ["1", 1], ["2", 2]]), seen = []; | ||
o.forEach(function (value, key, obj) { | ||
seen.push(value); | ||
assert(obj === o); | ||
assert(""+value === key); | ||
// mutations work as expected | ||
if (value === 1) { | ||
o.delete("0"); // remove from before current index | ||
o.delete("2"); // remove from after current index | ||
o.set("3", 3); // insertion | ||
} else if (value === 3) { | ||
o.set("0", 0); // insertion at the end | ||
} | ||
}); | ||
assert(JSON.stringify(seen) === JSON.stringify([0, 1, 3, 0])); | ||
assert(JSON.stringify(o._values) === JSON.stringify([1, 3, 0])); | ||
}); | ||
it("Map#clear", function(){ | ||
@@ -385,2 +411,21 @@ var o = new Map(); | ||
it("Set#forEach with mutations", function () { | ||
var o = new Set([0, 1, 2]), seen = []; | ||
o.forEach(function (value, sameValue, obj) { | ||
seen.push(value); | ||
assert(obj === o); | ||
assert(value === sameValue); | ||
// mutations work as expected | ||
if (value === 1) { | ||
o.delete(0); // remove from before current index | ||
o.delete(2); // remove from after current index | ||
o.add(3); // insertion | ||
} else if (value === 3) { | ||
o.add(0); // insertion at the end | ||
} | ||
}); | ||
assert(JSON.stringify(seen) === JSON.stringify([0, 1, 3, 0])); | ||
assert(JSON.stringify(o._values) === JSON.stringify([1, 3, 0])); | ||
}); | ||
it("Set#clear", function(){ | ||
@@ -394,3 +439,2 @@ var o = new Set(); | ||
it("WeakSet existence", function () { | ||
@@ -397,0 +441,0 @@ assert(WeakSet); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29946
643