Skip to content

Commit abbdb5d

Browse files
Merge pull request prototypejs#60 from jochenberger/fix-hash-each
Ensure index is passed to the iterator in `Hash#each`.
2 parents dfa1c21 + 8a0cd1e commit abbdb5d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/prototype/lang/hash.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ var Hash = Class.create(Enumerable, (function() {
9494

9595
// Our _internal_ each
9696
function _each(iterator, context) {
97+
var i = 0;
9798
for (var key in this._object) {
9899
var value = this._object[key], pair = [key, value];
99100
pair.key = key;
100101
pair.value = value;
101-
iterator.call(context, pair);
102+
iterator.call(context, pair, i);
103+
i++;
102104
}
103105
}
104106

test/unit/hash_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ new Test.Unit.Runner({
182182
var foo = new FooMaker('bar');
183183
this.assertEqual("key=bar", new Hash(foo).toQueryString());
184184
this.assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString());
185+
},
186+
187+
testIterationWithEach: function() {
188+
var h = $H({a:1, b:2});
189+
var result = []
190+
h.each(function(kv, i){
191+
result.push(i);
192+
});
193+
this.assertEnumEqual([0,1], result);
185194
}
186195

187196
});

0 commit comments

Comments
 (0)