~andreserl/maas/precise_packaging_sru

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/array-extras/array-extras-debug.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-08 23:21:23 UTC
  • mfrom: (145.2.21 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130308232123-tws9nffx8f6eoa0f
* debian/maas-dhcp.maas-dhcp-server.upstart: leases file should be owned
  by user/group 'dhcpd' instead of root.
* debian/control: Force dependency version for python-django to
  (>= 1.3.1-4ubuntu1.7).
* Continue to ship yui3 and raphael with MAAS.
  - debian/patches/04_precise_no_yui_root.patch: Add.
  - debian/control: Drop dependencies on yui3 and raphael.
  - debian/source/include-binaries: Add to not FTBFS
  - debian/extras/jslibs: Ship JS libraries.
  - debian/copyright: Update copyright to reflect libraries license.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('array-extras', function(Y) {
 
8
 
 
9
/**
 
10
Adds additional utility methods to the `Y.Array` class.
 
11
 
 
12
@module collection
 
13
@submodule array-extras
 
14
**/
 
15
 
 
16
var A          = Y.Array,
 
17
    L          = Y.Lang,
 
18
    ArrayProto = Array.prototype;
 
19
 
 
20
/**
 
21
Returns the index of the last item in the array that contains the specified
 
22
value, or `-1` if the value isn't found.
 
23
 
 
24
@method lastIndexOf
 
25
@param {Array} a Array to search in.
 
26
@param {Any} val Value to search for.
 
27
@param {Number} [fromIndex] Index at which to start searching backwards.
 
28
  Defaults to the array's length - 1. If negative, it will be taken as an offset
 
29
  from the end of the array. If the calculated index is less than 0, the array
 
30
  will not be searched and `-1` will be returned.
 
31
@return {Number} Index of the item that contains the value, or `-1` if not
 
32
  found.
 
33
@static
 
34
@for Array
 
35
**/
 
36
A.lastIndexOf = L._isNative(ArrayProto.lastIndexOf) ?
 
37
    function(a, val, fromIndex) {
 
38
        // An undefined fromIndex is still considered a value by some (all?)
 
39
        // native implementations, so we can't pass it unless it's actually
 
40
        // specified.
 
41
        return fromIndex || fromIndex === 0 ? a.lastIndexOf(val, fromIndex) :
 
42
                a.lastIndexOf(val);
 
43
    } :
 
44
    function(a, val, fromIndex) {
 
45
        var len = a.length,
 
46
            i   = len - 1;
 
47
 
 
48
        if (fromIndex || fromIndex === 0) {
 
49
            i = Math.min(fromIndex < 0 ? len + fromIndex : fromIndex, len);
 
50
        }
 
51
 
 
52
        if (i > -1 && len > 0) {
 
53
            for (; i > -1; --i) {
 
54
                if (i in a && a[i] === val) {
 
55
                    return i;
 
56
                }
 
57
            }
 
58
        }
 
59
 
 
60
        return -1;
 
61
    };
 
62
 
 
63
/**
 
64
Returns a copy of the specified array with duplicate items removed.
 
65
 
 
66
@method unique
 
67
@param {Array} a Array to dedupe.
 
68
@return {Array} Copy of the array with duplicate items removed.
 
69
@static
 
70
**/
 
71
A.unique = function(a, sort) {
 
72
    // Note: the sort param is deprecated and intentionally undocumented since
 
73
    // YUI 3.3.0. It never did what the API docs said it did (see the older
 
74
    // comment below as well).
 
75
    var i       = 0,
 
76
        len     = a.length,
 
77
        results = [],
 
78
        item, j;
 
79
 
 
80
    for (; i < len; ++i) {
 
81
        item = a[i];
 
82
 
 
83
        // This loop iterates over the results array in reverse order and stops
 
84
        // if it finds an item that matches the current input array item (a
 
85
        // dupe). If it makes it all the way through without finding a dupe, the
 
86
        // current item is pushed onto the results array.
 
87
        for (j = results.length; j > -1; --j) {
 
88
            if (item === results[j]) {
 
89
                break;
 
90
            }
 
91
        }
 
92
 
 
93
        if (j === -1) {
 
94
            results.push(item);
 
95
        }
 
96
    }
 
97
 
 
98
    // Note: the sort option doesn't really belong here... I think it was added
 
99
    // because there was a way to fast path the two operations together.  That
 
100
    // implementation was not working, so I replaced it with the following.
 
101
    // Leaving it in so that the API doesn't get broken.
 
102
    if (sort) {
 
103
        Y.log('The sort parameter is deprecated and will be removed in a future version of YUI.', 'warn', 'deprecated');
 
104
 
 
105
        if (L.isNumber(results[0])) {
 
106
            results.sort(A.numericSort);
 
107
        } else {
 
108
            results.sort();
 
109
        }
 
110
    }
 
111
 
 
112
    return results;
 
113
};
 
114
 
 
115
/**
 
116
Executes the supplied function on each item in the array. Returns a new array
 
117
containing the items for which the supplied function returned a truthy value.
 
118
 
 
119
@method filter
 
120
@param {Array} a Array to filter.
 
121
@param {Function} f Function to execute on each item.
 
122
@param {Object} [o] Optional context object.
 
123
@return {Array} Array of items for which the supplied function returned a
 
124
  truthy value (empty if it never returned a truthy value).
 
125
@static
 
126
*/
 
127
A.filter = L._isNative(ArrayProto.filter) ?
 
128
    function(a, f, o) {
 
129
        return a.filter(f, o);
 
130
    } :
 
131
    function(a, f, o) {
 
132
        var i       = 0,
 
133
            len     = a.length,
 
134
            results = [],
 
135
            item;
 
136
 
 
137
        for (; i < len; ++i) {
 
138
            if (i in a) {
 
139
                item = a[i];
 
140
 
 
141
                if (f.call(o, item, i, a)) {
 
142
                    results.push(item);
 
143
                }
 
144
            }
 
145
        }
 
146
 
 
147
        return results;
 
148
    };
 
149
 
 
150
/**
 
151
The inverse of `Array.filter()`. Executes the supplied function on each item.
 
152
Returns a new array containing the items for which the supplied function
 
153
returned `false`.
 
154
 
 
155
@method reject
 
156
@param {Array} a the array to iterate.
 
157
@param {Function} f the function to execute on each item.
 
158
@param {object} [o] Optional context object.
 
159
@return {Array} The items for which the supplied function returned `false`.
 
160
@static
 
161
*/
 
162
A.reject = function(a, f, o) {
 
163
    return A.filter(a, function(item, i, a) {
 
164
        return !f.call(o, item, i, a);
 
165
    });
 
166
};
 
167
 
 
168
/**
 
169
Executes the supplied function on each item in the array. Iteration stops if the
 
170
supplied function does not return a truthy value.
 
171
 
 
172
@method every
 
173
@param {Array} a the array to iterate.
 
174
@param {Function} f the function to execute on each item.
 
175
@param {Object} [o] Optional context object.
 
176
@return {Boolean} `true` if every item in the array returns `true` from the
 
177
  supplied function, `false` otherwise.
 
178
@static
 
179
*/
 
180
A.every = L._isNative(ArrayProto.every) ?
 
181
    function(a, f, o) {
 
182
        return a.every(f, o);
 
183
    } :
 
184
    function(a, f, o) {
 
185
        for (var i = 0, l = a.length; i < l; ++i) {
 
186
            if (i in a && !f.call(o, a[i], i, a)) {
 
187
                return false;
 
188
            }
 
189
        }
 
190
 
 
191
        return true;
 
192
    };
 
193
 
 
194
/**
 
195
Executes the supplied function on each item in the array and returns a new array
 
196
containing all the values returned by the supplied function.
 
197
 
 
198
@example
 
199
 
 
200
    // Convert an array of numbers into an array of strings.
 
201
    Y.Array.map([1, 2, 3, 4], function (item) {
 
202
      return '' + item;
 
203
    });
 
204
    // => ['1', '2', '3', '4']
 
205
 
 
206
@method map
 
207
@param {Array} a the array to iterate.
 
208
@param {Function} f the function to execute on each item.
 
209
@param {object} [o] Optional context object.
 
210
@return {Array} A new array containing the return value of the supplied function
 
211
  for each item in the original array.
 
212
@static
 
213
*/
 
214
A.map = L._isNative(ArrayProto.map) ?
 
215
    function(a, f, o) {
 
216
        return a.map(f, o);
 
217
    } :
 
218
    function(a, f, o) {
 
219
        var i       = 0,
 
220
            len     = a.length,
 
221
            results = a.concat();
 
222
 
 
223
        for (; i < len; ++i) {
 
224
            if (i in a) {
 
225
                results[i] = f.call(o, a[i], i, a);
 
226
            }
 
227
        }
 
228
 
 
229
        return results;
 
230
    };
 
231
 
 
232
 
 
233
/**
 
234
Executes the supplied function on each item in the array, "folding" the array
 
235
into a single value.
 
236
 
 
237
@method reduce
 
238
@param {Array} a Array to iterate.
 
239
@param {Any} init Initial value to start with.
 
240
@param {Function} f Function to execute on each item. This function should
 
241
  update and return the value of the computation. It will receive the following
 
242
  arguments:
 
243
    @param {Any} f.previousValue Value returned from the previous iteration,
 
244
      or the initial value if this is the first iteration.
 
245
    @param {Any} f.currentValue Value of the current item being iterated.
 
246
    @param {Number} f.index Index of the current item.
 
247
    @param {Array} f.array Array being iterated.
 
248
@param {Object} [o] Optional context object.
 
249
@return {Any} Final result from iteratively applying the given function to each
 
250
  element in the array.
 
251
@static
 
252
*/
 
253
A.reduce = L._isNative(ArrayProto.reduce) ?
 
254
    function(a, init, f, o) {
 
255
        // ES5 Array.reduce doesn't support a thisObject, so we need to
 
256
        // implement it manually.
 
257
        return a.reduce(function(init, item, i, a) {
 
258
            return f.call(o, init, item, i, a);
 
259
        }, init);
 
260
    } :
 
261
    function(a, init, f, o) {
 
262
        var i      = 0,
 
263
            len    = a.length,
 
264
            result = init;
 
265
 
 
266
        for (; i < len; ++i) {
 
267
            if (i in a) {
 
268
                result = f.call(o, result, a[i], i, a);
 
269
            }
 
270
        }
 
271
 
 
272
        return result;
 
273
    };
 
274
 
 
275
/**
 
276
Executes the supplied function on each item in the array, searching for the
 
277
first item that matches the supplied function.
 
278
 
 
279
@method find
 
280
@param {Array} a the array to search.
 
281
@param {Function} f the function to execute on each item. Iteration is stopped
 
282
  as soon as this function returns `true`.
 
283
@param {Object} [o] Optional context object.
 
284
@return {Object} the first item that the supplied function returns `true` for,
 
285
  or `null` if it never returns `true`.
 
286
@static
 
287
*/
 
288
A.find = function(a, f, o) {
 
289
    for (var i = 0, l = a.length; i < l; i++) {
 
290
        if (i in a && f.call(o, a[i], i, a)) {
 
291
            return a[i];
 
292
        }
 
293
    }
 
294
    return null;
 
295
};
 
296
 
 
297
/**
 
298
Iterates over an array, returning a new array of all the elements that match the
 
299
supplied regular expression.
 
300
 
 
301
@method grep
 
302
@param {Array} a Array to iterate over.
 
303
@param {RegExp} pattern Regular expression to test against each item.
 
304
@return {Array} All the items in the array that produce a match against the
 
305
  supplied regular expression. If no items match, an empty array is returned.
 
306
@static
 
307
*/
 
308
A.grep = function(a, pattern) {
 
309
    return A.filter(a, function(item, index) {
 
310
        return pattern.test(item);
 
311
    });
 
312
};
 
313
 
 
314
/**
 
315
Partitions an array into two new arrays, one with the items for which the
 
316
supplied function returns `true`, and one with the items for which the function
 
317
returns `false`.
 
318
 
 
319
@method partition
 
320
@param {Array} a Array to iterate over.
 
321
@param {Function} f Function to execute for each item in the array. It will
 
322
  receive the following arguments:
 
323
    @param {Any} f.item Current item.
 
324
    @param {Number} f.index Index of the current item.
 
325
    @param {Array} f.array The array being iterated.
 
326
@param {Object} [o] Optional execution context.
 
327
@return {Object} An object with two properties: `matches` and `rejects`. Each is
 
328
  an array containing the items that were selected or rejected by the test
 
329
  function (or an empty array if none).
 
330
@static
 
331
*/
 
332
A.partition = function(a, f, o) {
 
333
    var results = {
 
334
        matches: [],
 
335
        rejects: []
 
336
    };
 
337
 
 
338
    A.each(a, function(item, index) {
 
339
        var set = f.call(o, item, index, a) ? results.matches : results.rejects;
 
340
        set.push(item);
 
341
    });
 
342
 
 
343
    return results;
 
344
};
 
345
 
 
346
/**
 
347
Creates an array of arrays by pairing the corresponding elements of two arrays
 
348
together into a new array.
 
349
 
 
350
@method zip
 
351
@param {Array} a Array to iterate over.
 
352
@param {Array} a2 Another array whose values will be paired with values of the
 
353
  first array.
 
354
@return {Array} An array of arrays formed by pairing each element of the first
 
355
  array with an item in the second array having the corresponding index.
 
356
@static
 
357
*/
 
358
A.zip = function(a, a2) {
 
359
    var results = [];
 
360
    A.each(a, function(item, index) {
 
361
        results.push([item, a2[index]]);
 
362
    });
 
363
    return results;
 
364
};
 
365
 
 
366
 
 
367
}, '3.5.1' ,{requires:['yui-base']});