3 Contains the class Hash.
\r
11 It wraps an object that it uses internally as a map. The user must use set(), get(), and remove() to add/change, retrieve and remove values, it must not access the internal object directly. null/undefined values are allowed.
\r
14 Each hash instance has the length property.
\r
17 obj - an object to convert into a Hash instance.
\r
21 var hash = new Hash({a: 'hi', b: 'world', c: 'howdy'});
\r
22 hash.remove('b'); // b is removed.
\r
23 hash.set('c', 'hello');
\r
24 hash.get('c'); // returns 'hello'
\r
25 hash.length // returns 2 (a and c)
\r
29 var Hash = new Class({
\r
33 initialize: function(object){
\r
34 this.obj = object || {};
\r
40 Retrieves a value from the hash.
\r
50 return (this.hasKey(key)) ? this.obj[key] : null;
\r
55 Check the presence of a specified key-value pair in the hash.
\r
61 True if the Hash contains a value for the specified key, otherwise false
\r
64 hasKey: function(key){
\r
65 return (key in this.obj);
\r
70 Adds a key-value pair to the hash or replaces a previous value associated with the key.
\r
77 set: function(key, value){
\r
78 if (!this.hasKey(key)) this.length++;
\r
79 this.obj[key] = value;
\r
83 setLength: function(){
\r
85 for (var p in this.obj) this.length++;
\r
91 Removes a key-value pair from the hash.
\r
97 remove: function(key){
\r
98 if (!this.hasKey(key)) return this;
\r
99 delete this.obj[key];
\r
106 Calls a function for each key-value pair. The first argument passed to the function will be the value, the second one will be the key, like $each.
\r
109 fn - The function to call for each key-value pair
\r
110 bind - Optional, the object that will be referred to as "this" in the function
\r
113 each: function(fn, bind){
\r
114 $each(this.obj, fn, bind);
\r
119 Extends the current hash with an object containing key-value pairs. Values for duplicate keys will be replaced by the new ones.
\r
122 obj - An object containing key-value pairs
\r
125 extend: function(obj){
\r
126 $extend(this.obj, obj);
\r
127 return this.setLength();
\r
132 Merges the current hash with multiple objects.
\r
136 this.obj = $merge.apply(null, [this.obj].extend(arguments));
\r
137 return this.setLength();
\r
142 Empties all hash values properties and values.
\r
153 Returns an array containing all the keys, in the same order as the values returned by <Hash.values>.
\r
156 An array containing all the keys of the hash
\r
161 for (var property in this.obj) keys.push(property);
\r
167 Returns an array containing all the values, in the same order as the keys returned by <Hash.keys>.
\r
170 An array containing all the values of the hash
\r
173 values: function(){
\r
175 for (var property in this.obj) values.push(this.obj[property]);
\r
181 /* Section: Utility Functions */
\r
185 Shortcut to create a Hash from an Object.
\r
189 return new Hash(obj);
\r