summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/lang/Object.js
blob: 7b725b15ce929ad747b5da1c4b52620d39e8e7f8 (plain)
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
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2007 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#module(core)

************************************************************************ */

/**
 * Helper functions to handle Object as a Hash map.
 */
qx.OO.defineClass("qx.lang.Object");

/**
 * Check if the hash has any keys
 *
 * @param map {Object} the map to check
 * @return {Boolean} whether the map has any keys
 */
qx.Class.isEmpty = function(map)
{
  for (var s in map) {
    return false;
  }

  return true;
};


/**
 * Check whether the number of objects in the maps is at least "lenght"
 *
 * @param map {Object} the map to check
 * @param length {Integer} minimum number of objects in the map
 * @return {Boolean} whether the map contains at least "lenght" objects.
 */
qx.Class.hasMinLength = function(map, length)
{
  var i=0;

  for (var s in map)
  {
    if ((++i)>=length) {
      return true;
    }
  }

  return false;
};


/**
 * Get the number of objects in the map
 *
 * @param map {Object} the map
 * @return {Integer} number of objects in the map
 */
qx.Class.getLength = function(map)
{
  var i=0;

  for (var s in map) {
    i++;
  }

  return i;
};


/**
 * Get the keys of a map as array
 *
 * @param map {Object} the map
 * @return {Array} array of the keys of the map
 */
qx.Class.getKeys = function(map)
{
  var r = [];
  for (var s in map) {
    r.push(s);
  }

  return r;
};


/**
 * Get the keys of a map as string
 *
 * @param map {Object} the map
 * @return {String} String of the keys of the map
 *     The keys are separated by ", "
 */
qx.Class.getKeysAsString = function(map) {
  return qx.lang.Object.getKeys(map).join(", ");
};


/**
 * Get the values of a map as array
 *
 * @param map {Object} the map
 * @return {Array} array of the values of the map
 */
qx.Class.getValues = function(map)
{
  var r = [];
  for (var s in map) {
    r.push(map[s]);
  }

  return r;
};


/**
 * Merge two objects.
 *
 * If the Objects both have the same key, the value of the second object is taken.
 *
 * @param vObjectA {Object} target object
 * @param vObjectB {Object} object to be merged
 * @return {Object} ObjectA with merged values from ObjectB
 */
qx.Class.mergeWith = function(vObjectA, vObjectB)
{
  for (var vKey in vObjectB) {
    vObjectA[vKey] = vObjectB[vKey];
  }

  return vObjectA;
};


/**
 * Merge two objects. Existing values will not be overwritten.
 *
 * If the Objects both have the same key, the value of the first object is taken.
 *
 * @param vObjectA {Object} target object
 * @param vObjectB {Object} object to be merged
 * @return {Object} vObjectA with merged values from vObjectB
 */
qx.Class.carefullyMergeWith = function(vObjectA, vObjectB) {
  for (var vKey in vObjectB)
  {
    if (typeof vObjectA[vKey] === "undefined") {
      vObjectA[vKey] = vObjectB[vKey];
    }
  }

  return vObjectA;
};


/**
 * Merge a number of objects.
 *
 * @param vObjectA {Object} target object
 * @param varargs {Object} variable number of objects to merged with vObjectA
 * @return {Object} vObjectA with merged values from the other objects
 */
qx.Class.merge = function(vObjectA, varargs)
{
  var vLength = arguments.length;

  for (var i=1; i<vLength; i++) {
    qx.lang.Object.mergeWith(vObjectA, arguments[i]);
  }

  return vObjectA;
};


/**
 * Return a copy of an Object
 *
 * @param vObject {Object} Object to copy
 * @return {Object} copy of vObject
 */
qx.Class.copy = function(vObject) {
  return qx.lang.Object.mergeWith({}, vObject);
};


/**
 * Inverts a Map by exchanging the keys with the values.
 * If the map has the same values for different keys, information will get lost.
 * The values will be converted to Strings using the toString methos.
 *
 * @param vObject {Object} Map to invert
 * @return {Object} inverted Map
 */
qx.Class.invert = function(vObject) {
  var result = {};
  for (var key in vObject) {
    var value = vObject[key].toString();
    result[value] = key;
  }
  return result;
}