es6-collections
Advanced tools
Comparing version 0.3.2 to 0.3.4
@@ -1,4 +0,4 @@ | ||
(function(f){function g(a){return function e(c){if(!this||this.constructor!==e)return new e(c);this._keys=[];this._values=[];this.objectOnly=a;if(c)if(this.add)for(var b=c.length;b--;this.add(c[b]));else for(b=c.length;b--;this.set(c[b][0],c[b][1]));}}function h(a){this.has(a)&&(this._keys.splice(b,1),this._values.splice(b,1));return-1<b}function q(a){return this.has(a)?this._values[b]:void 0}function r(a,d){if(this.objectOnly&&d!==Object(d))throw new TypeError("Invalid value used as weak collection key"); | ||
if(d!=d||0===d)for(b=a.length;b--&&!y(a[b],d););else b=a.indexOf(d);return-1<b}function s(a){return r.call(this,this._values,a)}function t(a){return r.call(this,this._keys,a)}function u(a,d){this.has(a)?this._values[b]=d:this._values[this._keys.push(a)-1]=d}function k(){this._values.length=0}function v(){return this._values.slice()}function z(){return this._keys.slice()}function w(){return this._values.length}function A(a,b){var e=this,c=e._values.slice();e._keys.slice().forEach(function(f,g){a.call(b, | ||
f,c[g],e)})}function B(a,b){var e=this;e._values.slice().forEach(function(c){a.call(b,c,c,e)})}function x(a){!this.has(a)&&!!this._values.push(a)}if("undefined"==typeof l){var l=g(!0);l.prototype={constructor:l,"delete":h,clear:k,get:q,has:t,set:u};f.WeakMap=l}if("undefined"==typeof m){var m=g();m.prototype={constructor:m,"delete":h,has:t,get:q,set:u,size:w,keys:z,values:v,forEach:A,clear:k};f.Map=m}if("undefined"==typeof n){var n=g();n.prototype={constructor:n,has:s,add:x,"delete":h,clear:k,size:w, | ||
values:v,forEach:B};f.Set=n}if("undefined"==typeof p){var p=g(!0);p.prototype={constructor:p,"delete":h,add:x,clear:k,has:s};f.WeakSet=p}var b,y=Object.is})("undefined"==typeof exports?window:global); | ||
(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&&s.call(this,a)}b||t(a,"size",{get:u});a.constructor=e;e.prototype=a;return e}function s(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--&&!v(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 w(){return this._keys.slice()}function u(){return this._values.length}function x(a,b){var e= | ||
this,c=e._values.slice();e._keys.slice().forEach(function(d,f){a.call(b,d,c[f],e)})}function y(a,b){var c=this;c._values.slice().forEach(function(d){a.call(b,d,d,c)})}var c,t=Object.defineProperty,v=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:w,values:r,forEach:x,clear:h}));"undefined"==typeof Set&&(d.Set=f({has:m,add:q,"delete":g,clear:h,values:r,forEach:y}));"undefined"==typeof WeakSet&& | ||
(d.WeakSet=f({"delete":g,add:q,clear:h,has:m},!0))})("undefined"==typeof exports?window:global); |
83
index.js
(function (exports) {'use strict'; | ||
//shared pointer | ||
var i; | ||
//shortcuts | ||
var defineProperty = Object.defineProperty, is = Object.is; | ||
//Polyfill global objects | ||
if (typeof WeakMap == 'undefined') { | ||
var WeakMap = createCollection(true); | ||
WeakMap.prototype = { | ||
constructor: WeakMap, | ||
exports.WeakMap = createCollection({ | ||
// WeakMap#delete(key:void*):boolean | ||
@@ -16,10 +21,7 @@ 'delete': sharedDelete, | ||
set: sharedSet | ||
}; | ||
exports.WeakMap = WeakMap; | ||
}, true); | ||
} | ||
if (typeof Map == 'undefined') { | ||
var Map = createCollection(); | ||
Map.prototype = { | ||
constructor: Map, | ||
exports.Map = createCollection({ | ||
// WeakMap#delete(key:void*):boolean | ||
@@ -34,4 +36,2 @@ 'delete': sharedDelete, | ||
set: sharedSet, | ||
// Map#size(void):number === Mozilla only so far | ||
size: sharedSize, | ||
// Map#keys(void):Array === not in specs | ||
@@ -45,14 +45,11 @@ keys: sharedKeys, | ||
clear: sharedClear | ||
}; | ||
exports.Map = Map; | ||
}); | ||
} | ||
if (typeof Set == 'undefined') { | ||
var Set =createCollection(); | ||
Set.prototype = { | ||
constructor: Set, | ||
exports.Set = createCollection({ | ||
// Set#has(value:void*):boolean | ||
has: setHas, | ||
// Set#add(value:void*):boolean | ||
add: sharedSetAdd, | ||
add: sharedAdd, | ||
// Set#delete(key:void*):boolean | ||
@@ -62,4 +59,2 @@ 'delete': sharedDelete, | ||
clear: sharedClear, | ||
// Set#size(void):number === Mozilla only | ||
size: sharedSize, | ||
// Set#values(void):Array === not in specs | ||
@@ -69,14 +64,11 @@ values: sharedValues, | ||
forEach: sharedSetIterate | ||
}; | ||
exports.Set = Set; | ||
}); | ||
} | ||
if (typeof WeakSet == 'undefined') { | ||
var WeakSet = createCollection(true); | ||
WeakSet.prototype = { | ||
constructor: WeakSet, | ||
exports.WeakSet = createCollection({ | ||
// WeakSet#delete(key:void*):boolean | ||
'delete': sharedDelete, | ||
// WeakSet#add(value:void*):boolean | ||
add: sharedSetAdd, | ||
add: sharedAdd, | ||
// WeakSet#clear(): | ||
@@ -86,4 +78,3 @@ clear: sharedClear, | ||
has: setHas | ||
}; | ||
exports.WeakSet = WeakSet; | ||
}, true); | ||
} | ||
@@ -96,4 +87,4 @@ | ||
*/ | ||
function createCollection(objectOnly){ | ||
return function Collection(a){ | ||
function createCollection(proto, objectOnly){ | ||
function Collection(a){ | ||
if (!this || this.constructor !== Collection) return new Collection(a); | ||
@@ -106,19 +97,28 @@ this._keys = []; | ||
if (a) init.call(this, a); | ||
}; | ||
} | ||
} | ||
//define size for non object-only collections | ||
if (!objectOnly) { | ||
defineProperty(proto, 'size', { | ||
get: sharedSize | ||
}); | ||
} | ||
//shared pointer | ||
var i; | ||
var is = Object.is; | ||
//set prototype | ||
proto.constructor = Collection; | ||
Collection.prototype = proto; | ||
return Collection; | ||
} | ||
/** parse initial iterable argument passed */ | ||
function init(a){ | ||
var i; | ||
//init Set argument, like `[1,2,3,{}]` | ||
if (this.add) | ||
for (var i = a.length; i--; this.add(a[i])); | ||
a.forEach(this.add, this); | ||
//init Map argument like `[[1,2], [{}, 4]]` | ||
else | ||
for (var i = a.length; i--; this.set(a[i][0], a[i][1])); | ||
a.forEach(function(a){this.set(a[0],a[1])}, this); | ||
} | ||
@@ -158,2 +158,3 @@ | ||
/** @chainable */ | ||
function sharedSet(key, value) { | ||
@@ -165,4 +166,11 @@ this.has(key) ? | ||
; | ||
return this; | ||
} | ||
/** @chainable */ | ||
function sharedAdd(value) { | ||
if (!this.has(value)) this._values.push(value); | ||
return this; | ||
} | ||
function sharedClear() { | ||
@@ -200,7 +208,2 @@ this._values.length = 0; | ||
/** Set#add recycled through bind per each instanceof Set */ | ||
function sharedSetAdd(value) { | ||
!this.has(value) && !!this._values.push(value); | ||
} | ||
})(typeof exports == 'undefined' ? window : global); |
{ | ||
"name": "es6-collections", | ||
"version": "0.3.2", | ||
"version": "0.3.4", | ||
"description": "ES6 Harmony like collections such Map, WeakMap, and Set", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/WebReflection/es6-collections", |
@@ -108,3 +108,3 @@ var assert = require('better-assert'); | ||
assert(m.has(c)); | ||
assert(m.size(), 3); | ||
assert(m.size, 3); | ||
if ("__proto__" in {}) { | ||
@@ -121,7 +121,7 @@ assert(Map().__proto__.isPrototypeOf(Map())); | ||
if ("size" in o) { | ||
assert(o.size() === 0); | ||
assert(o.size === 0); | ||
o.set("a", "a"); | ||
assert(o.size() === 1); | ||
assert(o.size === 1); | ||
o["delete"]("a"); | ||
assert(o.size() === 0); | ||
assert(o.size === 0); | ||
} | ||
@@ -249,3 +249,3 @@ }); | ||
o.clear(); | ||
assert(!o.size()); | ||
assert(!o.size); | ||
assert(!o.values().length); | ||
@@ -264,3 +264,3 @@ }); | ||
assert(s.has(2)); | ||
assert(s.size(), 2); | ||
assert(s.size, 2); | ||
if ("__proto__" in {}) { | ||
@@ -277,7 +277,7 @@ assert(Set().__proto__.isPrototypeOf(Set())); | ||
if ("size" in o) { | ||
assert(o.size() === 0); | ||
assert(o.size === 0); | ||
o.add("a"); | ||
assert(o.size() === 1); | ||
assert(o.size === 1); | ||
o["delete"]("a"); | ||
assert(o.size() === 0); | ||
assert(o.size === 0); | ||
} | ||
@@ -288,3 +288,3 @@ }); | ||
var o = Set(); | ||
assert(o.add(NaN) === undefined); | ||
assert(o.add(NaN)); | ||
assert(o.has(NaN)); | ||
@@ -355,3 +355,3 @@ }); | ||
o.clear(); | ||
assert(!o.size()); | ||
assert(!o.size); | ||
assert(!o.values().length); | ||
@@ -368,5 +368,5 @@ }); | ||
assert(WeakSet() instanceof WeakSet); | ||
var a = {}; | ||
var s = WeakSet([a]); | ||
assert(s.has(a)); | ||
var a = {}, b = {}; | ||
var s = WeakSet([a, b]); | ||
assert(s.has(a) && s.has(b)); | ||
if ("__proto__" in {}) { | ||
@@ -377,2 +377,27 @@ assert(WeakSet().__proto__.isPrototypeOf(WeakSet())); | ||
}); | ||
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 a = {}, b = {}; | ||
s.add(1).add(2); | ||
assert(s.has(1) && s.has(2) && s.size === 2); | ||
ws.add(a).add(b); | ||
assert(ws.has(a) && ws.has(b)); | ||
m.set(1, 1).set(a, 2); | ||
assert(m.has(1) && m.has(a) && m.size === 2); | ||
wm.set(a, b).set(b, a); | ||
assert(wm.get(a) === b && wm.get(b) === a); | ||
}); | ||
it("Recognize any iterable as the constructor input", function(){ | ||
var a = new Set(new Set([1,2])); | ||
assert(a.has(1)); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25105
533
0