~launchpad-pqm/lazr-js/toolchain

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/collection/array-extras.js

  • Committer: Sidnei da Silva
  • Date: 2009-11-16 00:51:29 UTC
  • mto: This revision was merged to the branch mainline in revision 154.
  • Revision ID: sidnei.da.silva@canonical.com-20091116005129-8ibwjlboa38glaw5
- Improved generation of skin modules and revamped combo service to make it more twisty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
 
Code licensed under the BSD License:
4
 
http://developer.yahoo.com/yui/license.html
5
 
version: 3.2.0
6
 
build: 2676
7
 
*/
8
 
YUI.add('array-extras', function(Y) {
9
 
 
10
 
/**
11
 
 * Collection utilities beyond what is provided in the YUI core
12
 
 * @module collection
13
 
 * @submodule array-extras
14
 
 */
15
 
 
16
 
var L = Y.Lang, Native = Array.prototype, A = Y.Array;
17
 
 
18
 
/**
19
 
 * Adds the following array utilities to the YUI instance
20
 
 * (Y.Array).  This is in addition to the methods provided
21
 
 * in the core.
22
 
 * @class YUI~array~extras
23
 
 */
24
 
 
25
 
/**
26
 
 * Returns the index of the last item in the array
27
 
 * that contains the specified value, -1 if the
28
 
 * value isn't found.
29
 
 * @method Array.lastIndexOf
30
 
 * @static
31
 
 * @param a {Array} the array to search
32
 
 * @param val the value to search for
33
 
 * @return {int} the index of hte item that contains the value or -1
34
 
 */
35
 
A.lastIndexOf = (Native.lastIndexOf) ?
36
 
    function(a ,val) {
37
 
        return a.lastIndexOf(val);    
38
 
    } :
39
 
    function(a, val) {
40
 
        for (var i=a.length-1; i>=0; i=i-1) {
41
 
            if (a[i] === val) {
42
 
                break;
43
 
            }
44
 
        }
45
 
        return i;
46
 
    };
47
 
 
48
 
/**
49
 
 * Returns a copy of the array with the duplicate entries removed
50
 
 * @method Array.unique
51
 
 * @static
52
 
 * @param a {Array} the array to find the subset of uniques for
53
 
 * @param sort {bool} flag to denote if the array is sorted or not. Defaults to false, the more general operation
54
 
 * @return {Array} a copy of the array with duplicate entries removed
55
 
 */
56
 
A.unique = function(a, sort) {
57
 
    var b = a.slice(), i = 0, n = -1, item = null;
58
 
 
59
 
    while (i < b.length) {
60
 
        item = b[i];
61
 
        while ((n = A.lastIndexOf(b, item)) !== i) {
62
 
            b.splice(n, 1);
63
 
        }
64
 
        i += 1;
65
 
    }
66
 
 
67
 
    // Note: the sort option doesn't really belong here... I think it was added
68
 
    // because there was a way to fast path the two operations together.  That
69
 
    // implementation was not working, so I replaced it with the following.
70
 
    // Leaving it in so that the API doesn't get broken.
71
 
    if (sort) {
72
 
        if (L.isNumber(b[0])) {
73
 
            b.sort(A.numericSort);
74
 
        } else {
75
 
            b.sort();
76
 
        }
77
 
    }
78
 
 
79
 
    return b;
80
 
};
81
 
 
82
 
/**
83
 
* Executes the supplied function on each item in the array.
84
 
* Returns a new array containing the items that the supplied
85
 
* function returned true for.
86
 
* @method Array.filter
87
 
* @param a {Array} the array to iterate
88
 
* @param f {Function} the function to execute on each item
89
 
* @param o Optional context object
90
 
* @static
91
 
* @return {Array} The items on which the supplied function
92
 
* returned true. If no items matched an empty array is 
93
 
* returned.
94
 
*/
95
 
A.filter = (Native.filter) ?
96
 
    function(a, f, o) {
97
 
        return Native.filter.call(a, f, o);
98
 
    } :
99
 
    function(a, f, o) {
100
 
        var results = [];
101
 
        A.each(a, function(item, i, a) {
102
 
            if (f.call(o, item, i, a)) {
103
 
                results.push(item);
104
 
            }
105
 
        });
106
 
 
107
 
        return results;
108
 
    };
109
 
 
110
 
/**
111
 
* The inverse of filter. Executes the supplied function on each item. 
112
 
* Returns a new array containing the items that the supplied
113
 
* function returned *false* for.
114
 
* @method Array.reject
115
 
* @param a {Array} the array to iterate
116
 
* @param f {Function} the function to execute on each item
117
 
* @param o Optional context object
118
 
* @static
119
 
* @return {Array} The items on which the supplied function
120
 
* returned false.
121
 
*/
122
 
A.reject = function(a, f, o) {
123
 
    return A.filter(a, function(item, i, a) {
124
 
        return !f.call(o, item, i, a);
125
 
    });
126
 
};
127
 
 
128
 
/**
129
 
* Executes the supplied function on each item in the array.
130
 
* Iteration stops if the supplied function does not return
131
 
* a truthy value.
132
 
* @method Array.every
133
 
* @param a {Array} the array to iterate
134
 
* @param f {Function} the function to execute on each item
135
 
* @param o Optional context object
136
 
* @static
137
 
* @return {boolean} true if every item in the array returns true
138
 
* from the supplied function.
139
 
*/
140
 
A.every = (Native.every) ?
141
 
    function(a, f, o) {
142
 
        return Native.every.call(a,f,o);
143
 
    } :
144
 
    function(a, f, o) {
145
 
        for (var i = 0, l = a.length; i < l; i=i+1) {
146
 
            if (!f.call(o, a[i], i, a)) {
147
 
                return false;
148
 
            }
149
 
        }
150
 
 
151
 
        return true;
152
 
    };
153
 
 
154
 
/**
155
 
* Executes the supplied function on each item in the array.
156
 
* @method Array.map
157
 
* @param a {Array} the array to iterate
158
 
* @param f {Function} the function to execute on each item
159
 
* @param o Optional context object
160
 
* @static
161
 
* @return {Array} A new array containing the return value
162
 
* of the supplied function for each item in the original
163
 
* array.
164
 
*/
165
 
A.map = (Native.map) ? 
166
 
    function(a, f, o) {
167
 
        return Native.map.call(a, f, o);
168
 
    } :
169
 
    function(a, f, o) {
170
 
        var results = [];
171
 
        A.each(a, function(item, i, a) {
172
 
            results.push(f.call(o, item, i, a));
173
 
        });
174
 
        return results;
175
 
    };
176
 
 
177
 
 
178
 
/**
179
 
* Executes the supplied function on each item in the array.
180
 
* Reduce "folds" the array into a single value.  The callback
181
 
* function receives four arguments:
182
 
* the value from the previous callback call (or the initial value), 
183
 
* the value of the current element, the current index, and 
184
 
* the array over which iteration is occurring.
185
 
* @method Array.reduce
186
 
* @param a {Array} the array to iterate
187
 
* @param init The initial value to start from
188
 
* @param f {Function} the function to execute on each item. It
189
 
* is responsible for returning the updated value of the
190
 
* computation.
191
 
* @param o Optional context object
192
 
* @static
193
 
* @return A value that results from iteratively applying the
194
 
* supplied function to each element in the array.
195
 
*/
196
 
A.reduce = (Native.reduce) ?
197
 
    function(a, init, f, o) {
198
 
        //Firefox's Array.reduce does not allow inclusion of a
199
 
        //  thisObject, so we need to implement it manually
200
 
        return Native.reduce.call(a, function(init, item, i, a) {
201
 
            return f.call(o, init, item, i, a);
202
 
        }, init);
203
 
    } :
204
 
    function(a, init, f, o) {
205
 
        var r = init;
206
 
        A.each(a, function (item, i, a) {
207
 
            r = f.call(o, r, item, i, a);
208
 
        });
209
 
        return r;
210
 
    };
211
 
 
212
 
 
213
 
/**
214
 
* Executes the supplied function on each item in the array,
215
 
* searching for the first item that matches the supplied
216
 
* function.
217
 
* @method Array.find
218
 
* @param a {Array} the array to search
219
 
* @param f {Function} the function to execute on each item. 
220
 
* Iteration is stopped as soon as this function returns true
221
 
* on an item.
222
 
* @param o Optional context object
223
 
* @static
224
 
* @return {object} the first item that the supplied function
225
 
* returns true for, or null if it never returns true
226
 
*/
227
 
A.find = function(a, f, o) {
228
 
    for(var i=0, l = a.length; i < l; i++) {
229
 
        if (f.call(o, a[i], i, a)) {
230
 
            return a[i];
231
 
        }
232
 
    }
233
 
    return null;
234
 
};
235
 
 
236
 
/**
237
 
* Iterates over an array, returning a new array of all the elements
238
 
* that match the supplied regular expression
239
 
* @method Array.grep
240
 
* @param a {Array} a collection to iterate over
241
 
* @param pattern {RegExp} The regular expression to test against 
242
 
* each item
243
 
* @static
244
 
* @return {Array} All the items in the collection that 
245
 
* produce a match against the supplied regular expression. 
246
 
* If no items match, an empty array is returned.
247
 
*/
248
 
A.grep = function (a, pattern) {
249
 
    return A.filter(a, function (item, index) {
250
 
        return pattern.test(item);
251
 
    });
252
 
};
253
 
 
254
 
 
255
 
/**
256
 
* Partitions an array into two new arrays, one with the items
257
 
* that match the supplied function, and one with the items that
258
 
* do not.
259
 
* @method Array.partition
260
 
* @param a {Array} a collection to iterate over
261
 
* @paran f {Function} a function that will receive each item 
262
 
* in the collection and its index.
263
 
* @param o Optional execution context of f.
264
 
* @static
265
 
* @return An object with two members, 'matches' and 'rejects',
266
 
* that are arrays containing the items that were selected or 
267
 
* rejected by the test function (or an empty array).
268
 
*/
269
 
A.partition = function (a, f, o) {
270
 
    var results = {
271
 
        matches: [], 
272
 
        rejects: []
273
 
    };
274
 
 
275
 
    A.each(a, function (item, index) {
276
 
        var set = f.call(o, item, index, a) ? results.matches : results.rejects;
277
 
        set.push(item);
278
 
    });
279
 
 
280
 
    return results;
281
 
};
282
 
 
283
 
/**
284
 
* Creates an array of arrays by pairing the corresponding
285
 
* elements of two arrays together into a new array.
286
 
* @method Array.zip
287
 
* @param a {Array} a collection to iterate over
288
 
* @param a2 {Array} another collection whose members will be 
289
 
* paired with members of the first parameter
290
 
* @static
291
 
* @return An array of arrays formed by pairing each element 
292
 
* of the first collection with an item in the second collection 
293
 
* having the corresponding index.
294
 
*/
295
 
A.zip = function (a, a2) {
296
 
    var results = [];
297
 
    A.each(a, function (item, index) {
298
 
        results.push([item, a2[index]]);
299
 
    });
300
 
    return results;
301
 
};
302
 
 
303
 
A.forEach = A.each;
304
 
 
305
 
 
306
 
}, '3.2.0' );