- uploading 1.1 in tags
[mootools.git] / Plugins / Hash.js
blobc6abade4e6fb2020304bb14837d54e396fd38a58
1 /*\r
2 Script: Hash.js\r
3         Contains the class Hash.\r
4 \r
5 License:\r
6         MIT-style license.\r
7 */\r
8 \r
9 /*\r
10 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
13 Note:\r
14         Each hash instance has the length property.\r
16 Arguments:\r
17         obj - an object to convert into a Hash instance.\r
19 Example:\r
20         (start code)\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
26         (end)\r
27 */\r
29 var Hash = new Class({\r
31         length: 0,\r
33         initialize: function(object){\r
34                 this.obj = object || {};\r
35                 this.setLength();\r
36         },\r
38         /*\r
39         Property: get\r
40                 Retrieves a value from the hash.\r
42         Arguments:\r
43                 key - The key\r
45         Returns:\r
46                 The value\r
47         */\r
49         get: function(key){\r
50                 return (this.hasKey(key)) ? this.obj[key] : null;\r
51         },\r
53         /*\r
54         Property: hasKey\r
55                 Check the presence of a specified key-value pair in the hash.\r
57         Arguments:\r
58                 key - The key\r
60         Returns:\r
61                 True if the Hash contains a value for the specified key, otherwise false\r
62         */\r
64         hasKey: function(key){\r
65                 return (key in this.obj);\r
66         },\r
68         /*\r
69         Property: set\r
70                 Adds a key-value pair to the hash or replaces a previous value associated with the key.\r
72         Arguments:\r
73                 key - The key\r
74                 value - The value\r
75         */\r
77         set: function(key, value){\r
78                 if (!this.hasKey(key)) this.length++;\r
79                 this.obj[key] = value;\r
80                 return this;\r
81         },\r
83         setLength: function(){\r
84                 this.length = 0;\r
85                 for (var p in this.obj) this.length++;\r
86                 return this;\r
87         },\r
89         /*\r
90         Property: remove\r
91                 Removes a key-value pair from the hash.\r
93         Arguments:\r
94                 key - The key\r
95         */\r
97         remove: function(key){\r
98                 if (!this.hasKey(key)) return this;\r
99                 delete this.obj[key];\r
100                 this.length--;\r
101                 return this;\r
102         },\r
104         /*\r
105         Property: each\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
108         Arguments:\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
111         */\r
113         each: function(fn, bind){\r
114                 $each(this.obj, fn, bind);\r
115         },\r
117         /*\r
118         Property: extend\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
121         Arguments:\r
122                 obj - An object containing key-value pairs\r
123         */\r
125         extend: function(obj){\r
126                 $extend(this.obj, obj);\r
127                 return this.setLength();\r
128         },\r
130         /*\r
131         Property: merge\r
132                 Merges the current hash with multiple objects.\r
133         */\r
135         merge: function(){\r
136                 this.obj = $merge.apply(null, [this.obj].extend(arguments));\r
137                 return this.setLength();\r
138         },\r
140         /*\r
141         Property: empty\r
142                 Empties all hash values properties and values.\r
143         */\r
145         empty: function(){\r
146                 this.obj = {};\r
147                 this.length = 0;\r
148                 return this;\r
149         },\r
151         /*\r
152         Property: keys\r
153                 Returns an array containing all the keys, in the same order as the values returned by <Hash.values>.\r
155         Returns:\r
156                 An array containing all the keys of the hash\r
157         */\r
159         keys: function(){\r
160                 var keys = [];\r
161                 for (var property in this.obj) keys.push(property);\r
162                 return keys;\r
163         },\r
165         /*\r
166         Property: values\r
167                 Returns an array containing all the values, in the same order as the keys returned by <Hash.keys>.\r
169         Returns:\r
170                 An array containing all the values of the hash\r
171         */\r
173         values: function(){\r
174                 var values = [];\r
175                 for (var property in this.obj) values.push(this.obj[property]);\r
176                 return values;\r
177         }\r
179 });\r
181 /* Section: Utility Functions */\r
183 /*\r
184 Function: $H\r
185         Shortcut to create a Hash from an Object.\r
186 */\r
188 function $H(obj){\r
189         return new Hash(obj);\r