Issue #70 -- implementing each, find, all, any, etc. without the use of a try/catch/throw. Minor speedup + avoids destroying the stack trace.

This commit is contained in:
Jeremy Ashkenas
2010-12-01 11:08:34 -05:00
parent 6b8bb0cacd
commit 2d06e1d526
3 changed files with 23 additions and 41 deletions

View File

@@ -7,10 +7,6 @@ $(document).ready(function() {
equals(num, i + 1, 'each iterators provide value and iteration count');
});
var answer = null;
_.each([1, 2, 3], function(num){ if ((answer = num) == 2) _.breakLoop(); });
equals(answer, 2, 'the loop broke in the middle');
var answers = [];
_.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5});
equals(answers.join(', '), '5, 10, 15', 'context object property accessed');
@@ -65,7 +61,7 @@ $(document).ready(function() {
sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
equals(sum, 6, 'OO-style reduce');
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
equals(sum, 6, 'default initial value');
});
@@ -73,10 +69,10 @@ $(document).ready(function() {
test('collections: reduceRight', function() {
var list = _.reduceRight(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
equals(list, 'bazbarfoo', 'can perform right folds');
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; }, '');
equals(list, 'bazbarfoo', 'aliased as "foldr"');
var list = _.foldr(["foo", "bar", "baz"], function(memo, str){ return memo + str; });
equals(list, 'bazbarfoo', 'default initial value');
});

View File

@@ -15,15 +15,6 @@ $(document).ready(function() {
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() {
var ids = [], i = 0;
while(i++ < 100) ids.push(_.uniqueId());