es6-collections
Advanced tools
Comparing version 0.4.1 to 0.5.1
@@ -1,4 +0,4 @@ | ||
(function(d){function f(a,b){function e(a){if(!this||this.constructor!==e)return new e(a);this._keys=[];this._values=[];this.objectOnly=b;a&&t.call(this,a)}b||u(a,"size",{get:v});a.constructor=e;e.prototype=a;return e}function t(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(c,1),this._values.splice(c,1));return-1<c}function k(a){return this.has(a)?this._values[c]:void 0}function l(a,b){if(this.objectOnly&&b!==Object(b))throw new TypeError("Invalid value used as weak collection key"); | ||
if(b!=b||0===b)for(c=a.length;c--&&!w(a[c],b););else c=a.indexOf(b);return-1<c}function m(a){return l.call(this,this._values,a)}function n(a){return l.call(this,this._keys,a)}function p(a,b){this.has(a)?this._values[c]=b:this._values[this._keys.push(a)-1]=b;return this}function q(a){this.has(a)||this._values.push(a);return this}function h(){this._values.length=0}function r(){return this._values.slice()}function x(){return this._keys.slice()}function v(){return this._values.length}function y(a,b){var e= | ||
this,c=e._values.slice();e._keys.slice().forEach(function(d,f){a.call(b,c[f],d,e)})}function z(a,b){var c=this;c._values.slice().forEach(function(d){a.call(b,d,d,c)})}var c,u=Object.defineProperty,w=Object.is;"undefined"==typeof WeakMap&&(d.WeakMap=f({"delete":g,clear:h,get:k,has:n,set:p},!0));"undefined"==typeof Map&&(d.Map=f({"delete":g,has:n,get:k,set:p,keys:x,values:r,forEach:y,clear:h}));"undefined"==typeof Set&&(d.Set=f({has:m,add:q,"delete":g,clear:h,values:r,forEach:z}));"undefined"==typeof WeakSet&& | ||
(d.WeakSet=f({"delete":g,add:q,clear:h,has:m},!0))})("undefined"!=typeof exports&&"undefined"!=typeof global?global:window); | ||
(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); |
46
index.js
@@ -5,3 +5,3 @@ (function (exports) {'use strict'; | ||
//shortcuts | ||
var defineProperty = Object.defineProperty, is = Object.is; | ||
var defineProperty = Object.defineProperty, is = function(a,b) { return isNaN(a)? isNaN(b): a === b; }; | ||
@@ -36,6 +36,8 @@ | ||
set: sharedSet, | ||
// Map#keys(void):Array === not in specs | ||
// Map#keys(void):Iterator | ||
keys: sharedKeys, | ||
// Map#values(void):Array === not in specs | ||
// Map#values(void):Iterator | ||
values: sharedValues, | ||
// Map#entries(void):Iterator | ||
entries: mapEntries, | ||
// Map#forEach(callback:Function, context:void*):void ==> callback.call(context, key, value, mapObject) === not in specs` | ||
@@ -58,4 +60,8 @@ forEach: sharedForEach, | ||
clear: sharedClear, | ||
// Set#values(void):Array === not in specs | ||
// Set#keys(void):Iterator | ||
keys: sharedValues, // specs actually say "the same function object as the initial value of the values property" | ||
// Set#values(void):Iterator | ||
values: sharedValues, | ||
// Set#entries(void):Iterator | ||
entries: setEntries, | ||
// Set#forEach(callback:Function, context:void*):void ==> callback.call(context, value, index) === not in specs | ||
@@ -140,3 +146,3 @@ forEach: sharedSetIterate | ||
//NaN or 0 passed | ||
if (key != key || key === 0) for (i = list.length; i-- && !is(list[i], key);); | ||
if (key != key || key === 0) for (i = list.length; i-- && !is(list[i], key);){} | ||
else i = list.indexOf(key); | ||
@@ -175,10 +181,34 @@ return -1 < i; | ||
/** keys, values, and iterate related methods */ | ||
function sharedKeys() { | ||
return sharedIterator(this._keys); | ||
} | ||
function sharedValues() { | ||
return this._values.slice(); | ||
return sharedIterator(this._values); | ||
} | ||
function sharedKeys() { | ||
return this._keys.slice(); | ||
function mapEntries() { | ||
return sharedIterator(this._keys, this._values); | ||
} | ||
function setEntries() { | ||
return sharedIterator(this._values, this._values); | ||
} | ||
function sharedIterator(array, array2) { | ||
var j = 0, done = false; | ||
return { | ||
next: function() { | ||
var v; | ||
if (!done && j < array.length) { | ||
v = array2 ? [array[j], array2[j]]: array[j]; | ||
j += 1; | ||
} else { | ||
done = true; | ||
} | ||
return { done: done, value: v }; | ||
} | ||
}; | ||
} | ||
function sharedSize() { | ||
@@ -185,0 +215,0 @@ return this._values.length; |
{ | ||
"name": "es6-collections", | ||
"version": "0.4.1", | ||
"version": "0.5.1", | ||
"description": "ES6 Harmony like collections such Map, WeakMap, and Set", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/WebReflection/es6-collections", |
@@ -12,6 +12,6 @@ var assert = require('better-assert'); | ||
assert(new WeakMap instanceof WeakMap); | ||
assert(WeakMap() instanceof WeakMap); | ||
assert(new WeakMap() instanceof WeakMap); | ||
var a = {}; | ||
var b = {}; | ||
var c = WeakMap(); | ||
var c = new WeakMap(); | ||
var m = new WeakMap([[a,1], [b,2], [c, 3]]); | ||
@@ -22,4 +22,4 @@ assert(m.has(a)); | ||
if ("__proto__" in {}) { | ||
assert(WeakMap().__proto__.isPrototypeOf(WeakMap())); | ||
assert(WeakMap().__proto__ === WeakMap.prototype); | ||
assert((new WeakMap).__proto__.isPrototypeOf(new WeakMap())); | ||
assert((new WeakMap).__proto__ === WeakMap.prototype); | ||
} | ||
@@ -30,3 +30,3 @@ }); | ||
var | ||
o = WeakMap(), | ||
o = new WeakMap(), | ||
generic = {}, | ||
@@ -42,3 +42,3 @@ callback = function () {} | ||
var | ||
o = WeakMap(), | ||
o = new WeakMap(), | ||
generic = {}, | ||
@@ -55,3 +55,3 @@ callback = function () {} | ||
var | ||
o = WeakMap(), | ||
o = new WeakMap(), | ||
generic = {}, | ||
@@ -72,3 +72,3 @@ callback = function () {} | ||
var | ||
o = WeakMap(), | ||
o = new WeakMap(), | ||
generic = {}, | ||
@@ -91,3 +91,3 @@ callback = function () {} | ||
it("non object key throws an error", function () { | ||
var o = WeakMap(); | ||
var o = new WeakMap(); | ||
try { | ||
@@ -107,6 +107,6 @@ o.set("key", o); | ||
assert(new Map instanceof Map); | ||
assert(Map() instanceof Map); | ||
assert(new Map() instanceof Map); | ||
var a = 1; | ||
var b = {}; | ||
var c = Map(); | ||
var c = new Map(); | ||
var m = new Map([[1,1], [b,2], [c, 3]]); | ||
@@ -118,4 +118,4 @@ assert(m.has(a)); | ||
if ("__proto__" in {}) { | ||
assert(Map().__proto__.isPrototypeOf(Map())); | ||
assert(Map().__proto__ === Map.prototype); | ||
assert((new Map).__proto__.isPrototypeOf(new Map())); | ||
assert((new Map).__proto__ === Map.prototype); | ||
} | ||
@@ -126,3 +126,3 @@ }); | ||
var | ||
o = Map() | ||
o = new Map() | ||
; | ||
@@ -140,3 +140,3 @@ if ("size" in o) { | ||
var | ||
o = Map(), | ||
o = new Map(), | ||
generic = {}, | ||
@@ -152,3 +152,3 @@ callback = function () {} | ||
var | ||
o = Map(), | ||
o = new Map(), | ||
generic = {}, | ||
@@ -165,3 +165,3 @@ callback = function () {} | ||
var | ||
o = Map(), | ||
o = new Map(), | ||
generic = {}, | ||
@@ -185,7 +185,13 @@ callback = function () {} | ||
assert(!o.has(-0)); | ||
assert(!o.has(0)); | ||
o.set(-0, callback); | ||
assert(o.has(-0)); | ||
assert(o.has(0)); | ||
assert(o.get(-0) === callback); | ||
assert(o.get(0) === callback); | ||
o.set(0, generic); | ||
assert(o.has(-0)); | ||
assert(o.get(-0) === callback); | ||
assert(o.has(0)); | ||
assert(o.get(-0) === generic); | ||
assert(o.get(0) === generic); | ||
@@ -196,3 +202,3 @@ }); | ||
var | ||
o = Map(), | ||
o = new Map(), | ||
generic = {}, | ||
@@ -215,3 +221,3 @@ callback = function () {} | ||
it("non object key does not throw an error", function () { | ||
var o = Map(); | ||
var o = new Map(); | ||
try { | ||
@@ -225,21 +231,35 @@ o.set("key", o); | ||
it("keys and values behavior", function () { | ||
var o = Map(); | ||
o.set("key", "value"); | ||
if ("keys" in o) { | ||
assert(o.keys() instanceof Array); | ||
assert(o.keys().length === 1); | ||
assert(o.keys()[0] === "key"); | ||
assert(o.keys(1).join("") === o.keys().join("")); | ||
} | ||
if ("values" in o) { | ||
assert(o.values() instanceof Array); | ||
assert(o.values().length === 1); | ||
assert(o.values()[0] === "value"); | ||
assert(o.values(1).join("") === o.values().join("")); | ||
} | ||
it("keys, values, entries behavior", function () { | ||
// test that things get returned in insertion order as per the specs | ||
var o = new Map([["1", 1], ["2", 2], ["3", 3]]); | ||
var keys = o.keys(), values = o.values(); | ||
var k = keys.next(), v = values.next(); | ||
assert(k.value === "1" && v.value === 1); | ||
o.delete("2"); | ||
k = keys.next(), v = values.next(); | ||
assert(k.value === "3" && v.value === 3); | ||
// insertion of previously-removed item goes to the end | ||
o.set("2", 2); | ||
k = keys.next(), v = values.next(); | ||
assert(k.value === "2" && v.value === 2); | ||
// when called again, new iterator starts from beginning | ||
var entriesagain = o.entries(); | ||
assert(entriesagain.next().value[0] === "1"); | ||
assert(entriesagain.next().value[0] === "3"); | ||
assert(entriesagain.next().value[0] === "2"); | ||
// after a iterator is finished, don't return any more elements | ||
k = keys.next(), v = values.next(); | ||
assert(k.done && v.done); | ||
k = keys.next(), v = values.next(); | ||
assert(k.done && v.done); | ||
o.set("4", 4); | ||
k = keys.next(), v = values.next(); | ||
assert(k.done && v.done); | ||
// new element shows up in iterators that didn't yet finish | ||
assert(entriesagain.next().value[0] === "4"); | ||
assert(entriesagain.next().done); | ||
}); | ||
it("Map#forEach", function () { | ||
var o = Map(), i; | ||
var o = new Map(), i; | ||
o.set("key 0", 0); | ||
@@ -254,3 +274,3 @@ o.set("key 1", 1); | ||
}); | ||
assert(!o.keys().length); | ||
assert(!o.size); | ||
} | ||
@@ -260,3 +280,3 @@ }); | ||
it("Map#clear", function(){ | ||
var o = Map(); | ||
var o = new Map(); | ||
o.set(1, '1'); | ||
@@ -267,3 +287,2 @@ o.set(2, '2'); | ||
assert(!o.size); | ||
assert(!o.values().length); | ||
}); | ||
@@ -277,4 +296,4 @@ | ||
assert(new Set instanceof Set); | ||
assert(Set() instanceof Set); | ||
var s = Set([1,2]); | ||
assert(new Set() instanceof Set); | ||
var s = new Set([1,2]); | ||
assert(s.has(1)); | ||
@@ -284,4 +303,4 @@ assert(s.has(2)); | ||
if ("__proto__" in {}) { | ||
assert(Set().__proto__.isPrototypeOf(Set())); | ||
assert(Set().__proto__ === Set.prototype); | ||
assert((new Set).__proto__.isPrototypeOf(new Set())); | ||
assert((new Set).__proto__ === Set.prototype); | ||
} | ||
@@ -292,3 +311,3 @@ }); | ||
var | ||
o = Set() | ||
o = new Set() | ||
; | ||
@@ -305,3 +324,3 @@ if ("size" in o) { | ||
it("Set#add", function () { | ||
var o = Set(); | ||
var o = new Set(); | ||
assert(o.add(NaN)); | ||
@@ -313,3 +332,3 @@ assert(o.has(NaN)); | ||
var | ||
o = Set(), | ||
o = new Set(), | ||
generic = {}, | ||
@@ -332,10 +351,31 @@ callback = function () {} | ||
it("values behavior", function () { | ||
var o = Set(); | ||
o.add("value"); | ||
if ("values" in o) { | ||
assert(o.values() instanceof Array); | ||
assert(o.values().length === 1); | ||
assert(o.values()[0] === "value"); | ||
assert(o.values(1).join("") === o.values().join("")); | ||
} | ||
// test that things get returned in insertion order as per the specs | ||
var o = new Set([1, 2, 3]); | ||
assert(o.keys === o.values); // same function, as per the specs | ||
var values = o.values(); | ||
var v = values.next(); | ||
assert(v.value === 1); | ||
o.delete(2); | ||
v = values.next(); | ||
assert(v.value === 3); | ||
// insertion of previously-removed item goes to the end | ||
o.add(2); | ||
v = values.next(); | ||
assert(v.value === 2); | ||
// when called again, new iterator starts from beginning | ||
var entriesagain = o.entries(); | ||
assert(entriesagain.next().value[1] === 1); | ||
assert(entriesagain.next().value[1] === 3); | ||
assert(entriesagain.next().value[1] === 2); | ||
// after a iterator is finished, don't return any more elements | ||
v = values.next(); | ||
assert(v.done); | ||
v = values.next(); | ||
assert(v.done); | ||
o.add(4); | ||
v = values.next(); | ||
assert(v.done); | ||
// new element shows up in iterators that didn't yet finish | ||
assert(entriesagain.next().value[1] === 4); | ||
assert(entriesagain.next().done); | ||
}); | ||
@@ -345,3 +385,3 @@ | ||
var | ||
o = Set(), | ||
o = new Set(), | ||
generic = {}, | ||
@@ -356,3 +396,3 @@ callback = function () {} | ||
it("Set#forEach", function () { | ||
var o = Set(), i = 0; | ||
var o = new Set(), i = 0; | ||
o.add("value 0"); | ||
@@ -368,3 +408,3 @@ o.add("value 1"); | ||
}); | ||
assert(!o.values().length); | ||
assert(!o.size); | ||
} | ||
@@ -374,3 +414,3 @@ }); | ||
it("Set#clear", function(){ | ||
var o = Set(); | ||
var o = new Set(); | ||
o.add(1); | ||
@@ -380,3 +420,2 @@ o.add(2); | ||
assert(!o.size); | ||
assert(!o.values().length); | ||
}); | ||
@@ -391,9 +430,9 @@ | ||
assert(new WeakSet instanceof WeakSet); | ||
assert(WeakSet() instanceof WeakSet); | ||
assert((new WeakSet) instanceof WeakSet); | ||
var a = {}, b = {}; | ||
var s = WeakSet([a, b]); | ||
var s = new WeakSet([a, b]); | ||
assert(s.has(a) && s.has(b)); | ||
if ("__proto__" in {}) { | ||
assert(WeakSet().__proto__.isPrototypeOf(WeakSet())); | ||
assert(WeakSet().__proto__ === WeakSet.prototype); | ||
assert((new WeakSet).__proto__.isPrototypeOf(new WeakSet())); | ||
assert((new WeakSet).__proto__ === WeakSet.prototype); | ||
} | ||
@@ -403,6 +442,6 @@ }); | ||
it("Set#add, WeakSet#add, Map#set and WeakMap#set are chainable now", function(){ | ||
var s = Set(); | ||
var ws = WeakSet(); | ||
var m = Map(); | ||
var wm = WeakMap(); | ||
var s = new Set(); | ||
var ws = new WeakSet(); | ||
var m = new Map(); | ||
var wm = new WeakMap(); | ||
var a = {}, b = {}; | ||
@@ -409,0 +448,0 @@ |
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
28142
602