API Docs for: 1.0.0
Show:

File: src/gallery-iterable-extras/js/array-iterator.js

  1. /**
  2. * @module gallery-iterable-extras
  3. */
  4.  
  5. /**********************************************************************
  6. * Iterator for an array. Useful for any class that manages an array and
  7. * wants to mix in `Y.Iterable`. Safe, but not stable, when the array is
  8. * modified during iteration.
  9. *
  10. * @class ArrayIterator
  11. * @method constructor
  12. * @param list {Array}
  13. */
  14.  
  15. function ArrayIterator(
  16. /* array */ list)
  17. {
  18. this._list = list;
  19. this.moveToBeginning();
  20. }
  21.  
  22. ArrayIterator.prototype =
  23. {
  24. /**
  25. * @method atBeginning
  26. * @return {Boolean} true if at the beginning
  27. */
  28. atBeginning: function()
  29. {
  30. return (this._next <= 0);
  31. },
  32.  
  33. /**
  34. * @method atEnd
  35. * @return {Boolean} true if at the end
  36. */
  37. atEnd: function()
  38. {
  39. return (this._next >= this._list.length);
  40. },
  41.  
  42. /**
  43. * Move to the beginning of the list.
  44. *
  45. * @method moveToBeginning
  46. */
  47. moveToBeginning: function()
  48. {
  49. this._next = 0;
  50. },
  51.  
  52. /**
  53. * Move to the end of the list.
  54. *
  55. * @method moveToEnd
  56. */
  57. moveToEnd: function()
  58. {
  59. this._next = this._list.length;
  60. },
  61.  
  62. /**
  63. * @method next
  64. * @return {Mixed} next value in the list or undefined if at the end
  65. */
  66. next: function()
  67. {
  68. if (this._next < this._list.length)
  69. {
  70. return this._list[ this._next++ ];
  71. }
  72. },
  73.  
  74. /**
  75. * @method prev
  76. * @return {Mixed} previous value in the list or undefined if at the beginning
  77. */
  78. prev: function()
  79. {
  80. if (this._next > 0)
  81. {
  82. return this._list[ --this._next ];
  83. }
  84. }
  85. };
  86.  
  87. Y.ArrayIterator = ArrayIterator;
  88.