Skip to content

Commit a6190e7

Browse files
authored
Update mapPolyfill.src.js
1 parent cb24bb7 commit a6190e7

File tree

1 file changed

+129
-164
lines changed

1 file changed

+129
-164
lines changed

Diff for: mapPolyfill.src.js

+129-164
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,153 @@
11
//anonyco
2-
(function(){
2+
if (typeof Map == 'undefined' || !/*window.*/Map.prototype.keys || typeof Set == 'undefined' || !/*window.*/Set.prototype.keys) (function(){
33
'use-strict';
44
var keycur,
55
i, len,
66
k, v,
7-
iterable;
7+
iterable,
8+
Mapproto = {
9+
//length: 0,
10+
'delete': function( key ){
11+
keycur = NaNsearch( this.k, key ); // k is for keys
12+
if (!~keycur) return false;
13+
this.k.splice(keycur, 1);
14+
this.v.splice(keycur, 1);
15+
--this.size;
16+
return true;
17+
},
18+
'get': function( key ){
19+
return this.v[NaNsearch(this.k, key)]; // automagicly returns undefined if it doesn't exist
20+
},
21+
'set': function( key, value ){
22+
keycur = NaNsearch(this.k, key);
23+
if (!~keycur) // if (keycur === -1)
24+
this.k[keycur = this.size++] = key;
25+
this.v[keycur] = value;
26+
return this;
27+
},
28+
'has': function( key ){
29+
return NaNsearch(this.k, key) > -1;
30+
},
31+
'clear': function(){
32+
this.k.length = this.v.length = this.size = 0;
33+
//return undefined
34+
},
35+
'forEach': function( Func, thisArg ){
36+
if(thisArg)
37+
Func = Func.bind(thisArg);
38+
var i = -1, len = this.size;
39+
while (++i !== len) Func(this.v[i], this.k[i], this);
40+
},
41+
'entries': function( ){
42+
var nextIndex = 0, that = this;
43+
return {
44+
next: function() {
45+
return nextIndex !== that.size ? {value: [that.k[nextIndex++], that.v[nextIndex]], done: false} : {done: true};
46+
}
47+
};
48+
},
49+
'keys': function( ){
50+
var nextIndex = 0, that = this;
51+
return {
52+
next: function() {
53+
return nextIndex !== that.size ? {value: that.k[nextIndex++], done: false} : {done: true};
54+
}
55+
};
56+
},
57+
'values': function( ){
58+
var nextIndex = 0, that = this;
59+
return {
60+
next: function() {
61+
return nextIndex !== that.size ? {value: that.v[nextIndex++], done: false} : {done: true};
62+
}
63+
};
64+
},
65+
toString: function(){return '[object Map]'}
66+
};
867
function NaNsearch(arr, val){ // search that compensates for NaN indexs
968
if (val === val) // if val is not NaN
1069
return arr.indexOf(val);
11-
70+
1271
i = 0, len = arr.length;
1372
// Check for the first index that is not itself (i.e. NaN)
1473
while (arr[i] === arr[i] && ++i !== len)
1574
; // do nothing
1675
return i;
17-
}
18-
76+
};
77+
1978
// Map & WeakMap polyfill
20-
if (!window.Map || !/*window.*/Map.keys){
21-
/*window.*/WeakMap = /*window.*/Map = function(raw){
22-
k = this.k = [];
23-
v = this.v = [];
24-
len = 0;
25-
if (raw){
26-
iterable = Object(raw);
27-
// split up the data into two useable streams: one for keys (k), and one for values (v)
28-
i = +iterable.length;
29-
if (i !== i) // if i is NaN
30-
throw new TypeError('(' + (raw.toString || iterable.toString)() + ') is not iterable');
31-
32-
while (i--)
33-
if (iterable[i] instanceof Object){
34-
if (!~NaNsearch(k, iterable[i][0])) // if current is not already in the array
35-
k[len] = iterable[i][0], v[len++] = iterable[i][1]; // len++ increments len, but returns value before increment
36-
} else
37-
throw new TypeError('Iterator value ' + iterable[i] + ' is not an entry object');
38-
k.reverse();
39-
v.reverse();
40-
}
41-
this.size = len;
79+
/*window.*/WeakMap = /*window.*/Map = function(raw){
80+
k = this.k = [];
81+
v = this.v = [];
82+
len = 0;
83+
if (raw !== undefined && raw !== null){
84+
iterable = Object(raw);
85+
// split up the data into two useable streams: one for keys (k), and one for values (v)
86+
i = +iterable.length;
87+
if (i != i) // if i is NaN
88+
throw new TypeError('(' + (raw.toString || iterable.toString)() + ') is not iterable');
89+
90+
while (i--)
91+
if (iterable[i] instanceof Object){
92+
if (!~NaNsearch(k, iterable[i][0])) // if current is not already in the array
93+
k[len] = iterable[i][0], v[len++] = iterable[i][1]; // len++ increments len, but returns value before increment
94+
} else
95+
throw new TypeError('Iterator value ' + iterable[i] + ' is not an entry object');
96+
k.reverse();
97+
v.reverse();
4298
}
43-
/*window.*/Map.prototype = {
44-
length: 0,
45-
'delete': function( key ){
46-
keycur = NaNsearch( this.k, key ); // k is for keys
47-
if (!~keycur) return false;
48-
this.k.splice(keycur, 1);
49-
this.v.splice(keycur, 1);
50-
--this.size;
51-
return true;
52-
},
53-
'get': function( key ){
54-
return this.v[NaNsearch(this.k, key)]; // automagicly returns undefined if it doesn't exist
55-
},
56-
'set': function( key, value ){
57-
keycur = NaNsearch(this.k, key);
58-
if (!~keycur) // if (keycur === -1)
59-
this.k[keycur = this.size++] = key;
60-
this.v[keycur] = value;
61-
return this;
62-
},
63-
'has': function( key ){
64-
return NaNsearch(this.k, key) > -1;
65-
},
66-
'clear': function(){
67-
this.k.length = this.v.length = this.size = 0;
68-
//return undefined
69-
},
70-
'forEach': function( Func, thisArg ){
71-
if(thisArg)
72-
Func = Func.bind(thisArg);
73-
var i = -1, len = this.size;
74-
while (++i !== len) Func(this.v[i], this.k[i], this);
75-
},
76-
'entries': function( ){
77-
var nextIndex = 0, that = this;
78-
return {
79-
next: function() {
80-
return nextIndex !== that.size ? {value: [that.k[nextIndex++], that.v[nextIndex]], done: false} : {done: true};
81-
}
82-
};
83-
},
84-
'keys': function( ){
85-
var nextIndex = 0, that = this;
86-
return {
87-
next: function() {
88-
return nextIndex !== that.size ? {value: that.k[nextIndex++], done: false} : {done: true};
89-
}
90-
};
91-
},
92-
'values': function( ){
93-
var nextIndex = 0, that = this;
94-
return {
95-
next: function() {
96-
return nextIndex !== that.size ? {value: that.v[nextIndex++], done: false} : {done: true};
97-
}
98-
};
99-
},
100-
toString: function(){return '[object Map]'}
101-
};
102-
/*if (typeof Symbol === 'function'){
99+
this.size = len;
100+
};
101+
/*window.*/Map.prototype = Mapproto;
102+
/*if (typeof Symbol === 'function'){
103103
Map.prototype[Symbol.iterator] = Map.prototype.values;
104104
Map.prototype[Symbol.toStringTag] = 'Map';
105105
}*/
106-
}
107-
106+
108107
// Set & WeakSet polyfill
109-
if (!window.Set || !/*window.*/Set.values){
110-
/*window.*/WeakSet = /*window.*/Set = function(raw){
111-
k = this.k = [];
112-
len = 0;
113-
if (raw){
114-
iterable = Object(raw);
115-
// split up the data into two useable streams: one for keys (k), and one for values (v)
116-
i = +iterable.length;
117-
if (i !== i) // if i is NaN
118-
throw new TypeError('(' + (raw.toString || iterable.toString)() + ') is not iterable');
119-
120-
while (i--)
121-
if (!~NaNsearch(k, iterable[i])) // if current is not already in the array
122-
k[len++] = iterable[i]; // len++ increments len, but returns value before increment
123-
k.reverse();
124-
}
125-
this.size = len;
108+
/*window.*/WeakSet = /*window.*/Set = function(raw){
109+
k = this.k = this.v = [];
110+
len = 0;
111+
if (raw !== undefined && raw !== null){
112+
iterable = Object(raw);
113+
// split up the data into two useable streams: one for keys (k), and one for values (v)
114+
i = +iterable.length;
115+
if (i != i) // if i is NaN
116+
throw new TypeError('(' + (raw.toString || iterable.toString)() + ') is not iterable');
117+
118+
while (i--)
119+
if (!~NaNsearch(k, iterable[i])) // if current is not already in the array
120+
k[len++] = iterable[i]; // len++ increments len, but returns value before increment
121+
k.reverse();
126122
}
127-
/*window.*/Set.prototype = {
128-
length: 0,
129-
'delete': function( value ){
130-
keycur = NaNsearch(this.k, value ); // k is for keys
131-
if (!~keycur) return false;
132-
this.k.splice(keycur, 1);
133-
--this.size;
134-
return true;
135-
},
136-
'add': function( value ){
137-
keycur = NaNsearch(this.k, value);
138-
if (!~keycur) keycur = this.size++;
139-
this.k[keycur] = value;
140-
return this;
141-
},
142-
'has': function( value ){
143-
return NaNsearch(this.k, value) > -1;
144-
},
145-
'clear': function(){
146-
this.k.length = this.size = 0;
147-
//return undefined
148-
},
149-
'forEach': function( Func, thisArg ){
150-
if(thisArg)
151-
Func = Func.bind(thisArg);
152-
var i = -1, len = this.size;
153-
while (++i !== len) Func(this.k[i], this.k[i], this);
154-
},
155-
'entries': function(){
156-
var nextIndex = -1, that = this;
157-
return {
158-
next: function() {
159-
return ++nextIndex !== that.size ? {value: [that.k[nextIndex], that.k[nextIndex]], done: false} : {done: true};
160-
}
161-
};
162-
},
163-
'keys': function(){
164-
var nextIndex = -1, that = this;
165-
return {
166-
next: function() {
167-
return ++nextIndex !== that.size ? {value: that.k[nextIndex], done: false} : {done: true};
168-
}
169-
};
170-
},
171-
/*'values': function(){ // same as keys
172-
var nextIndex = -1, that = this;
173-
return {
174-
next: function() {
175-
return ++nextIndex !== that.size ? {value: that.k[nextIndex], done: false} : {done: true};
176-
}
177-
};
178-
},*/
179-
toString: function(){return '[object Set]'}
180-
};
181-
/*if (typeof Symbol === 'function'){
123+
this.size = len;
124+
}
125+
/*window.*/Set.prototype = {
126+
//length: 0,
127+
'delete': function( value ){
128+
keycur = NaNsearch(this.k, value ); // k is for keys
129+
if (!~keycur) return false;
130+
this.k.splice(keycur, 1);
131+
--this.size;
132+
return true;
133+
},
134+
'add': function( value ){
135+
keycur = NaNsearch(this.k, value);
136+
if (!~keycur) keycur = this.size++;
137+
this.k[keycur] = value;
138+
return this;
139+
},
140+
'has': Mapproto.has,
141+
'clear': Mapproto.clear,
142+
'forEach': Mapproto.forEach,
143+
'entries': Mapproto.entries,
144+
'keys': Mapproto.keys,
145+
'values': Mapproto.keys,
146+
toString: function(){return '[object Set]'}
147+
};
148+
/*if (typeof Symbol === 'function'){
182149
Set.prototype[Symbol.iterator] = Set.prototype.values;
183150
Set.prototype[Symbol.toStringTag] = 'Set';
184151
Set.prototype[Symbol.toPrimitive] = function(){return this.k};
185152
}*/
186-
/*window.*/Set.prototype.values = /*window.*/Set.prototype.keys;
187-
};
188-
})(typeof global === 'undefined' ? window : global); // for NodeJS
153+
})();

0 commit comments

Comments
 (0)