collection-debug.js revision 6af8358a6bd80bcc795828ce62c1ecd22daab6a1
279N/A * Collection utilities beyond what is provided in the YUI core 279N/A * @submodule array-extras 279N/A * Adds the following array utilities to the YUI instance 279N/A * (Y.Array). This is in addition to the methods provided 279N/A * @class YUI~array~extras 279N/A * Returns the index of the last item in the array that contains the specified 279N/A * value, or -1 if the value isn't found. 279N/A * @method Array.lastIndexOf 279N/A * @param {Array} a Array to search in. 279N/A * @param {any} val Value to search for. 279N/A * @param {Number} fromIndex (optional) Index at which to start searching 279N/A * backwards. Defaults to the array's length - 1. If negative, it will be 279N/A * taken as an offset from the end of the array. If the calculated index is 840N/A * less than 0, the array will not be searched and -1 will be returned. 447N/A * @return {Number} Index of the item that contains the value, or -1 if not 844N/A // An undefined fromIndex is still considered a value by some (all?) 279N/A // native implementations, so we can't pass it unless it's actually 279N/A * Returns a copy of the specified array with duplicate items removed. 279N/A * @param {Array} a Array to dedupe. 279N/A * @return {Array} Copy of the array with duplicate items removed. 447N/A // Note: the sort param is deprecated and intentionally undocumented since 279N/A // YUI 3.3.0. It never did what the API docs said it did (see the older 279N/A // comment below as well). // 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. // 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. Y.
log(
'The sort parameter is deprecated and will be removed in a future version of YUI.',
'warn',
'deprecated');
* 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. * @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). * The inverse of filter. Executes the supplied function on each item. * Returns a new array containing the items that the supplied * function returned *false* for. * @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 on which the supplied function A.
reject =
function(a, f, o) {
* Executes the supplied function on each item in the array. * Iteration stops if the supplied function does not return * @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. for (
var i =
0, l = a.
length; i < l; ++i) {
if (i
in a && !f.
call(o, a[i], i, a)) {
* Executes the supplied function on each item in the array. * @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 * Executes the supplied function on each item in the array. * Reduce "folds" the array into a single value. The callback * function receives four arguments: * the value from the previous callback call (or the initial value), * the value of the current element, the current index, and * the array over which iteration is occurring. * @param {Array} a the array to iterate. * @param {any} init The initial value to start from. * @param {Function} f the function to execute on each item. It * is responsible for returning the updated value of the * @param {object} o Optional context object. * @return {any} A value that results from iteratively applying the * supplied function to each element in the array. function(a,
init, f, o) {
// ES5 Array.reduce doesn't support a thisObject, so we need to function(a,
init, f, o) {
* Executes the supplied function on each item in the array, * searching for the first item that matches the supplied * @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. 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)) {
* Iterates over an array, returning a new array of all the elements * that match the supplied regular expression * @param {Array} a a collection to iterate over. * @param {RegExp} pattern The regular expression to test against * @return {Array} All the items in the collection that * produce a match against the supplied regular expression. * If no items match, an empty array is returned. * Partitions an array into two new arrays, one with the items * that match the supplied function, and one with the items that * @method Array.partition * @param {Array} a a collection to iterate over. * @param {Function} f a function that will receive each item * in the collection and its index. * @param {object} o Optional execution context of f. * @return {object} An object with two members, 'matches' and 'rejects', * that are arrays containing the items that were selected or * rejected by the test function (or an empty array). * Creates an array of arrays by pairing the corresponding * elements of two arrays together into a new array. * @param {Array} a a collection to iterate over. * @param {Array} a2 another collection whose members will be * paired with members of the first parameter. * @return {array} An array of arrays formed by pairing each element * of the first collection with an item in the second collection * having the corresponding index. A.
zip =
function(a,
a2) {
YUI.
add(
'arraylist',
function(Y) {
* Collection utilities beyond what is provided in the YUI core * Generic ArrayList class for managing lists of items and iterating operations * over them. The targeted use for this class is for augmentation onto a * class that is responsible for managing multiple instances of another class * (e.g. NodeList for Nodes). The recommended use is to augment your class with * ArrayList, then use ArrayList.addMethod to mirror the API of the constituent * items on the list's API. * The default implementation creates immutable lists, but mutability can be * provided via the arraylist-add submodule or by implementing mutation methods * directly on the augmented class's prototype. * @param items { Array } array of items this list will be responsible for // ||= to support lazy initialization from augment * Get an item by index from the list. Override this method if managing a * list of objects that have a different public representation (e.g. Node * instances vs DOM nodes). The iteration methods that accept a user * function will use this method for access list items for operation. * @param i { Integer } index to fetch * @return { mixed } the item at the requested index * <p>Execute a function on each item of the list, optionally providing a * custom execution context. Default context is the item.</p> * <p>The callback signature is <code>callback( item, index )</code>.</p> * @param fn { Function } the function to execute * @param context { mixed } optional override 'this' in the function * @return { ArrayList } this instance * <p>Execute a function on each item of the list, optionally providing a * custom execution context. Default context is the item.</p> * <p>The callback signature is <code>callback( item, index )</code>.</p> * <p>Unlike <code>each</code>, if the callback returns true, the * iteratation will stop.</p> * @param fn { Function } the function to execute * @param context { mixed } optional override 'this' in the function * @return { Boolean } True if the function returned true on an item * Finds the first index of the needle in the managed array of items. * @param needle { mixed } The item to search for * @return { Integer } Array index if found. Otherwise -1 * How many items are in this list? * @return { Integer } Number of items in the list * Is this instance managing any items? * @return { Boolean } true if 1 or more items are being managed * Provides an array-like representation for JSON.stringify. * @return { Array } an array representation of the ArrayList // Default implementation does not distinguish between public and private * Protected method for optimizations that may be appropriate for API * mirroring. Similar in functionality to <code>item</code>, but is used by * methods added with <code>ArrayList.addMethod()</code>. * @param i { Integer } Index of item to fetch * @return { mixed } The item appropriate for pass through API methods * <p>Adds a pass through method to dest (typically the prototype of a list * class) that calls the named method on each item in the list with * whatever parameters are passed in. Allows for API indirection via list * <p>Accepts a single string name or an array of string names.</p> * <pre><code>list.each( function ( item ) { * item.methodName( 1, 2, 3 ); * list.methodName( 1, 2, 3 );</code></pre> * <p>Additionally, the pass through methods use the item retrieved by the * <code>_item</code> method in case there is any special behavior that is * appropriate for API mirroring.</p> * <p>If the iterated method returns a value, the return value from the * added method will be an array of values with each value being at the * corresponding index for that item. If the iterated method does not * return a value, the added method will be chainable. * @param dest {Object} Object or prototype to receive the iterator method * @param name {String|String[]} Name of method of methods to create YUI.
add(
'arraylist-add',
function(Y) {
* Collection utilities beyond what is provided in the YUI core * @submodule arraylist-add * Adds methods add and remove to Y.ArrayList * Add a single item to the ArrayList. Does not prevent duplicates. * @param { mixed } item Item presumably of the same type as others in the * @param {Number} index (Optional.) Number representing the position at * which the item should be inserted. * @return {ArrayList} the instance. * Removes first or all occurrences of an item to the ArrayList. If a * comparator is not provided, uses itemsAreEqual method to determine * @param { mixed } needle Item to find and remove from the list. * @param { Boolean } all If true, remove all occurrences. * @param { Function } comparator optional a/b function to test equivalence. * @return {ArrayList} the instance. * Default comparator for items stored in this list. Used by remove(). * @param { mixed } a item to test equivalence with. * @param { mixed } b other item to test equivalance. * @return { Boolean } true if items are deemed equivalent. },
'@VERSION@' ,{
requires:[
'arraylist']});
YUI.
add(
'arraylist-filter',
function(Y) {
* Collection utilities beyond what is provided in the YUI core * @submodule arraylist-filter * Adds filter method to ArrayList prototype * @class ArrayList~filter * <p>Create a new ArrayList (or augmenting class instance) from a subset * of items as determined by the boolean function passed as the * argument. The original ArrayList is unchanged.</p> * <p>The validator signature is <code>validator( item )</code>.</p> * @param { Function } validator Boolean function to determine in or out. * @return { ArrayList } New instance based on who passed the validator. },
'@VERSION@' ,{
requires:[
'arraylist']});
YUI.
add(
'array-invoke',
function(Y) {
* Collection utilities beyond what is provided in the YUI core * @submodule array-invoke * Adds the <code>Y.Array.invoke( items, methodName )</code> utility method. * @class YUI~array~invoke * <p>Execute a named method on an array of objects. Items in the list that do * not have a function by that name will be skipped. For example, * <code>Y.Array.invoke( arrayOfDrags, 'plug', Y.Plugin.DDProxy );</code></p> * <p>The return values from each call are returned in an array.</p> * @param { Array } items Array of objects supporting the named method. * @param { String } name the name of the method to execute on each item. * @param { mixed } args* Any number of additional args are passed as * parameters to the execution of the named method. * @return { Array } All return values, indexed according to item index. YUI.
add(
'collection',
function(Y){},
'@VERSION@' ,{
use:[
'array-extras',
'arraylist',
'arraylist-add',
'arraylist-filter',
'array-invoke']});