adding breakLoop

This commit is contained in:
Jeremy Ashkenas
2009-11-08 12:07:10 -05:00
parent cda4612a00
commit 5eec4e5d22
3 changed files with 146 additions and 118 deletions

View File

@@ -90,7 +90,7 @@
_.each(obj, function(value, index, list) {
if (iterator.call(context, value, index, list)) {
result = value;
throw '__break__';
_.breakLoop();
}
});
return result;
@@ -123,7 +123,7 @@
if (obj.every) return obj.every(iterator, context);
var result = true;
_.each(obj, function(value, index, list) {
if (!(result = result && iterator.call(context, value, index, list))) throw '__break__';
if (!(result = result && iterator.call(context, value, index, list))) _.breakLoop();
});
return result;
};
@@ -135,7 +135,7 @@
if (obj.some) return obj.some(iterator, context);
var result = false;
_.each(obj, function(value, index, list) {
if (result = iterator.call(context, value, index, list)) throw '__break__';
if (result = iterator.call(context, value, index, list)) _.breakLoop();
});
return result;
};
@@ -146,7 +146,7 @@
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
var found = false;
_.each(obj, function(value) {
if (found = value === target) throw '__break__';
if (found = value === target) _.breakLoop();
});
return found;
};
@@ -446,6 +446,11 @@
return value;
};
// Break out of the middle of an iteration.
_.breakLoop = function() {
throw "__break__";
};
// Generate a unique integer id (unique within the entire client session).
// Useful for temporary DOM ids.
var idCounter = 0;