3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('array-extras', function(Y) {
10
Adds additional utility methods to the `Y.Array` class.
13
@submodule array-extras
18
ArrayProto = Array.prototype;
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.
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
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
41
return fromIndex || fromIndex === 0 ? a.lastIndexOf(val, fromIndex) :
44
function(a, val, fromIndex) {
48
if (fromIndex || fromIndex === 0) {
49
i = Math.min(fromIndex < 0 ? len + fromIndex : fromIndex, len);
52
if (i > -1 && len > 0) {
54
if (i in a && a[i] === val) {
64
Returns a copy of the specified array with duplicate items removed.
67
@param {Array} a Array to dedupe.
68
@return {Array} Copy of the array with duplicate items removed.
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).
80
for (; i < len; ++i) {
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]) {
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.
103
Y.log('The sort parameter is deprecated and will be removed in a future version of YUI.', 'warn', 'deprecated');
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.
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).
127
A.filter = L._isNative(ArrayProto.filter) ?
129
return a.filter(f, o);
137
for (; i < len; ++i) {
141
if (f.call(o, item, i, a)) {
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
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`.
162
A.reject = function(a, f, o) {
163
return A.filter(a, function(item, i, a) {
164
return !f.call(o, item, i, a);
169
Executes the supplied function on each item in the array. Iteration stops if the
170
supplied function does not return a truthy value.
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.
180
A.every = L._isNative(ArrayProto.every) ?
182
return a.every(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)) {
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.
200
// Convert an array of numbers into an array of strings.
201
Y.Array.map([1, 2, 3, 4], function (item) {
204
// => ['1', '2', '3', '4']
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.
214
A.map = L._isNative(ArrayProto.map) ?
221
results = a.concat();
223
for (; i < len; ++i) {
225
results[i] = f.call(o, a[i], i, a);
234
Executes the supplied function on each item in the array, "folding" the array
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
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.
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);
261
function(a, init, f, o) {
266
for (; i < len; ++i) {
268
result = f.call(o, result, a[i], i, a);
276
Executes the supplied function on each item in the array, searching for the
277
first item that matches the supplied function.
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`.
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)) {
298
Iterates over an array, returning a new array of all the elements that match the
299
supplied regular expression.
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.
308
A.grep = function(a, pattern) {
309
return A.filter(a, function(item, index) {
310
return pattern.test(item);
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
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).
332
A.partition = function(a, f, o) {
338
A.each(a, function(item, index) {
339
var set = f.call(o, item, index, a) ? results.matches : results.rejects;
347
Creates an array of arrays by pairing the corresponding elements of two arrays
348
together into a new array.
351
@param {Array} a Array to iterate over.
352
@param {Array} a2 Another array whose values will be paired with values of the
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.
358
A.zip = function(a, a2) {
360
A.each(a, function(item, index) {
361
results.push([item, a2[index]]);
367
}, '3.5.1' ,{requires:['yui-base']});