Permalink
Browse files

Changes to forEach methods for a little more robustness.

  • Loading branch information...
1 parent a54f5bb commit eeb0bcc54bb8565b9289ce8ec2a174bee7174255 @jcoglan jcoglan committed Oct 1, 2008
Showing with 15 additions and 7 deletions.
  1. +4 −2 source/command.js
  2. +4 −2 source/enumerable.js
  3. +3 −1 source/linked_list.js
  4. +4 −2 source/set.js
View
@@ -29,8 +29,10 @@ JS.Command = new JS.Class({
},
forEach: function(block, context) {
- for (var i = 0, n = this._stack.length; i < n; i++)
- block.call(context || null, this._stack[i], i);
+ for (var i = 0, n = this._stack.length; i < n; i++) {
+ if (this._stack[i])
+ block.call(context || null, this._stack[i], i);
+ }
},
clear: function() {
View
@@ -1,8 +1,10 @@
JS.Enumerable = new JS.Module({
extend: {
forEach: function(block, context) {
- for (var i = 0, n = this.length; i < n; i++)
- block.call(context || null, this[i], i);
+ for (var i = 0, n = this.length; i < n; i++) {
+ if (this[i])
+ block.call(context || null, this[i], i);
+ }
},
isComparable: function(list) {
View
@@ -10,9 +10,11 @@ JS.LinkedList = new JS.Class({
},
forEach: function(block, context) {
- for (var node = this.first, next, i = 0, n = this.length; i < n; i++) {
+ var node = this.first, next, i, n;
+ for (i = 0, n = this.length; i < n; i++) {
next = node.next;
block.call(context || null, node, i);
+ if (node === this.last) break;
node = next;
}
},
View
@@ -3,8 +3,10 @@ JS.Set = new JS.Class({
forEach: function(list, block, context) {
if (!list) return;
if (list.forEach) return list.forEach(block, context);
- for (var i = 0, n = list.length; i < n; i++)
- block.call(context || null, list[i], i);
+ for (var i = 0, n = list.length; i < n; i++) {
+ if (list[i])
+ block.call(context || null, list[i], i);
+ }
},
areEqual: function(one, another) {

0 comments on commit eeb0bcc

Please sign in to comment.