/usr/share/javascript/yui3/array-extras/array-extras.js is in libjs-yui3-full 3.5.1-1ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /*
YUI 3.5.1 (build 22)
Copyright 2012 Yahoo! Inc. All rights reserved.
Licensed under the BSD License.
http://yuilibrary.com/license/
*/
YUI.add('array-extras', function(Y) {
/**
Adds additional utility methods to the `Y.Array` class.
@module collection
@submodule array-extras
**/
var A = Y.Array,
L = Y.Lang,
ArrayProto = Array.prototype;
/**
Returns the index of the last item in the array that contains the specified
value, or `-1` if the value isn't found.
@method lastIndexOf
@param {Array} a Array to search in.
@param {Any} val Value to search for.
@param {Number} [fromIndex] Index at which to start searching backwards.
Defaults to the array's length - 1. If negative, it will be taken as an offset
from the end of the array. If the calculated index is less than 0, the array
will not be searched and `-1` will be returned.
@return {Number} Index of the item that contains the value, or `-1` if not
found.
@static
@for Array
**/
A.lastIndexOf = L._isNative(ArrayProto.lastIndexOf) ?
function(a, val, fromIndex) {
// An undefined fromIndex is still considered a value by some (all?)
// native implementations, so we can't pass it unless it's actually
// specified.
return fromIndex || fromIndex === 0 ? a.lastIndexOf(val, fromIndex) :
a.lastIndexOf(val);
} :
function(a, val, fromIndex) {
var len = a.length,
i = len - 1;
if (fromIndex || fromIndex === 0) {
i = Math.min(fromIndex < 0 ? len + fromIndex : fromIndex, len);
}
if (i > -1 && len > 0) {
for (; i > -1; --i) {
if (i in a && a[i] === val) {
return i;
}
}
}
return -1;
};
/**
Returns a copy of the specified array with duplicate items removed.
@method unique
@param {Array} a Array to dedupe.
@return {Array} Copy of the array with duplicate items removed.
@static
**/
A.unique = function(a, sort) {
// Note: the sort param is deprecated and intentionally undocumented since
// YUI 3.3.0. It never did what the API docs said it did (see the older
// comment below as well).
var i = 0,
len = a.length,
results = [],
item, j;
for (; i < len; ++i) {
item = a[i];
// This loop iterates over the results array in reverse order and stops
// if it finds an item that matches the current input array item (a
// dupe). If it makes it all the way through without finding a dupe, the
// current item is pushed onto the results array.
for (j = results.length; j > -1; --j) {
if (item === results[j]) {
break;
}
}
if (j === -1) {
results.push(item);
}
}
// Note: the sort option doesn't really belong here... I think it was added
// because there was a way to fast path the two operations together. That
// implementation was not working, so I replaced it with the following.
// Leaving it in so that the API doesn't get broken.
if (sort) {
if (L.isNumber(results[0])) {
results.sort(A.numericSort);
} else {
results.sort();
}
}
return results;
};
/**
Executes the supplied function on each item in the array. Returns a new array
containing the items for which the supplied function returned a truthy value.
@method filter
@param {Array} a Array to filter.
@param {Function} f Function to execute on each item.
@param {Object} [o] Optional context object.
@return {Array} Array of items for which the supplied function returned a
truthy value (empty if it never returned a truthy value).
@static
*/
A.filter = L._isNative(ArrayProto.filter) ?
function(a, f, o) {
return a.filter(f, o);
} :
function(a, f, o) {
var i = 0,
len = a.length,
results = [],
item;
for (; i < len; ++i) {
if (i in a) {
item = a[i];
if (f.call(o, item, i, a)) {
results.push(item);
}
}
}
return results;
};
/**
The inverse of `Array.filter()`. Executes the supplied function on each item.
Returns a new array containing the items for which the supplied function
returned `false`.
@method reject
@param {Array} a the array to iterate.
@param {Function} f the function to execute on each item.
@param {object} [o] Optional context object.
@return {Array} The items for which the supplied function returned `false`.
@static
*/
A.reject = function(a, f, o) {
return A.filter(a, function(item, i, a) {
return !f.call(o, item, i, a);
});
};
/**
Executes the supplied function on each item in the array. Iteration stops if the
supplied function does not return a truthy value.
@method every
@param {Array} a the array to iterate.
@param {Function} f the function to execute on each item.
@param {Object} [o] Optional context object.
@return {Boolean} `true` if every item in the array returns `true` from the
supplied function, `false` otherwise.
@static
*/
A.every = L._isNative(ArrayProto.every) ?
function(a, f, o) {
return a.every(f, o);
} :
function(a, f, o) {
for (var i = 0, l = a.length; i < l; ++i) {
if (i in a && !f.call(o, a[i], i, a)) {
return false;
}
}
return true;
};
/**
Executes the supplied function on each item in the array and returns a new array
containing all the values returned by the supplied function.
@example
// Convert an array of numbers into an array of strings.
Y.Array.map([1, 2, 3, 4], function (item) {
return '' + item;
});
// => ['1', '2', '3', '4']
@method map
@param {Array} a the array to iterate.
@param {Function} f the function to execute on each item.
@param {object} [o] Optional context object.
@return {Array} A new array containing the return value of the supplied function
for each item in the original array.
@static
*/
A.map = L._isNative(ArrayProto.map) ?
function(a, f, o) {
return a.map(f, o);
} :
function(a, f, o) {
var i = 0,
len = a.length,
results = a.concat();
for (; i < len; ++i) {
if (i in a) {
results[i] = f.call(o, a[i], i, a);
}
}
return results;
};
/**
Executes the supplied function on each item in the array, "folding" the array
into a single value.
@method reduce
@param {Array} a Array to iterate.
@param {Any} init Initial value to start with.
@param {Function} f Function to execute on each item. This function should
update and return the value of the computation. It will receive the following
arguments:
@param {Any} f.previousValue Value returned from the previous iteration,
or the initial value if this is the first iteration.
@param {Any} f.currentValue Value of the current item being iterated.
@param {Number} f.index Index of the current item.
@param {Array} f.array Array being iterated.
@param {Object} [o] Optional context object.
@return {Any} Final result from iteratively applying the given function to each
element in the array.
@static
*/
A.reduce = L._isNative(ArrayProto.reduce) ?
function(a, init, f, o) {
// ES5 Array.reduce doesn't support a thisObject, so we need to
// implement it manually.
return a.reduce(function(init, item, i, a) {
return f.call(o, init, item, i, a);
}, init);
} :
function(a, init, f, o) {
var i = 0,
len = a.length,
result = init;
for (; i < len; ++i) {
if (i in a) {
result = f.call(o, result, a[i], i, a);
}
}
return result;
};
/**
Executes the supplied function on each item in the array, searching for the
first item that matches the supplied function.
@method find
@param {Array} a the array to search.
@param {Function} f the function to execute on each item. Iteration is stopped
as soon as this function returns `true`.
@param {Object} [o] Optional context object.
@return {Object} the first item that the supplied function returns `true` for,
or `null` if it never returns `true`.
@static
*/
A.find = function(a, f, o) {
for (var i = 0, l = a.length; i < l; i++) {
if (i in a && f.call(o, a[i], i, a)) {
return a[i];
}
}
return null;
};
/**
Iterates over an array, returning a new array of all the elements that match the
supplied regular expression.
@method grep
@param {Array} a Array to iterate over.
@param {RegExp} pattern Regular expression to test against each item.
@return {Array} All the items in the array that produce a match against the
supplied regular expression. If no items match, an empty array is returned.
@static
*/
A.grep = function(a, pattern) {
return A.filter(a, function(item, index) {
return pattern.test(item);
});
};
/**
Partitions an array into two new arrays, one with the items for which the
supplied function returns `true`, and one with the items for which the function
returns `false`.
@method partition
@param {Array} a Array to iterate over.
@param {Function} f Function to execute for each item in the array. It will
receive the following arguments:
@param {Any} f.item Current item.
@param {Number} f.index Index of the current item.
@param {Array} f.array The array being iterated.
@param {Object} [o] Optional execution context.
@return {Object} An object with two properties: `matches` and `rejects`. Each is
an array containing the items that were selected or rejected by the test
function (or an empty array if none).
@static
*/
A.partition = function(a, f, o) {
var results = {
matches: [],
rejects: []
};
A.each(a, function(item, index) {
var set = f.call(o, item, index, a) ? results.matches : results.rejects;
set.push(item);
});
return results;
};
/**
Creates an array of arrays by pairing the corresponding elements of two arrays
together into a new array.
@method zip
@param {Array} a Array to iterate over.
@param {Array} a2 Another array whose values will be paired with values of the
first array.
@return {Array} An array of arrays formed by pairing each element of the first
array with an item in the second array having the corresponding index.
@static
*/
A.zip = function(a, a2) {
var results = [];
A.each(a, function(item, index) {
results.push([item, a2[index]]);
});
return results;
};
}, '3.5.1' ,{requires:['yui-base']});
|