Array.iterator 0.3

This plugin provides functionality for array iteration with php like methods (next, prev, current, key, end, reset, rewind). And some more functionality.



Details

Author
cainrus
Current version
0.3
GitHub
cainrus/mootools-array-iterator
Downloads
4686
Category
Native
Tags
Report
GitHub Issues

Releases


Dependencies

  • core/1.3: *

How to use

  • Add some lines in the head of your HTML document.

    <script type="text/javascript" src="mootools.js"></script> 
    <script type="text/javascript" src="Array.Iterator.js"></script>
    
  • Create array and iterator( you can create a few instances of it:

    var collection = [1,2,3,4,5];                 // array
    var iterator = collection.iterator(options);  // iterator
    var iterator2 = collection.iterator(options); // another iterator
    
  • Now you can easily iterate that array:

    console.log(iterator.next());    // 1
    console.log(iterator.end());     // 5
    console.log(iterator.prev());    // 4
    console.log(iterator.reset());   // 1
    console.log(iterator.slide(3));  // 4
    console.log(iterator.slide(-2)); // 2
    console.log(iterator.rewind());  // null
    
  • You can get array from iterator:

    console.log(iterator.ref());             // [1,2,3,4,5]
    console.log(iterator.ref()===collection) // true
    
  • You can get pointer from the iterator:

    console.log('value = ' + iterator.jump(2)); // value = 3
    console.log('key = ' + iterator.key());     // key = 2
    
  • Try to get penultimate value:

    iterator.reset();
    iterator.jump(-2); console.log(iterator.current()); // 4
    iterator.end(); console.log(iterator.prev());       // 4, same thing
    
  • Try to play with pit option:

    var collection = [1,2,3]
    var iterator = collection.iterator({pit : true});
    console.log(iterator.next());                 // 1
    console.log(iterator.next());                 // 2
    console.log(iterator.next());                 // 3
    // Null index at start(or end), use it to catch end of range.
    console.log(iterator.next());                 // null
    console.log(iterator.next());                 // 1
    console.log(iterator.next());                 // 2
    iterator.options.pit = false;
    // No more null index when iterating!
    console.log(iterator.prev());                 // 1
    console.log(iterator.prev());                 // 3
    
  • Try to play with limits option:

    iterator.options.limits = true;
    console.log(iterator.jump(0));                // 1
    console.log(iterator.next());                 // 2
    console.log(iterator.next());                 // 3
    console.log(iterator.next());                 // 3
    console.log(iterator.next());                 // 3
    
  • Try to designate boundaries of iterator with min & max options:

    iterator.options.min = 1;
    iterator.options.max = 2;
    console.log(iterator.reset());                // 2
    console.log(iterator.next());                 // 3
    console.log(iterator.next());                 // 3
    iterator.options.limits = false;
    console.log(iterator.next());                 // 2
    
  • Try to exclude some indexes from iteration with pass option:

    var collection = [1,2,3,4,5]
    var iterator = collection.iterator({pass:2});
    console.log(iterator.reset());                // 1
    console.log(iterator.next());                 // 2
    // You skipped one!
    console.log(iterator.next());                 // 4
    // Try to skip more.
    iterator.options.pass = [1,3];
    console.log(iterator.reset());                // 1
    console.log(iterator.next());                 // 3
    console.log(iterator.next());                 // 5
    
  • Try ability to chain method calls(jump, end, reset, rewind, next, prev, slide):

    var collection = [1,2,3,4,5]
    var iterator = collection.iterator({chains:true});
    iterator.reset().next().next().current();         // 3
    iterator.rewind().slide(-2).current();            // 4
    

Methods Of Iterator

  • reset - select first index and return it
  • end - select last index and return it
  • next - select next index and return it
  • prev - select previous index and return it
  • rewind - Select null index.
  • jump - Select index and return value. Can be positive or negative(will select index from end).
  • slide - Jump over passed offset and return value. Can be positive or negative. You can pass second argument "from", to move from described index.
  • key - return active key (number or null if not exist or not allowed)
  • ref - return original array.
  • range - return allowed range of array. Options min, max, pass are accounted.
  • valid - return active key or null if index is not valid or not allowed. You can validate with this method any index if it is valid.

Options Of Iterator

  • pit - Add null position in iteration cycle. By default null position available when key in not valid or on start.
  • limits - Restrict the movement from first to last allowed index or backwards.
  • min - Minimum allowed index, you can't select index less than it.
  • max - Maximum allowed index, you can't select index more than it.
  • pass - index or array of indexes to skip.
  • chains - Allow ability to chain method calls of movement

Example

/* create inventory for game */ 
var inventory = [$('Axe'), $('Potion'), $('MagicBook')];

// Iteration of inventory.
var activeSlot = inventory.iterator({
  pit : true
}); 

// Try to log all items in inventory
do {
    console.log(activeSlot.next());
} while (activeSlot.valid());

// reset to the null index when yo need it
Game.addEvent('inventoryCreate', function(){
    activeSlot.rewind();
});

// Try to select last added item of invenroty
Game.addEvent('inventoryOpen', function(){
  activeSlot.reset();
});

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus