-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashList.js
More file actions
101 lines (85 loc) · 2.1 KB
/
HashList.js
File metadata and controls
101 lines (85 loc) · 2.1 KB
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
// See https://github.com/matthaak/HashList/ for documentation
var HashList = Class.create();
HashList.prototype = {
// listGE - optional delimited string or GlideElement
// delim - optional delimiter string of any characters, defaults to comma
initialize: function(listGE, delim) {
if (typeof listGE !== 'undefined')
this.listGE = listGE;
else
this.listGE = '';
this.delim = delim || ',';
this._arr = [];
this._obj = {};
var delimEsc = this.delim.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
var delimRE = new RegExp('\\s*' + delimEsc + '\\s*');
var i, elmt;
var listArr = this.listGE.toString().split(delimRE);
for (i=0; i<listArr.length; i++) {
elmt = listArr[i];
if (elmt && !this._obj[elmt]) {
this._arr.push(elmt);
this._obj[elmt] = true;
}
}
},
add : function(value) {
if (this.contains(value)) return false;
this._arr.push(value);
this._obj[value] = true;
this._setGE();
return true;
},
addAll: function(hashList) {
var me = this;
var amModified = false;
hashList._arr.forEach(function(val) {
if (!me.contains(val)) {
me.add(val);
amModified = true;
}
});
return amModified;
},
clear : function() {
this._arr = [];
this._obj = {};
this._setGE();
},
clone : function() {
var cloneHL = new HashList(this.listGE.toString(), this.delim);
return cloneHL;
},
contains : function(value) {
return this._obj[value] || false;
},
isEmpty : function() {
return this._arr.length===0;
},
remove : function(value) {
if (!this.contains(value)) return false;
var idx = this._arr.indexOf(value);
this._arr.splice(idx, 1);
delete this._obj[value];
this._setGE();
return true;
},
size : function() {
return this._arr.length;
},
toString : function() {
return this._arr.join(this.delim);
},
toArray : function() {
return this._arr.slice();
},
_setGE : function() {
var newValue = this._arr.join(this.delim);
if (this.listGE.setValue) { // If inited with GlideElement
this.listGE.setValue(newValue);
} else {
this.listGE = newValue;
}
},
type: 'HashList'
};