* Hash.extend must be Object.append instead of the Type extend method
[mootools/dkf.git] / Source / Types / Object.js
blob717de14bf84ab4a8b98bd3779408f6092e1fb82d
1 /*
2 ---
4 name: Object
6 description: Object generic methods
8 license: MIT-style license.
10 requires: Type
12 provides: [Object, Hash]
14 ...
18 Object.extend({
19         
20         subset: function(object, keys){
21                 var results = {};
22                 for (var i = 0, l = keys.length; i < l; i++){
23                         var k = keys[i];
24                         results[k] = object[k];
25                 }
26                 return results;
27         },
28         
29         map: function(object, fn, bind){
30                 var results = {};
31                 for (var key in object){
32                         if (object.hasOwnProperty(key)) results[key] = fn.call(bind, object[key], key, object);
33                 }
34                 return results;
35         },
36         
37         filter: function(object, fn, bind){
38                 var results = {};
39                 Object.each(object, function(value, key){
40                         if (fn.call(bind, value, key, object)) results[key] = value;
41                 });
42                 return results;
43         },
44         
45         every: function(object, fn, bind){
46                 for (var key in object){
47                         if (object.hasOwnProperty(key) && !fn.call(bind, object[key], key)) return false;
48                 }
49                 return true;
50         },
51         
52         some: function(object, fn, bind){
53                 for (var key in object){
54                         if (object.hasOwnProperty(key) && fn.call(bind, object[key], key)) return true;
55                 }
56                 return false;
57         },
58         
59         keys: function(object){
60                 var keys = [];
61                 for (var key in object){
62                         if (object.hasOwnProperty(key)) keys.push(key);
63                 }
64                 return keys;
65         },
66         
67         values: function(object){
68                 var values = [];
69                 for (var key in object){
70                         if (object.hasOwnProperty(key)) values.push(object[key]);
71                 }
72                 return values;
73         },
74         
75         getLength: function(object){
76                 return Object.keys(object).length;
77         },
78         
79         keyOf: function(object, value){
80                 for (var key in object){
81                         if (object.hasOwnProperty(key) && object[key] === value) return key;
82                 }
83                 return null;
84         },
85         
86         contains: function(object, value){
87                 return Object.keyOf(object, value) != null;
88         },
89         
90         toQueryString: function(object, base){
91                 var queryString = [];
92                 
93                 Object.each(object, function(value, key){
94                         if (base) key = base + '[' + key + ']';
95                         var result;
96                         switch (typeOf(value)){
97                                 case 'object': result = Object.toQueryString(value, key); break;
98                                 case 'array':
99                                         var qs = {};
100                                         value.each(function(val, i){
101                                                 qs[i] = val;
102                                         });
103                                         result = Object.toQueryString(qs, key);
104                                 break;
105                                 default: result = key + '=' + encodeURIComponent(value);
106                         }
107                         if (value != undefined) queryString.push(result);
108                 });
110                 return queryString.join('&');
111         }
112         
116 //<1.2compat>
118 Hash.implement({
120         has: Object.prototype.hasOwnProperty,
122         keyOf: function(value){
123                 return Object.keyOf(this, value);
124         },
126         hasValue: function(value){
127                 return Object.contains(this, value);
128         },
130         extend: function(properties){
131                 Hash.each(properties || {}, function(value, key){
132                         Hash.set(this, key, value);
133                 }, this);
134                 return this;
135         },
137         combine: function(properties){
138                 Hash.each(properties || {}, function(value, key){
139                         Hash.include(this, key, value);
140                 }, this);
141                 return this;
142         },
144         erase: function(key){
145                 if (this.hasOwnProperty(key)) delete this[key];
146                 return this;
147         },
149         get: function(key){
150                 return (this.hasOwnProperty(key)) ? this[key] : null;
151         },
153         set: function(key, value){
154                 if (!this[key] || this.hasOwnProperty(key)) this[key] = value;
155                 return this;
156         },
158         empty: function(){
159                 Hash.each(this, function(value, key){
160                         delete this[key];
161                 }, this);
162                 return this;
163         },
165         include: function(key, value){
166                 if (this[key] == undefined) this[key] = value;
167                 return this;
168         },
170         map: function(fn, bind){
171                 return new Hash(Object.map(this, fn, bind));
172         },
174         filter: function(fn, bind){
175                 return new Hash(Object.filter(this, fn, bind));
176         },
178         every: function(fn, bind){
179                 return Object.every(this, fn, bind);
180         },
182         some: function(fn, bind){
183                 return Object.some(this, fn, bind);
184         },
186         getKeys: function(){
187                 return Object.keys(this);
188         },
190         getValues: function(){
191                 return Object.values(this);
192         },
194         toQueryString: function(base){
195                 return Object.toQueryString(this, base);
196         }
200 Hash.extend = Object.append;
202 Hash.alias({indexOf: 'keyOf', contains: 'hasValue'});
204 //</1.2compat>