UNPKG

collections-es6

Version:

ES6 Map, Set, WeakMap and WeakSet polyfill in pure ES5

1 lines 18 kB
!function(global,factory){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=factory(global):factory(global)}("undefined"!=typeof window?window:global,function(global){"use strict";var defineProperty=Object.defineProperty,defineProperties=Object.defineProperties,isArray=Array.isArray,create=Object.create,ES6="object"==typeof global.ES6?global.ES6:global.ES6={},isCallable=function(fn){return"function"==typeof fn},Iterator=function(){},MapIterator=function(map,flag){this._map=map,this._flag=flag,this._currentEntry=null,this._done=!1},SetIterator=function(set,flag){this._set=set,this._flag=flag,this._currentEntry=null,this._done=!1},isObject=function(value){return null!==value&&("object"==typeof value||"function"==typeof value)},simpleInheritance=function(child,parent){if("function"!=typeof child||"function"!=typeof parent)throw new TypeError("Child and Parent must be function type");child.prototype=Object.create(parent.prototype),child.prototype.constructor=child};defineProperty(Iterator.prototype,Symbol.iterator.toString(),{value:function(){return this},writable:!0,configurable:!0}),simpleInheritance(MapIterator,Iterator),simpleInheritance(SetIterator,Iterator),defineProperty(MapIterator.prototype,Symbol.toStringTag.toString(),{value:"Map Iterator",configurable:!0}),defineProperty(SetIterator.prototype,Symbol.toStringTag.toString(),{value:"Set Iterator",configurable:!0}),MapIterator.prototype.next=function(){if(!(this instanceof MapIterator))throw new TypeError("Method Map Iterator.prototype.next called on incompatible receiver "+String(this));var nextValue;return this._done?{done:!0,value:void 0}:(null===this._currentEntry?this._currentEntry=this._map._head:this._currentEntry=this._currentEntry.next,null===this._currentEntry?(this._done=!0,{done:!0,value:void 0}):(1===this._flag?nextValue=[this._currentEntry.key,this._currentEntry.value]:2===this._flag?nextValue=this._currentEntry.value:3===this._flag&&(nextValue=this._currentEntry.key),{done:!1,value:nextValue}))},SetIterator.prototype.next=function(){if(!(this instanceof SetIterator))throw new TypeError("Method Set Iterator.prototype.next called on incompatible receiver "+String(this));var nextValue;return this._done?{done:!0,value:void 0}:(null===this._currentEntry?this._currentEntry=this._set._head:this._currentEntry=this._currentEntry.next,null===this._currentEntry?(this._done=!0,{done:!0,value:void 0}):(1===this._flag?nextValue=[this._currentEntry.value,this._currentEntry.value]:2===this._flag&&(nextValue=this._currentEntry.value),{done:!1,value:nextValue}))};var hash=function(key){var strKey=String(key);return void 0===key||null===key?"I___"+strKey:"number"==typeof key?"N___"+strKey:"string"==typeof key?"S___"+strKey:"boolean"==typeof key?"B___"+strKey:null},Map=function Map(iterable){if(!(this instanceof Map)||isMap(this))throw new TypeError("Constructor Map requires 'new'");setupMapInternals(this),null!==iterable&&void 0!==iterable&&ES6.forOf(iterable,function(entry){if(!isObject(entry))throw new TypeError("Iterator value "+entry+" is not an entry object");this.set(entry[0],entry[1])},this)};Map.prototype.set=function(key,value){if(!isMap(this))throw new TypeError("Method Map.prototype.set called on incompatible receiver "+this);var entry,keyHash=hash(key),objectHash=this._objectHash;if(null===keyHash){if("number"==typeof key[objectHash]&&this._data.objects[key[objectHash]]instanceof MapEntry)return(entry=this._data.objects[key[objectHash]]).value=value,this;entry=new MapEntry(key,value),this._data.objects.push(entry),defineProperty(key,objectHash.toString(),{value:this._data.objects.length-1,configurable:!0}),this._size++}else{if(this._data.primitives[keyHash]instanceof MapEntry)return(entry=this._data.primitives[keyHash]).value=value,this;entry=new MapEntry(key,value),this._data.primitives[keyHash]=entry,this._size++}return null===this._head&&(this._head=entry,entry.next=null,entry.prev=null),null===this._tail?this._tail=this._head:(this._tail.next=entry,entry.prev=this._tail,entry.next=null,this._tail=entry),this},Map.prototype.has=function(key){if(!isMap(this))throw new TypeError("Method Map.prototype.has called on incompatible receiver "+this);var keyHash=hash(key),objectHash=this._objectHash;return null===keyHash?"number"==typeof key[objectHash]&&this._data.objects[key[objectHash]]instanceof MapEntry:this._data.primitives[keyHash]instanceof MapEntry},Map.prototype.get=function(key){if(!isMap(this))throw new TypeError("Method Map.prototype.get called on incompatible receiver "+this);var keyHash=hash(key),objectHash=this._objectHash;if(null===keyHash){if("number"==typeof key[objectHash]&&this._data.objects[key[objectHash]]instanceof MapEntry)return this._data.objects[key[objectHash]].value}else if(this._data.primitives[keyHash]instanceof MapEntry)return this._data.primitives[keyHash].value},Map.prototype.clear=function(){if(!isMap(this))throw new TypeError("Method Map.prototype.clear called on incompatible receiver "+this);var entry;Object.getOwnPropertyNames(this._data.primitives).forEach(function(prop){this._data.primitives[prop]instanceof MapEntry&&(entry=this._data.primitives[prop],delete this._data.primitives[prop],entry.next=null,entry.prev=null)},this),Object.getOwnPropertyNames(this._data.objects).forEach(function(prop){this._data.objects[prop]instanceof MapEntry&&(entry=this._data.objects[prop],delete this._data.objects[prop],delete entry.key[this._objectHash],entry.next=null,entry.prev=null)},this),this._data.objects.length=0,this._head=null,this._tail=null,this._size=0},Map.prototype.delete=function(key){if(!isMap(this))throw new TypeError("Method Map.prototype.delete called on incompatible receiver "+this);var entry,keyHash=hash(key),objectHash=this._objectHash;if(null===keyHash){if(!("number"==typeof key[objectHash]&&this._data.objects[key[objectHash]]instanceof MapEntry))return!1;entry=this._data.objects[key[objectHash]],delete this._data.objects[key[objectHash]],delete entry.key[objectHash]}else{if(!(this._data.primitives[keyHash]instanceof MapEntry))return!1;entry=this._data.primitives[keyHash],delete this._data.primitives[keyHash]}return null!==entry.prev&&null!==entry.next?(entry.prev.next=entry.next,entry.next.prev=entry.prev,entry.next=null,entry.prev=null):null===entry.prev&&null!==entry.next&&(this._head=entry.next,entry.next.prev=null,entry.next=null),null!==entry.prev&&null===entry.next?(this._tail=entry.prev,entry.prev.next=null,entry.prev=null):(this._head=null,this._tail=null),this._size--,!0},defineProperty(Map.prototype,"size",{get:function(){if(!isMap(this))throw new TypeError("Method Map.prototype.size called on incompatible receiver "+this);return this._size},configurable:!0}),Map.prototype.entries=function(){if(!isMap(this))throw new TypeError("Method Map.prototype.entries called on incompatible receiver "+this);return new MapIterator(this,1)},Map.prototype.values=function(){if(!isMap(this))throw new TypeError("Method Map.prototype.values called on incompatible receiver "+this);return new MapIterator(this,2)},Map.prototype.keys=function(){if(!isMap(this))throw new TypeError("Method Map.prototype.keys called on incompatible receiver "+this);return new MapIterator(this,3)},Map.prototype.forEach=function(callback,thisArg){if(!isMap(this))throw new TypeError("Method Map.prototype.forEach called on incompatible receiver "+this);if(!isCallable(callback))throw new TypeError(callback+" is not a function");for(var currentEntry=this._head;null!==currentEntry;)callback.call(thisArg,currentEntry.value,currentEntry.key,this),currentEntry=currentEntry.next},Map.prototype[Symbol.iterator]=Map.prototype.entries,defineProperty(Map.prototype,Symbol.toStringTag.toString(),{value:"Map",configurable:!0});var setupMapInternals=function(map){defineProperties(map,{_isMap:{value:!0},_head:{value:null,writable:!0},_tail:{value:null,writable:!0},_objectHash:{value:Symbol("Hash(map)")},_size:{value:0,writable:!0},_data:{value:create(null,{primitives:{value:create(null)},objects:{value:[]}})}})},isMap=function(map){return map instanceof Map&&function(map){return!0===map._isMap&&(null===map._head||map._head instanceof MapEntry)&&(null===map._tail||map._tail instanceof MapEntry)&&ES6.isSymbol(map._objectHash)&&"number"==typeof map._size&&isObject(map._data)&&isObject(map._data.primitives)&&isArray(map._data.objects)}(map)},MapEntry=function(key,value){this.key=key,this.value=value,this.next=null,this.prev=null},Set=function Set(iterable){if(!(this instanceof Set)||isSet(this))throw new TypeError("Constructor Set requires 'new'");setupSetInternals(this),null!==iterable&&void 0!==iterable&&ES6.forOf(iterable,function(entry){this.add(entry)},this)};Set.prototype.add=function(value){if(!isSet(this))throw new TypeError("Method Set.prototype.add called on incompatible receiver "+this);var entry,valueHash=hash(value),objectHash=this._objectHash;if(null===valueHash){if("number"==typeof value[objectHash]&&this._data.objects[value[objectHash]]instanceof SetEntry)return this;entry=new SetEntry(value),this._data.objects.push(entry),defineProperty(value,objectHash.toString(),{value:this._data.objects.length-1,configurable:!0}),this._size++}else{if(this._data.primitives[valueHash]instanceof SetEntry)return this;entry=new SetEntry(value),this._data.primitives[valueHash]=entry,this._size++}return null===this._head&&(this._head=entry,entry.next=null,entry.prev=null),null===this._tail?this._tail=this._head:(this._tail.next=entry,entry.prev=this._tail,entry.next=null,this._tail=entry),this},Set.prototype.has=function(value){if(!isSet(this))throw new TypeError("Method Set.prototype.has called on incompatible receiver "+this);var valueHash=hash(value),objectHash=this._objectHash;return null===valueHash?"number"==typeof value[objectHash]&&this._data.objects[value[objectHash]]instanceof SetEntry:this._data.primitives[valueHash]instanceof SetEntry},Set.prototype.clear=function(){if(!isSet(this))throw new TypeError("Method Set.prototype.clear called on incompatible receiver "+this);var entry;Object.getOwnPropertyNames(this._data.primitives).forEach(function(prop){this._data.primitives[prop]instanceof SetEntry&&(entry=this._data.primitives[prop],delete this._data.primitives[prop],entry.next=null,entry.prev=null)},this),Object.getOwnPropertyNames(this._data.objects).forEach(function(prop){this._data.objects[prop]instanceof SetEntry&&(entry=this._data.objects[prop],delete this._data.objects[prop],delete entry.value[this._objectHash],entry.next=null,entry.prev=null)},this),this._data.objects.length=0,this._head=null,this._tail=null,this._size=0},Set.prototype.delete=function(value){if(!isSet(this))throw new TypeError("Method Set.prototype.delete called on incompatible receiver "+this);var entry,valueHash=hash(value),objectHash=this._objectHash;if(null===valueHash){if(!("number"==typeof value[objectHash]&&this._data.objects[value[objectHash]]instanceof SetEntry))return!1;entry=this._data.objects[value[objectHash]],delete this._data.objects[value[objectHash]],delete entry.value[objectHash]}else{if(!(this._data.primitives[valueHash]instanceof SetEntry))return!1;entry=this._data.primitives[valueHash],delete this._data.primitives[valueHash]}return null!==entry.prev&&null!==entry.next?(entry.prev.next=entry.next,entry.next.prev=entry.prev,entry.next=null,entry.prev=null):null===entry.prev&&null!==entry.next&&(this._head=entry.next,entry.next.prev=null,entry.next=null),null!==entry.prev&&null===entry.next?(this._tail=entry.prev,entry.prev.next=null,entry.prev=null):(this._head=null,this._tail=null),this._size--,!0},Set.prototype.entries=function(){if(!isSet(this))throw new TypeError("Method Set.prototype.entries called on incompatible receiver "+this);return new SetIterator(this,1)},Set.prototype.values=function(){if(!isSet(this))throw new TypeError("Method Set.prototype.values called on incompatible receiver "+this);return new SetIterator(this,2)},Set.prototype.keys=Set.prototype.values,Set.prototype[Symbol.iterator]=Set.prototype.values,defineProperty(Set.prototype,"size",{get:function(){if(!isSet(this))throw new TypeError("Method Set.prototype.size called on incompatible receiver "+this);return this._size},configurable:!0}),Set.prototype.forEach=function(callback,thisArg){if(!isSet(this))throw new TypeError("Method Set.prototype.forEach called on incompatible receiver "+this);if(!isCallable(callback))throw new TypeError(callback+" is not a function");for(var currentEntry=this._head;null!==currentEntry;)callback.call(thisArg,currentEntry.value,currentEntry.value,this),currentEntry=currentEntry.next},defineProperty(Set.prototype,Symbol.toStringTag.toString(),{value:"Set",configurable:!0});var setupSetInternals=function(set){defineProperties(set,{_isSet:{value:!0},_head:{value:null,writable:!0},_tail:{value:null,writable:!0},_objectHash:{value:Symbol("Hash(set)")},_size:{value:0,writable:!0},_data:{value:create(null,{primitives:{value:create(null)},objects:{value:[]}})}})},SetEntry=function(value){this.value=value,this.next=null,this.prev=null},isSet=function(set){return set instanceof Set&&function(set){return!0===set._isSet&&(null===set._head||set._head instanceof SetEntry)&&(null===set._tail||set._tail instanceof SetEntry)&&ES6.isSymbol(set._objectHash)&&"number"==typeof set._size&&isObject(set._data)&&isObject(set._data.primitives)&&isArray(set._data.objects)}(set)},WeakMap=function WeakMap(iterable){if(!(this instanceof WeakMap)||isWeakMap(this))throw new TypeError("Constructor WeakMap requires 'new'");setupWeakMapInternals(this),null!==iterable&&void 0!==iterable&&ES6.forOf(iterable,function(entry){if(!isObject(entry))throw new TypeError("Iterator value "+entry+" is not an entry object");this.set(entry[0],entry[1])},this)},setupWeakMapInternals=function(weakMap){defineProperties(weakMap,{_isWeakMap:{value:!0},_objectHash:{value:Symbol("Hash(weakmap)")},_values:{value:[]}})};defineProperty(WeakMap.prototype,Symbol.toStringTag.toString(),{value:"WeakMap",configurable:!0});var isWeakMap=function(weakMap){return weakMap instanceof WeakMap&&function(weakMap){return!0===weakMap._isWeakMap&&ES6.isSymbol(weakMap._objectHash)&&isArray(weakMap._values)}(weakMap)};WeakMap.prototype.set=function(key,value){if(!isWeakMap(this))throw new TypeError("Method WeakMap.prototype.set called on incompatible receiver "+this);if(!isObject(key)||ES6.isSymbol(key))throw new TypeError("Invalid value used as weak map key");var objectHash=this._objectHash;return"number"==typeof key[objectHash]&&this._values.hasOwnProperty(key[objectHash])?this._values[key[objectHash]]=value:(this._values.push(value),defineProperty(key,objectHash.toString(),{value:this._values.length-1,configurable:!0})),this},WeakMap.prototype.get=function(key){if(!isWeakMap(this))throw new TypeError("Method WeakMap.prototype.get called on incompatible receiver "+this);if(isObject(key)&&!ES6.isSymbol(key)){var objectHash=this._objectHash;return"number"==typeof key[objectHash]&&this._values.hasOwnProperty(key[objectHash])?this._values[key[objectHash]]:void 0}},WeakMap.prototype.has=function(key){if(!isWeakMap(this))throw new TypeError("Method WeakMap.prototype.has called on incompatible receiver "+this);if(!isObject(key)||ES6.isSymbol(key))return!1;var objectHash=this._objectHash;return"number"==typeof key[objectHash]&&this._values.hasOwnProperty(key[objectHash])},WeakMap.prototype.delete=function(key){if(!isWeakMap(this))throw new TypeError("Method WeakMap.prototype.delete called on incompatible receiver "+this);if(!isObject(key)||ES6.isSymbol(key))return!1;var objectHash=this._objectHash;return!("number"!=typeof key[objectHash]||!this._values.hasOwnProperty(key[objectHash]))&&(delete this._values[key[objectHash]],delete key[objectHash],!0)};var WeakSet=function WeakSet(iterable){if(!(this instanceof WeakSet)||isWeakSet(this))throw new TypeError("Constructor WeakSet requires 'new'");setupWeakSetInternals(this),null!==iterable&&void 0!==iterable&&ES6.forOf(iterable,function(entry){this.add(entry)},this)},setupWeakSetInternals=function(weakSet){defineProperties(weakSet,{_isWeakSet:{value:!0},_objectHash:{value:Symbol("Hash(weakset)")}})},isWeakSet=function(weakSet){return weakSet instanceof WeakSet&&function(weakSet){return!0===weakSet._isWeakSet&&ES6.isSymbol(weakSet._objectHash)}(weakSet)};return WeakSet.prototype.add=function(value){if(!isWeakSet(this))throw new TypeError("Method WeakSet.prototype.add called on incompatible receiver "+this);if(!isObject(value)||ES6.isSymbol(value))throw new TypeError("Invalid value used in weak set");var objectHash=this._objectHash;return value[objectHash]===objectHash.toString()||defineProperty(value,objectHash.toString(),{value:objectHash.toString(),configurable:!0}),this},WeakSet.prototype.has=function(value){if(!isWeakSet(this))throw new TypeError("Method WeakSet.prototype.has called on incompatible receiver "+this);if(!isObject(value)||ES6.isSymbol(value))return!1;var objectHash=this._objectHash;return value[objectHash]===objectHash.toString()},WeakSet.prototype.delete=function(value){if(!isWeakSet(this))throw new TypeError("Method WeakSet.prototype.delete called on incompatible receiver "+this);if(!isObject(value)||ES6.isSymbol(value))return!1;var objectHash=this._objectHash;return value[objectHash]===objectHash.toString()&&(delete value[objectHash],!0)},defineProperty(WeakSet.prototype,Symbol.toStringTag.toString(),{value:"WeakSet",configurable:!0}),defineProperties(ES6,{isMap:{value:isMap,writable:!0,configurable:!0},isSet:{value:isSet,writable:!0,configurable:!0},isWeakMap:{value:isWeakMap,writable:!0,configurable:!0},isWeakSet:{value:isWeakSet,writable:!0,configurable:!0}}),defineProperty(global,"Map",{value:Map,writable:!0,configurable:!0}),defineProperty(global,"Set",{value:Set,writable:!0,configurable:!0}),defineProperty(global,"WeakMap",{value:WeakMap,writable:!0,configurable:!0}),defineProperty(global,"WeakSet",{value:WeakSet,writable:!0,configurable:!0}),ES6});