HashMap.js
10.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @class Ext.util.HashMap
* <p>
* Represents a collection of a set of key and value pairs. Each key in the HashMap
* must be unique, the same key cannot exist twice. Access to items is provided via
* the key only. Sample usage:
* <pre><code>
var map = new Ext.util.HashMap();
map.add('key1', 1);
map.add('key2', 2);
map.add('key3', 3);
map.each(function(key, value, length){
console.log(key, value, length);
});
* </code></pre>
* </p>
*
* <p>The HashMap is an unordered class,
* there is no guarantee when iterating over the items that they will be in any particular
* order. If this is required, then use a {@link Ext.util.MixedCollection}.
* </p>
*/
Ext.define('Ext.util.HashMap', {
mixins: {
observable: 'Ext.util.Observable'
},
/**
* @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
* A default is provided that returns the <b>id</b> property on the object. This function is only used
* if the add method is called with a single argument.
*/
/**
* Creates new HashMap.
* @param {Object} config (optional) Config object.
*/
constructor: function(config) {
config = config || {};
var me = this,
keyFn = config.keyFn;
me.addEvents(
/**
* @event add
* Fires when a new item is added to the hash
* @param {Ext.util.HashMap} this.
* @param {String} key The key of the added item.
* @param {Object} value The value of the added item.
*/
'add',
/**
* @event clear
* Fires when the hash is cleared.
* @param {Ext.util.HashMap} this.
*/
'clear',
/**
* @event remove
* Fires when an item is removed from the hash.
* @param {Ext.util.HashMap} this.
* @param {String} key The key of the removed item.
* @param {Object} value The value of the removed item.
*/
'remove',
/**
* @event replace
* Fires when an item is replaced in the hash.
* @param {Ext.util.HashMap} this.
* @param {String} key The key of the replaced item.
* @param {Object} value The new value for the item.
* @param {Object} old The old value for the item.
*/
'replace'
);
me.mixins.observable.constructor.call(me, config);
me.clear(true);
if (keyFn) {
me.getKey = keyFn;
}
},
/**
* Gets the number of items in the hash.
* @return {Number} The number of items in the hash.
*/
getCount: function() {
return this.length;
},
/**
* Implementation for being able to extract the key from an object if only
* a single argument is passed.
* @private
* @param {String} key The key
* @param {Object} value The value
* @return {Array} [key, value]
*/
getData: function(key, value) {
// if we have no value, it means we need to get the key from the object
if (value === undefined) {
value = key;
key = this.getKey(value);
}
return [key, value];
},
/**
* Extracts the key from an object. This is a default implementation, it may be overridden
* @param {Object} o The object to get the key from
* @return {String} The key to use.
*/
getKey: function(o) {
return o.id;
},
/**
* Adds an item to the collection. Fires the {@link #event-add} event when complete.
*
* @param {String/Object} key The key to associate with the item, or the new item.
*
* If a {@link #getKey} implementation was specified for this HashMap,
* or if the key of the stored items is in a property called `id`,
* the HashMap will be able to *derive* the key for the new item.
* In this case just pass the new item in this parameter.
*
* @param {Object} [o] The item to add.
*
* @return {Object} The item added.
*/
add: function(key, value) {
var me = this;
if (value === undefined) {
value = key;
key = me.getKey(value);
}
if (me.containsKey(key)) {
return me.replace(key, value);
}
me.map[key] = value;
++me.length;
if (me.hasListeners.add) {
me.fireEvent('add', me, key, value);
}
return value;
},
/**
* Replaces an item in the hash. If the key doesn't exist, the
* {@link #method-add} method will be used.
* @param {String} key The key of the item.
* @param {Object} value The new value for the item.
* @return {Object} The new value of the item.
*/
replace: function(key, value) {
var me = this,
map = me.map,
old;
if (value === undefined) {
value = key;
key = me.getKey(value);
}
if (!me.containsKey(key)) {
me.add(key, value);
}
old = map[key];
map[key] = value;
if (me.hasListeners.replace) {
me.fireEvent('replace', me, key, value, old);
}
return value;
},
/**
* Remove an item from the hash.
* @param {Object} o The value of the item to remove.
* @return {Boolean} True if the item was successfully removed.
*/
remove: function(o) {
var key = this.findKey(o);
if (key !== undefined) {
return this.removeAtKey(key);
}
return false;
},
/**
* Remove an item from the hash.
* @param {String} key The key to remove.
* @return {Boolean} True if the item was successfully removed.
*/
removeAtKey: function(key) {
var me = this,
value;
if (me.containsKey(key)) {
value = me.map[key];
delete me.map[key];
--me.length;
if (me.hasListeners.remove) {
me.fireEvent('remove', me, key, value);
}
return true;
}
return false;
},
/**
* Retrieves an item with a particular key.
* @param {String} key The key to lookup.
* @return {Object} The value at that key. If it doesn't exist, <tt>undefined</tt> is returned.
*/
get: function(key) {
return this.map[key];
},
/**
* Removes all items from the hash.
* @return {Ext.util.HashMap} this
*/
clear: function(/* private */ initial) {
var me = this;
me.map = {};
me.length = 0;
if (initial !== true && me.hasListeners.clear) {
me.fireEvent('clear', me);
}
return me;
},
/**
* Checks whether a key exists in the hash.
* @param {String} key The key to check for.
* @return {Boolean} True if they key exists in the hash.
*/
containsKey: function(key) {
return this.map[key] !== undefined;
},
/**
* Checks whether a value exists in the hash.
* @param {Object} value The value to check for.
* @return {Boolean} True if the value exists in the dictionary.
*/
contains: function(value) {
return this.containsKey(this.findKey(value));
},
/**
* Return all of the keys in the hash.
* @return {Array} An array of keys.
*/
getKeys: function() {
return this.getArray(true);
},
/**
* Return all of the values in the hash.
* @return {Array} An array of values.
*/
getValues: function() {
return this.getArray(false);
},
/**
* Gets either the keys/values in an array from the hash.
* @private
* @param {Boolean} isKey True to extract the keys, otherwise, the value
* @return {Array} An array of either keys/values from the hash.
*/
getArray: function(isKey) {
var arr = [],
key,
map = this.map;
for (key in map) {
if (map.hasOwnProperty(key)) {
arr.push(isKey ? key: map[key]);
}
}
return arr;
},
/**
* Executes the specified function once for each item in the hash.
* Returning false from the function will cease iteration.
*
* The paramaters passed to the function are:
* <div class="mdetail-params"><ul>
* <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
* <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
* <li><b>length</b> : Number<p class="sub-desc">The total number of items in the hash</p></li>
* </ul></div>
* @param {Function} fn The function to execute.
* @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
* @return {Ext.util.HashMap} this
*/
each: function(fn, scope) {
// copy items so they may be removed during iteration.
var items = Ext.apply({}, this.map),
key,
length = this.length;
scope = scope || this;
for (key in items) {
if (items.hasOwnProperty(key)) {
if (fn.call(scope, key, items[key], length) === false) {
break;
}
}
}
return this;
},
/**
* Performs a shallow copy on this hash.
* @return {Ext.util.HashMap} The new hash object.
*/
clone: function() {
var hash = new this.self(),
map = this.map,
key;
hash.suspendEvents();
for (key in map) {
if (map.hasOwnProperty(key)) {
hash.add(key, map[key]);
}
}
hash.resumeEvents();
return hash;
},
/**
* @private
* Find the key for a value.
* @param {Object} value The value to find.
* @return {Object} The value of the item. Returns <tt>undefined</tt> if not found.
*/
findKey: function(value) {
var key,
map = this.map;
for (key in map) {
if (map.hasOwnProperty(key) && map[key] === value) {
return key;
}
}
return undefined;
}
});