-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.js
180 lines (143 loc) · 3.93 KB
/
set.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 模拟实现Set
// Set包含属性size,方法add、delete、has、clear、forEach
// 第一版
(function(global) {
function Set(data) {
this._values = [];
this.size = 0;
data &&
data.forEach(function(item) {
this.add(item);
}, this);
}
Set.prototype.add = function(value) {
if (this._values.indexOf(value) === -1) {
this._values.push(value);
++this.size;
}
return this;
};
Set.prototype.has = function(value) {
return this._values.indexOf(value) !== -1;
};
Set.prototype.delete = function(value) {
var index = this._values.indexOf(value);
if (index === -1) return false;
this._values.splice(index, 1);
--this.size;
return true;
};
Set.prototype.clear = function(value) {
this._values = [];
this.size = 0;
};
Set.prototype.forEach = function(fn, thisArg) {
thisArg = thisArg || global;
for (var i = 0; i < this._values.length; i++) {
fn.call(thisArg, this._values[i], this._values[i], this);
}
};
Set.length = 0;
global.Set = Set;
})(this);
// 第二版,Set对于NaN来说会进行去重,而实际上NaN.indexOf(NaN) === -1,利用Symbol进行独一无二值处理
(function(gloabl) {
var NaNSymbol = Symbol(NaN);
var encodeVal = function(value) {
return value !== value ? NaNSymbol : value;
};
var decodeVal = function(value) {
return value === NaNSymbol ? NaN : value;
};
function Set(data) {
this._values = [];
this.size = 0;
data &&
data.forEach(function(item) {
this.add(item);
}, this);
}
Set.prototype.add = function(value) {
value = encodeVal(value);
if (this._values.indexOf(value) === -1) {
this._values.push(value);
++this.size;
}
return this;
};
Set.prototype.has = function(value) {
return this._values.indexOf(encodeVal(value)) !== -1;
};
Set.prototype.delete = function(value) {
var index = this._values.indexOf(encodeVal(value));
if (index === -1) return false;
this._values.splice(index, 1);
--this.size;
return true;
};
Set.prototype.clear = function(value) {};
Set.prototype.forEach = function(fn, thisArg) {};
Set.length = 0;
global.Set = Set;
})(this);
// 第三版 迭代器的实现和处理,初始化以及执行keys()、values()、entries()方法时都会返回迭代器
(function(global) {
var NaNSymbol = Symbol(NaN);
var encodeVal = function(value) {
return value !== value ? NaNSymbol : value;
};
var decodeVal = function(value) {
return value === NaNSymbol ? NaN : value;
};
var makeIterator = function(array, iterator) {
var nextInex = 0;
// new Set(new Set())会调用这里
var obj = {
next: function() {
return nextInex < array.length
? { value: iterator(array[nextInex++]), done: false }
: { value: void 0, done: true };
}
};
// [...set.keys()]会调用这里
obj[Symbol.iterator] = function() {
return obj;
};
return obj;
};
function forOf(obj, cb) {
let iterable, result;
if (typeof obj[Symbol.iterator] !== "function") {
throw new TypeError(result + " is not iterable");
}
if (typeof cb !== "function") {
throw new TypeError("cb must be callable");
}
iterable = obj[Symbol.iterator];
result = iterable.next();
while (!result.done) {
cb(result.vaule);
result = iterable.next();
}
}
function Set(data) {
this._values = [];
this.size = 0;
forOf(data, item => {
this.add(item);
});
}
Set.prototype.Symbol.iterator = function() {
return this.values();
};
Set.prototype.values = Set.prototype.keys = function() {
return makeIterator(this._values, function(value) {
return decodeVal(value);
});
};
Set.prototype.entries = function() {
return makeIterator(this._values, function(value) {
return [decodeVal(value), decodeVal(value)];
});
};
})(this);