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

   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 for arrays.
 *
 * The native JavaScript Array is not modified by this class. However,
 * there are modifications to the native Array in {@link qx.lang.Core} for
 * browsers that do not support certain JavaScript 1.6 features natively .
 *
 * The additions implemented here may be added directly to native Array by
 * a setting in {@link qx.lang.Prototypes}. This feature is not enabled by
 * default.
 *
 * The string/array generics introduced in JavaScript 1.6 are supported by
 * {@link qx.lang.Generics}.
 */
qx.OO.defineClass("qx.lang.Array");

/**
 * Convert an arguments object into an array
 *
 * @param args {arguments} arguments object
 * @return {Array}
 */
qx.lang.Array.fromArguments = function(args) {
  return Array.prototype.slice.call(args, 0);
};


/**
 * Expand shorthand definition to a four element list.
 * This is an utility function for padding/margin and all other shorthand handling.
 *
 * @param input {Array} array with one to four elements
 * @return {Array} array with four elements
 */
qx.lang.Array.fromShortHand = function(input)
{
  var len = input.length;

  if (len > 4 || len == 0) {
    this.error("Invalid number of arguments!");
  }

  var result = qx.lang.Array.copy(input);

  // Copy Values (according to the length)
  switch(len)
  {
    case 1:
      result[1] = result[2] = result[3] = result[0];
      break;

    case 2:
      result[2] = result[0];
      // no break here

    case 3:
      result[3] = result[1];
  }

  // Return list with 4 items
  return result;
};


/**
 * Return a copy of the given array
 *
 * @param arr {Array} the array to copy
 * @return {Array} copy of the array
 */
qx.lang.Array.copy = function(arr) {
  return arr.concat();
};


/**
 * Return a copy of the given array
 * The same as {@link qx.lang.Array.copy}
 *
 * @param arr {Array} the array to copy
 * @return {Array} copy of the array
 */
qx.lang.Array.clone = function(arr) {
  return arr.concat();
};


/**
 * Return the last element of an array
 *
 * @param arr {Array} the array
 * @return {var} the last element of the array
 */
qx.lang.Array.getLast = function(arr) {
  return arr[arr.length-1];
};


/**
 * Return the first element of an array
 *
 * @param arr {Array} the array
 * @return {var} the first element of the array
 */
qx.lang.Array.getFirst = function(arr) {
  return arr[0];
};


/**
 * Insert an element at a given position into the array
 *
 * @param arr {Array} the array
 * @param obj {var} the element to insert
 * @param i {Integer} position where to insert the element into the array
 * @return {Array} the array
 */
qx.lang.Array.insertAt = function(arr, obj, i)
{
  arr.splice(i, 0, obj);

  return arr;
};


/**
 * Insert an element into the array before a given second element
 *
 * @param arr {Array} the array
 * @param obj {var} object to be inserted
 * @param obj2 {var} insert obj1 before this object
 * @return {Array} the array
 */
qx.lang.Array.insertBefore = function(arr, obj, obj2)
{
  var i = arr.indexOf(obj2);

  if (i == -1)
  {
    arr.push(obj);
  }
  else
  {
    arr.splice(i, 0, obj);
  }

  return arr;
};


/**
 * Insert an element into the array after a given second element
 *
 * @param arr {Array} the array
 * @param obj {var} object to be inserted
 * @param obj2 {var} insert obj1 after this object
 * @return {Array} the array
 */
qx.lang.Array.insertAfter = function(arr, obj, obj2)
{
  var i = arr.indexOf(obj2);

  if (i == -1 || i == (arr.length-1))
  {
    arr.push(obj);
  }
  else
  {
    arr.splice(i+1, 0, obj);
  }

  return arr;
};


/**
 * Remove an element from the array at the given index
 *
 * @param arr {Array} the array
 * @param i {Integer} index of the element to be removed
 * @return {Array} the array with the element removed
 */
qx.lang.Array.removeAt = function(arr, i) {
  return arr.splice(i, 1);
};


/**
 * Remmove all elements from the array
 *
 * @param arr {Array} the array
 * @return {Array} empty array
 */
qx.lang.Array.removeAll = function(arr) {
  return arr.splice(0, arr.length);
};


/**
 * Append the elements of an array to the array
 *
 * @param arr {Array} the array
 * @param a {Array} the elements of this array will be appended to the array
 */
qx.lang.Array.append = function(arr, a) {
  Array.prototype.push.apply(arr, a);
};


/**
 * Remove an element from the array
 *
 * @param arr {Array} the array
 * @param obj {var} element to be removed from the array
 * @return {Array} array with the element removed
 */
qx.lang.Array.remove = function(arr, obj)
{
  var i = arr.indexOf(obj);

  if (i != -1) {
    return arr.splice(i, 1);
  }
};


/**
 * Whether the array contains the given element
 *
 * @param arr {Array} the array
 * @param obj {var} object to look for
 * @return {Boolean} whether the array contains the element
 */
qx.lang.Array.contains = function(arr, obj) {
  return arr.indexOf(obj) != -1;
};