Skip to content

Commit dfa1c21

Browse files
Merge pull request prototypejs#59 from jochenberger/fix-objectrange-each
Ensure index is passed to the iterator in `ObjectRange#each`.
2 parents ecacc02 + 2729bcb commit dfa1c21

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/prototype/lang/range.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ var ObjectRange = Class.create(Enumerable, (function() {
108108
}
109109

110110
function _each(iterator, context) {
111-
var value = this.start;
112-
while (this.include(value)) {
113-
iterator.call(context, value);
111+
var value = this.start, i;
112+
for (i = 0; this.include(value); i++) {
113+
iterator.call(context, value, i);
114114
value = value.succ();
115115
}
116116
}

test/unit/range_test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ new Test.Unit.Runner({
2727
});
2828

2929
this.assertEnumEqual([0, 1, 2, 3], results);
30+
31+
results = [];
32+
$R(2, 4, true).each(function(value, index) {
33+
results.push(index);
34+
});
35+
this.assertEnumEqual([0, 1], results);
36+
3037
},
3138

3239
testAny: function() {

0 commit comments

Comments
 (0)