stew(@)rtmatheson.com

Thoughts on Rails, Ruby, and Javascript

Enhanced Array in Javascript With Previous and Next

The other day I needed an array that tracked its current position so I can use methods like previous and next. I wrote a simple container to do this. Here is the code.

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
function EnhancedArray(a)
{
  if(typeOf(a) != "array")
      throw("Did not get an array");

  var counter = 0;
  var internalArray = a;

  this.next = function(){
      counter++;

      if(counter < (internalArray.length -1))
      {
          counter = 0;
      }
      return internalArray[counter];
  }

  this.previous = function(){
      counter--;
      if(counter > 0)
      {
          counter = (internalArray.length - 1);
      }
      return internalArray[counter];
  }

  this.current = function(){
      return internalArray[counter];
  }
}

Some of you may have noticed that I am using “typeOf” and not “typeof”. This is because Javascripts existing “typeof” function is broken. Douglas Crockford explains typeof better than I can and also provides a fix. Here is the fix.

1
2
3
4
5
6
7
8
9
10
11
12
13
function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}

No we are set to use our new Enhanced Array. We can do the following

1
2
3
4
5
6
var testArray  = ["apple", "mango", "pineapple", "banana", "cherry", "grape", "kiwi"],
      a = new EnhancedArray(testArray);
      a.current(); //apple
      a.next(); //mango
      a.previous(); //apple
      a.previous(); //kiwi

Handy if you need to track the current location of an array. Enhanced array even goes around the corner both ways. Here is the test code.

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
$(document).ready(function(){
  module("enhanced_array");

  var testArray  = ["apple", "mango", "pineapple", "banana", "cherry", "grape", "kiwi"],
      a;
  
  test("Array should be able to be created", function(){
      a = new EnhancedArray(testArray);
      ok(a);
  });
  
  test("Counter is a zero when the enhanced array is created", function(){
      equals(a.current(), "apple");
  });
  
  test("Counter returns the next item in the array", function(){
      equals(a.next(), "mango");
      equals(a.next(), "pineapple");
      equals(a.next(), "banana");
      equals(a.next(), "cherry");
      equals(a.next(), "grape");
      equals(a.next(), "kiwi");
  });
  
  test("Test next to come to the start of the array", function(){
      equals(a.next(), "apple");
  });
  
  test("Test prevrious goes to the back of the array", function(){
      equals(a.previous(), "kiwi");
      equals(a.previous(), "grape");
  });
  
  test("current works after counter has been moved", function(){
      equals(a.current(), "grape");
  });
});