2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('array-extras', function(Y) {
11 * Collection utilities beyond what is provided in the YUI core
13 * @submodule array-extras
16 var L = Y.Lang, Native = Array.prototype, A = Y.Array;
19 * Adds the following array utilities to the YUI instance
20 * (Y.Array). This is in addition to the methods provided
22 * @class YUI~array~extras
26 * Returns the index of the last item in the array that contains the specified
27 * value, or -1 if the value isn't found.
28 * @method Array.lastIndexOf
30 * @param {Array} a Array to search in.
31 * @param {any} val Value to search for.
32 * @param {Number} fromIndex (optional) Index at which to start searching
33 * backwards. Defaults to the array's length - 1. If negative, it will be
34 * taken as an offset from the end of the array. If the calculated index is
35 * less than 0, the array will not be searched and -1 will be returned.
36 * @return {Number} Index of the item that contains the value, or -1 if not
39 A.lastIndexOf = Native.lastIndexOf ?
40 function(a, val, fromIndex) {
41 // An undefined fromIndex is still considered a value by some (all?)
42 // native implementations, so we can't pass it unless it's actually
44 return fromIndex || fromIndex === 0 ? a.lastIndexOf(val, fromIndex) :
47 function(a, val, fromIndex) {
51 if (fromIndex || fromIndex === 0) {
52 i = Math.min(fromIndex < 0 ? len + fromIndex : fromIndex, len);
55 if (i > -1 && len > 0) {
67 * Returns a copy of the specified array with duplicate items removed.
68 * @method Array.unique
69 * @param {Array} a Array to dedupe.
70 * @return {Array} Copy of the array with duplicate items removed.
73 A.unique = function(a, sort) {
74 // Note: the sort param is deprecated and intentionally undocumented since
75 // YUI 3.3.0. It never did what the API docs said it did (see the older
76 // comment below as well).
82 for (; i < len; ++i) {
85 // This loop iterates over the results array in reverse order and stops
86 // if it finds an item that matches the current input array item (a
87 // dupe). If it makes it all the way through without finding a dupe, the
88 // current item is pushed onto the results array.
89 for (j = results.length; j > -1; --j) {
90 if (item === results[j]) {
100 // Note: the sort option doesn't really belong here... I think it was added
101 // because there was a way to fast path the two operations together. That
102 // implementation was not working, so I replaced it with the following.
103 // Leaving it in so that the API doesn't get broken.
105 if (L.isNumber(results[0])) {
106 results.sort(A.numericSort);
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 * @method Array.filter
119 * @param {Array} a Array to filter.
120 * @param {Function} f Function to execute on each item.
121 * @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).
126 A.filter = Native.filter ?
128 return a.filter(f, o);
136 for (; i < len; ++i) {
139 if (f.call(o, item, i, a)) {
148 * The inverse of filter. Executes the supplied function on each item.
149 * Returns a new array containing the items that the supplied
150 * function returned *false* for.
151 * @method Array.reject
152 * @param {Array} a the array to iterate.
153 * @param {Function} f the function to execute on each item.
154 * @param {object} o Optional context object.
156 * @return {Array} The items on which the supplied function
159 A.reject = function(a, f, o) {
160 return A.filter(a, function(item, i, a) {
161 return !f.call(o, item, i, a);
166 * Executes the supplied function on each item in the array.
167 * Iteration stops if the supplied function does not return
169 * @method Array.every
170 * @param {Array} a the array to iterate.
171 * @param {Function} f the function to execute on each item.
172 * @param {object} o Optional context object.
174 * @return {boolean} true if every item in the array returns true
175 * from the supplied function.
177 A.every = Native.every ?
179 return a.every(f, o);
182 for (var i = 0, l = a.length; i < l; ++i) {
183 if (!f.call(o, a[i], i, a)) {
192 * Executes the supplied function on each item in the array.
194 * @param {Array} a the array to iterate.
195 * @param {Function} f the function to execute on each item.
196 * @param {object} o Optional context object.
198 * @return {Array} A new array containing the return value
199 * of the supplied function for each item in the original
209 results = a.concat();
211 for (; i < len; ++i) {
212 results[i] = f.call(o, a[i], i, a);
220 * Executes the supplied function on each item in the array.
221 * Reduce "folds" the array into a single value. The callback
222 * function receives four arguments:
223 * the value from the previous callback call (or the initial value),
224 * the value of the current element, the current index, and
225 * the array over which iteration is occurring.
226 * @method Array.reduce
227 * @param {Array} a the array to iterate.
228 * @param {any} init The initial value to start from.
229 * @param {Function} f the function to execute on each item. It
230 * is responsible for returning the updated value of the
232 * @param {object} o Optional context object.
234 * @return {any} A value that results from iteratively applying the
235 * supplied function to each element in the array.
237 A.reduce = Native.reduce ?
238 function(a, init, f, o) {
239 // ES5 Array.reduce doesn't support a thisObject, so we need to
240 // implement it manually
241 return a.reduce(function(init, item, i, a) {
242 return f.call(o, init, item, i, a);
245 function(a, init, f, o) {
250 for (; i < len; ++i) {
251 result = f.call(o, result, a[i], i, a);
259 * Executes the supplied function on each item in the array,
260 * searching for the first item that matches the supplied
263 * @param {Array} a the array to search.
264 * @param {Function} f the function to execute on each item.
265 * Iteration is stopped as soon as this function returns true
267 * @param {object} o Optional context object.
269 * @return {object} the first item that the supplied function
270 * returns true for, or null if it never returns true.
272 A.find = function(a, f, o) {
273 for (var i = 0, l = a.length; i < l; i++) {
274 if (f.call(o, a[i], i, a)) {
282 * Iterates over an array, returning a new array of all the elements
283 * that match the supplied regular expression
285 * @param {Array} a a collection to iterate over.
286 * @param {RegExp} pattern The regular expression to test against
289 * @return {Array} All the items in the collection that
290 * produce a match against the supplied regular expression.
291 * If no items match, an empty array is returned.
293 A.grep = function(a, pattern) {
294 return A.filter(a, function(item, index) {
295 return pattern.test(item);
301 * Partitions an array into two new arrays, one with the items
302 * that match the supplied function, and one with the items that
304 * @method Array.partition
305 * @param {Array} a a collection to iterate over.
306 * @param {Function} f a function that will receive each item
307 * in the collection and its index.
308 * @param {object} o Optional execution context of f.
310 * @return {object} An object with two members, 'matches' and 'rejects',
311 * that are arrays containing the items that were selected or
312 * rejected by the test function (or an empty array).
314 A.partition = function(a, f, o) {
320 A.each(a, function(item, index) {
321 var set = f.call(o, item, index, a) ? results.matches : results.rejects;
329 * Creates an array of arrays by pairing the corresponding
330 * elements of two arrays together into a new array.
332 * @param {Array} a a collection to iterate over.
333 * @param {Array} a2 another collection whose members will be
334 * paired with members of the first parameter.
336 * @return {array} An array of arrays formed by pairing each element
337 * of the first collection with an item in the second collection
338 * having the corresponding index.
340 A.zip = function(a, a2) {
342 A.each(a, function(item, index) {
343 results.push([item, a2[index]]);
349 * forEach is an alias of Array.each. This is part of the
351 * @method Array.forEach