Dave Perrett

Javascript Hash Class

javascript, programming, web

After recently switching from prototype to the incredible jQuery, I had trouble finding an altermative for prototype’s Hash class. I ended up writing one based on MojaveLinux ‘s Javascript Hash Table article. It implements the length, first, keys, invoke, each, remove, item, itemByIndex, set and hasKey functions. You’ll need some extra array functions to use this class.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function Hash(){
    this.initialize(arguments);
}

Hash.prototype = {

    /*
     * Constructor
     */
    initialize: function() {
    this._length = 0;
    this._items = new Array();
    this._keys = new Array();
    for (var i = 0; i < arguments.length; i += 2) {
      if (typeof(arguments[i + 1]) != 'undefined') {
        this._items[arguments[i]] = arguments[i + 1];
        this._keys.push(arguments[i]);
        this._length++;
      }
    }
  },

    /*
     * Return the length of the hash
     */
  length: function() {
    return this._keys.length;
  },

    /*
     * Return the first item in the hash
     */
  first: function() {
    var result;
    if (this._keys.length > 0) {
      result = this._items[this._keys[0]];
    }
    return result;
  },

    /*
     * Return an array of keys for this hash
     */
  keys: function() {
    return this._keys;
  },

    /*
     * Run a function on each element in the hash
     */
  invoke: function(fn) {
    var args = [];
    for (var i = 1; i < arguments.length; i++) {
      args.push(arguments[i]);
    }

    for (var i = 0; i < this._keys.length; i++) {
      fn.apply(this._items[this._keys[i]], args);
    }
  },

    /*
     * Run a function on each element in the hash
     */
  each: function(fn) {
    var args = [];
    for (var i = 1; i < arguments.length; i++) {
      args.push(arguments[i]);
    }

    for (var i = 0; i < this._keys.length; i++) {
      fn(this._items[this._keys[i]], args);
    }
  },

    /*
     * Remove an item from the hash
     */
  remove: function(key) {
    var tmp_value;
    if (typeof(this._items[key]) != 'undefined') {
      this._length--;
      var tmp_value = this._items[key];
      delete this._items[key];
      this._keys.remove(key);
    }
    return tmp_value;
  },

    /*
     * Get an item from the hash
     */
  item: function(key) {
    return this._items[key];
  },

    /*
     * Get an item from the hash
     */
  itemByIndex: function(index) {
    return this._items[this._keys[index]];
  },

    /*
     * Set an item in the hash
     */
  set: function(in_key, in_value) {
    if (typeof(in_value) != 'undefined') {
      if (typeof(this._items[in_key]) == 'undefined') {
        this._length++;
      }
      this._items[in_key] = in_value;
      if (! this._keys.inArray(in_key)) {
        this._keys.push(in_key);
      }
    }
    return in_value;
  },

    /*
     * Return true if the item is in the hash
     */
  hasKey: function(key) {
    return typeof(this._items[key]) != 'undefined';
  }
}