UNPKG

965 BJavaScriptView Raw
1function StringSet(items) {
2 this._items = {};
3 this._length = items ? items.length : 0;
4 if (!items) return;
5 for (let i = 0, l = items.length; i < l; i++) {
6 if (items[i] === undefined) continue;
7 this._items[items[i]] = i;
8 }
9}
10
11StringSet.prototype.add = function(x) {
12 this._length = this._items[x] ? this._length : this._length + 1;
13 this._items[x] = this._items[x] ? this._items[x] : this._length;
14 return this;
15};
16
17StringSet.prototype.delete = function(x) {
18 this._length = this._items[x] ? this._length - 1 : this._length;
19 delete this._items[x];
20 return this;
21};
22
23StringSet.prototype.has = function(x) {
24 return this._items[x] !== undefined;
25};
26
27StringSet.prototype.values = function() {
28 const orderedKeys = Object.keys(this._items).sort((a, b) => this._items[a] - this._items[b]);
29 return orderedKeys;
30};
31
32StringSet.prototype.clear = function() {
33 this._length = 0;
34 this._items = {};
35 return this;
36};
37
38module.exports = StringSet;