Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 19th, 2012  |  syntax: None  |  size: 2.85 KB  |  hits: 3  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*global YUI*/
  2. /**
  3.  * Adds common array-extras methods to a class that also mixes ArrayList
  4.  *
  5.  * Example usage:
  6.  *
  7.  *     var MyClass = Y.Base.create("myclass", Y.Base, [Y.ArrayList, Y.ArrayListExtras]),
  8.  *       mc = new MyClass();
  9.  *
  10.  *     mc.add("hi");
  11.  *     mc.add("by");
  12.  *     mc.map(function (item) { return item.toUpperCase() }); // ["HI", "BYE"]
  13.  *    
  14.  * @module arraylist-extras
  15.  * @requires array-extras
  16.  */
  17. YUI.add("arraylist-extras", function (Y) {
  18.   "use strict";
  19.   var YArray = Y.Array,
  20.     ArrayListExtras = function () {};
  21.  
  22.   /**
  23.    * Adds common array methods to a class that also mixes ArrayList
  24.    *
  25.    * @class ArrayListExtras
  26.    * @constructor
  27.    */
  28.   ArrayListExtras.prototype = {
  29.     /**
  30.      * Executes the supplied function on each item in the arraylist, searching for the
  31.      * first item that matches the supplied function.
  32.      *
  33.      * @method find
  34.      * @param {Function} f the function to execute on each item. Iteration is stopped
  35.      *   as soon as this function returns `true`.
  36.      * @param {Object} [o] Optional context object.
  37.      * @return {Object} the first item that the supplied function returns `true` for,
  38.      *   or `null` if it never returns `true`.
  39.      */
  40.     find: function (f, o) {
  41.       return YArray.find(this._items, f, o || this);
  42.     },
  43.     /**
  44.      * Executes the supplied function on each item in the arraylist and returns a new array
  45.      * containing all the values returned by the supplied function.
  46.      *
  47.      * @method map
  48.      * @param {Function} f the function to execute on each item.
  49.      * @param {object} [o] Optional context object.
  50.      * @return {Array} A new array containing the return value of the supplied function
  51.      *   for each item in the original array.
  52.      */
  53.     map: function (f, o) {
  54.       return YArray.map(this._items, f, o || this);
  55.     },
  56.     /**
  57.      * Executes the supplied function on each item in the arraylist, "folding" the arraylist
  58.      * into a single value.
  59.      *
  60.      * @method reduce
  61.      * @param {Any} init Initial value to start with.
  62.      * @param {Function} f Function to execute on each item. This function should
  63.      *   update and return the value of the computation. It will receive the following
  64.      *   arguments:
  65.      *     @param {Any} f.previousValue Value returned from the previous iteration,
  66.      *       or the initial value if this is the first iteration.
  67.      *     @param {Any} f.currentValue Value of the current item being iterated.
  68.      *     @param {Number} f.index Index of the current item.
  69.      *     @param {Array} f.array Array being iterated.
  70.      * @param {Object} [o] Optional context object.
  71.      * @return {Any} Final result from iteratively applying the given function to each
  72.      *   element in the array.
  73.      */
  74.     reduce: function (i, f, o) {
  75.       return YArray.reduce(this._items, i, f, o || this);
  76.     }
  77.   };
  78.  
  79. }, "3.4.1", {
  80.   requires: ["array-extras"]
  81. });