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

@@ -202,8 +202,8 @@ _(lyrics).chain()
<b>Utility</b> <b>Utility</b>
<br /> <br />
<span class="methods"><a href="#noConflict">noConflict</a>, <span class="methods"><a href="#noConflict">noConflict</a>,
<a href="#identity">identity</a>, <a href="#uniqueId">uniqueId</a>, <a href="#identity">identity</a>, <a href="#breakLoop">breakLoop</a></span>,
<a href="#template">template</a></span> <a href="#uniqueId">uniqueId</a>, <a href="#template">template</a></span>
</p> </p>
<p> <p>
@@ -789,7 +789,21 @@ var underscore = _.noConflict();</pre>
<pre> <pre>
var moe = {name : 'moe'}; var moe = {name : 'moe'};
moe === _.identity(moe); moe === _.identity(moe);
=> true</pre> =&gt; true</pre>
<p id="breakLoop">
<b class="header">breakLoop</b><code>_.breakLoop()</code>
<br />
Breaks out of the current loop iteration. Similar to the <tt>break</tt>
keyword in regular "for" loop, but works within an iterator function.
</p>
<pre>
var result = null;
_.each([1, 2, 3], function(num) {
if ((result = num) == 2) _.breakLoop();
});
result;
=&gt; 2</pre>
<p id="uniqueId"> <p id="uniqueId">
<b class="header">uniqueId</b><code>_.uniqueId([prefix])</code> <b class="header">uniqueId</b><code>_.uniqueId([prefix])</code>

View File

@@ -15,6 +15,15 @@ $(document).ready(function() {
equals(_.identity(moe), moe, 'moe is the same as his identity'); equals(_.identity(moe), moe, 'moe is the same as his identity');
}); });
test('utility: breakLoop', function() {
var result = null;
_([1,2,3,4,5,6]).each(function(num) {
result = num;
if (num == 3) _.breakLoop();
});
equals(result, 3, 'broke out of a loop');
});
test("utility: uniqueId", function() { test("utility: uniqueId", function() {
var ids = [], i = 0; var ids = [], i = 0;
while(i++ < 100) ids.push(_.uniqueId()); while(i++ < 100) ids.push(_.uniqueId());
@@ -22,7 +31,7 @@ $(document).ready(function() {
}); });
test("utility: functions", function() { test("utility: functions", function() {
var expected = ["all", "any", "bind", "bindAll", "clone", "compact", "compose", var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose",
"defer", "delay", "detect", "each", "every", "extend", "filter", "first", "defer", "delay", "detect", "each", "every", "extend", "filter", "first",
"flatten", "foldl", "foldr", "forEach", "functions", "identity", "include", "flatten", "foldl", "foldr", "forEach", "functions", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual", "indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",

View File

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