-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhash.js
223 lines (205 loc) · 5.39 KB
/
hash.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(function() {
/**
* Hashtable implementation. This provides a 'length' property that's
* maintained throughout all updates, and has no restrictions upon the
* types of keys that may be used.
*
* @class Hash
*/
if(typeof window.Hash != 'undefined') {
var _Hash = window.Hash;
}
var Hash = window.Hash = function(args) {
if(this instanceof arguments.callee) {
this.init.apply(this, args && args.callee ? args : arguments);
} else {
return new Hash(arguments);
}
};
Hash.no_conflict = function() {
window.Hash = _Hash;
return Hash;
};
Hash.prototype = {
/**
* Creates a new Hash. If the copy_from object is specified, it copies
* the keys and values from that.
*
* @constructor
* @member Hash
* @param {Object} copy_from Copy fields/values from this object (optional)
*/
init: function(copy_from) {
this.empty();
if(copy_from) {
this.update(copy_from);
}
},
/**
* Returns the value of a key, or undefined if not set.
*
* @member Hash
* @param {String} key Key to retrieve.
*/
get: function(key) {
return this.hash[key];
},
/**
* Returns true if this hashtable contains the specified key.
*
* @member Hash
* @param {String} key Key to test.
*/
contains: function(key) {
return this.get(key) !== undefined;
},
/**
* Sets key to val, then returns val.
*
* @member Hash
* @param {String} key Key to set.
* @param {Object} val Value to set it to.
*/
put: function(key, val) {
if(!this.contains(key)) {
this.length++;
}
this.hash[key] = val;
return val;
},
/**
* Removes a key, returning the hashtable itself.
*
* @member Hash
* @param {String} key Key to remove.
*/
remove: function(key) {
delete this.hash[key];
this.length--;
return this;
},
/**
* Sets a key if it doesn't already exist.
*
* @member Hash
* @param {String} key Key to set.
* @param {Object} default_val Value to set it to if key doesn't exist.
* @returns {Object} The value of the key.
*/
ensure: function(key, default_val) {
var current_val = this.get(key);
if(current_val === undefined) {
return this.put(key, default_val);
}
return current_val;
},
/**
* Sets a key if it doesn't already exist. Instead of taking a default
* value, takes a function of the key and evaluates that to get the
* default. This lets you defer construction costs until they're really
* needed.
*
* @member Hash
* @param {String} key Key to set.
* @param {Function(key)} default_fn Function to invoke if key doesn't exist.
* @returns {Object} The value of the key.
*/
lazy_ensure: function(key, default_fn) {
var current_val = this.get(key);
if(current_val === undefined) {
return this.put(key, default_fn(key));
}
return current_val;
},
/**
* Removes and returns the specified key.
*
* @member Hash
*/
pop: function(key) {
var current_val = this.get(key);
this.remove(key);
return current_val;
},
/**
* Copies all properties of the specified object to this hash. Does not
* consider values inherited from the prototype.
*
* @member Hash
* @returns {Hash} The Hash itself.
*/
update: function(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
this.put(prop, obj[prop]);
}
}
return this;
},
/**
* Removes all entries from this hash.
*
* @member Hash
* @returns {Hash} The hash itself.
*/
empty: function() {
this.hash = {};
this.length = 0;
return this;
},
/**
* Returns a list of all keys in this hash. The list is in unspecified
* order, and does not share structure with the Hash.
*
* @member Hash
* @type Array<String>
*/
keys: function() {
var keys = [];
for(var prop in this.hash) {
keys.push(prop);
}
return keys;
},
/**
* Returns a list of all values in this hash. The list is in unspecified
* order, and does not share structure with the Hash. Values retain their
* identities.
*
* @member Hash
* @type Array<Object>
*/
values: function() {
var values = [];
for(var prop in this.hash) {
values.push(this.hash[prop]);
}
return values;
},
/**
* Returns a list of [key, value] arrays for this hash.
*
* @member Hash
* @type Array<Array<String, Object>>
*/
items: function() {
var items = [];
for(var prop in this.hash) {
items.push([prop, this.hash[prop]]);
}
return items;
},
/**
* Returns a { key: value } JavaScript object with the contents of this
* hash. This shares structure with the hash; mutations to it affect
* the original Hash object and vis versa. Make a copy if you don't
* want this behavior.
*
* @member Hash
* @type Array<{ key: String, value: Object}>
*/
items_obj: function() {
return this.hash;
}
};
})();