Issue #202 ... fallback method for sparse arrays.

This commit is contained in:
Jeremy Ashkenas
2011-05-11 10:27:52 -04:00
parent 5951d354af
commit 057da5bc82
2 changed files with 4 additions and 8 deletions

View File

@@ -26,10 +26,6 @@ $(document).ready(function() {
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
ok(answer, 'can reference the original collection from inside the iterator');
answers = [];
_.each({range : 1, speed : 2, length : 3}, function(v){ answers.push(v); });
ok(answers.join(', '), '1, 2, 3', 'can iterate over objects with numeric length properties');
answers = 0;
_.each(null, function(){ ++answers; });
equals(answers, 0, 'handles a null properly');
@@ -185,7 +181,7 @@ $(document).ready(function() {
people = _.sortBy(people, function(person){ return person.age; });
equals(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
});
test('collections: groupBy', function() {
var parity = _.groupBy([1, 2, 3, 4, 5, 6], function(num){ return num % 2; });
ok('0' in parity && '1' in parity, 'created a group for each value');

View File

@@ -73,7 +73,7 @@
obj.forEach(iterator, context);
} else if (_.isNumber(obj.length)) {
for (var i = 0, l = obj.length; i < l; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
for (var key in obj) {
@@ -250,7 +250,7 @@
return a < b ? -1 : a > b ? 1 : 0;
}), 'value');
};
// Groups the object's values by a criterion produced by an iterator
_.groupBy = function(obj, iterator) {
var result = {};
@@ -512,7 +512,7 @@
var funcs = slice.call(arguments);
return function() {
var args = slice.call(arguments);
for (var i=funcs.length-1; i >= 0; i--) {
for (var i = funcs.length - 1; i >= 0; i--) {
args = [funcs[i].apply(this, args)];
}
return args[0];