|
| 1 | +/* eslint-disable */ |
| 2 | +/* jscs:disable */ |
| 3 | +(function (exports) {'use strict'; |
| 4 | + //shared pointer |
| 5 | + var i; |
| 6 | + //shortcuts |
| 7 | + var defineProperty = Object.defineProperty, is = function(a,b) { return (a === b) || (a !== a && b !== b) }; |
| 8 | + |
| 9 | + //Polyfill global objects |
| 10 | + if (typeof WeakMap == 'undefined') { |
| 11 | + exports.WeakMap = createCollection({ |
| 12 | + // WeakMap#delete(key:void*):boolean |
| 13 | + 'delete': sharedDelete, |
| 14 | + // WeakMap#clear(): |
| 15 | + clear: sharedClear, |
| 16 | + // WeakMap#get(key:void*):void* |
| 17 | + get: sharedGet, |
| 18 | + // WeakMap#has(key:void*):boolean |
| 19 | + has: mapHas, |
| 20 | + // WeakMap#set(key:void*, value:void*):void |
| 21 | + set: sharedSet |
| 22 | + }, true); |
| 23 | + } |
| 24 | + |
| 25 | + if (typeof Map == 'undefined' || typeof ((new Map).values) !== 'function' || !(new Map).values().next) { |
| 26 | + exports.Map = createCollection({ |
| 27 | + // WeakMap#delete(key:void*):boolean |
| 28 | + 'delete': sharedDelete, |
| 29 | + //:was Map#get(key:void*[, d3fault:void*]):void* |
| 30 | + // Map#has(key:void*):boolean |
| 31 | + has: mapHas, |
| 32 | + // Map#get(key:void*):boolean |
| 33 | + get: sharedGet, |
| 34 | + // Map#set(key:void*, value:void*):void |
| 35 | + set: sharedSet, |
| 36 | + // Map#keys(void):Iterator |
| 37 | + keys: sharedKeys, |
| 38 | + // Map#values(void):Iterator |
| 39 | + values: sharedValues, |
| 40 | + // Map#entries(void):Iterator |
| 41 | + entries: mapEntries, |
| 42 | + // Map#forEach(callback:Function, context:void*):void ==> callback.call(context, key, value, mapObject) === not in specs` |
| 43 | + forEach: sharedForEach, |
| 44 | + // Map#clear(): |
| 45 | + clear: sharedClear |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + if (typeof Set == 'undefined' || typeof ((new Set).values) !== 'function' || !(new Set).values().next) { |
| 50 | + exports.Set = createCollection({ |
| 51 | + // Set#has(value:void*):boolean |
| 52 | + has: setHas, |
| 53 | + // Set#add(value:void*):boolean |
| 54 | + add: sharedAdd, |
| 55 | + // Set#delete(key:void*):boolean |
| 56 | + 'delete': sharedDelete, |
| 57 | + // Set#clear(): |
| 58 | + clear: sharedClear, |
| 59 | + // Set#keys(void):Iterator |
| 60 | + keys: sharedValues, // specs actually say "the same function object as the initial value of the values property" |
| 61 | + // Set#values(void):Iterator |
| 62 | + values: sharedValues, |
| 63 | + // Set#entries(void):Iterator |
| 64 | + entries: setEntries, |
| 65 | + // Set#forEach(callback:Function, context:void*):void ==> callback.call(context, value, index) === not in specs |
| 66 | + forEach: sharedForEach |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + if (typeof WeakSet == 'undefined') { |
| 71 | + exports.WeakSet = createCollection({ |
| 72 | + // WeakSet#delete(key:void*):boolean |
| 73 | + 'delete': sharedDelete, |
| 74 | + // WeakSet#add(value:void*):boolean |
| 75 | + add: sharedAdd, |
| 76 | + // WeakSet#clear(): |
| 77 | + clear: sharedClear, |
| 78 | + // WeakSet#has(value:void*):boolean |
| 79 | + has: setHas |
| 80 | + }, true); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * ES6 collection constructor |
| 85 | + * @return {Function} a collection class |
| 86 | + */ |
| 87 | + function createCollection(proto, objectOnly){ |
| 88 | + function Collection(a){ |
| 89 | + if (!this || this.constructor !== Collection) return new Collection(a); |
| 90 | + this._keys = []; |
| 91 | + this._values = []; |
| 92 | + this._itp = []; // iteration pointers |
| 93 | + this.objectOnly = objectOnly; |
| 94 | + |
| 95 | + //parse initial iterable argument passed |
| 96 | + if (a) init.call(this, a); |
| 97 | + } |
| 98 | + |
| 99 | + //define size for non object-only collections |
| 100 | + if (!objectOnly) { |
| 101 | + defineProperty(proto, 'size', { |
| 102 | + get: sharedSize |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + //set prototype |
| 107 | + proto.constructor = Collection; |
| 108 | + Collection.prototype = proto; |
| 109 | + |
| 110 | + return Collection; |
| 111 | + } |
| 112 | + |
| 113 | + /** parse initial iterable argument passed */ |
| 114 | + function init(a){ |
| 115 | + //init Set argument, like `[1,2,3,{}]` |
| 116 | + if (this.add) |
| 117 | + a.forEach(this.add, this); |
| 118 | + //init Map argument like `[[1,2], [{}, 4]]` |
| 119 | + else |
| 120 | + a.forEach(function(a){this.set(a[0],a[1])}, this); |
| 121 | + } |
| 122 | + |
| 123 | + /** delete */ |
| 124 | + function sharedDelete(key) { |
| 125 | + if (this.has(key)) { |
| 126 | + this._keys.splice(i, 1); |
| 127 | + this._values.splice(i, 1); |
| 128 | + // update iteration pointers |
| 129 | + this._itp.forEach(function(p) { if (i < p[0]) p[0]--; }); |
| 130 | + } |
| 131 | + // Aurora here does it while Canary doesn't |
| 132 | + return -1 < i; |
| 133 | + } |
| 134 | + |
| 135 | + function sharedGet(key) { |
| 136 | + return this.has(key) ? this._values[i] : undefined; |
| 137 | + } |
| 138 | + |
| 139 | + function has(list, key) { |
| 140 | + if (this.objectOnly && key !== Object(key)) |
| 141 | + throw new TypeError("Invalid value used as weak collection key"); |
| 142 | + //NaN or 0 passed |
| 143 | + if (key !== key || key === 0) for (i = list.length; i-- && !is(list[i], key);){} |
| 144 | + else i = list.indexOf(key); |
| 145 | + return -1 < i; |
| 146 | + } |
| 147 | + |
| 148 | + function setHas(value) { |
| 149 | + return has.call(this, this._values, value); |
| 150 | + } |
| 151 | + |
| 152 | + function mapHas(value) { |
| 153 | + return has.call(this, this._keys, value); |
| 154 | + } |
| 155 | + |
| 156 | + /** @chainable */ |
| 157 | + function sharedSet(key, value) { |
| 158 | + this.has(key) ? |
| 159 | + this._values[i] = value |
| 160 | + : |
| 161 | + this._values[this._keys.push(key) - 1] = value |
| 162 | + ; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** @chainable */ |
| 167 | + function sharedAdd(value) { |
| 168 | + if (!this.has(value)) this._values.push(value); |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + function sharedClear() { |
| 173 | + this._values.length = 0; |
| 174 | + } |
| 175 | + |
| 176 | + /** keys, values, and iterate related methods */ |
| 177 | + function sharedKeys() { |
| 178 | + return sharedIterator(this._itp, this._keys); |
| 179 | + } |
| 180 | + |
| 181 | + function sharedValues() { |
| 182 | + return sharedIterator(this._itp, this._values); |
| 183 | + } |
| 184 | + |
| 185 | + function mapEntries() { |
| 186 | + return sharedIterator(this._itp, this._keys, this._values); |
| 187 | + } |
| 188 | + |
| 189 | + function setEntries() { |
| 190 | + return sharedIterator(this._itp, this._values, this._values); |
| 191 | + } |
| 192 | + |
| 193 | + function sharedIterator(itp, array, array2) { |
| 194 | + var p = [0], done = false; |
| 195 | + itp.push(p); |
| 196 | + return { |
| 197 | + next: function() { |
| 198 | + var v, k = p[0]; |
| 199 | + if (!done && k < array.length) { |
| 200 | + v = array2 ? [array[k], array2[k]]: array[k]; |
| 201 | + p[0]++; |
| 202 | + } else { |
| 203 | + done = true; |
| 204 | + itp.splice(itp.indexOf(p), 1); |
| 205 | + } |
| 206 | + return { done: done, value: v }; |
| 207 | + } |
| 208 | + }; |
| 209 | + } |
| 210 | + |
| 211 | + function sharedSize() { |
| 212 | + return this._values.length; |
| 213 | + } |
| 214 | + |
| 215 | + function sharedForEach(callback, context) { |
| 216 | + var it = this.entries(); |
| 217 | + for (;;) { |
| 218 | + var r = it.next(); |
| 219 | + if (r.done) break; |
| 220 | + callback.call(context, r.value[1], r.value[0], this); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | +})(typeof exports != 'undefined' && typeof global != 'undefined' ? global : window ); |
0 commit comments