Skip to content

Commit

Permalink
Issue #202 ... fallback method for sparse arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed May 11, 2011
1 parent 5951d35 commit 057da5b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
6 changes: 1 addition & 5 deletions test/collections.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions underscore.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 057da5b

Please sign in to comment.