Skip to content

Commit

Permalink
Remove reduce error throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Dec 4, 2014
1 parent 93cd25b commit 41555ff
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
8 changes: 2 additions & 6 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@
ok(_.reduce(null, _.noop, 138) === 138, 'handles a null (with initial value) properly');
equal(_.reduce([], _.noop, undefined), undefined, 'undefined can be passed as a special case');
equal(_.reduce([_], _.noop), _, 'collection of length one with no initial value returns the first item');

throws(function() { _.reduce([], _.noop); }, TypeError, 'throws an error for empty arrays with no initial value');
throws(function() {_.reduce(null, _.noop);}, TypeError, 'handles a null (without initial value) properly');
equal(_.reduce([], _.noop), undefined, 'returns undefined when collection is empty and no initial value');
});

test('foldl', function() {
Expand All @@ -145,9 +143,7 @@
equal(_.reduceRight([_], _.noop), _, 'collection of length one with no initial value returns the first item');

equal(_.reduceRight([], _.noop, undefined), undefined, 'undefined can be passed as a special case');

throws(function() { _.reduceRight([], _.noop); }, TypeError, 'throws an error for empty arrays with no initial value');
throws(function() {_.reduceRight(null, _.noop);}, TypeError, 'handles a null (without initial value) properly');
equal(_.reduceRight([], _.noop), undefined, 'returns undefined when collection is empty and no initial value');

// Assert that the correct arguments are being passed.

Expand Down
6 changes: 1 addition & 5 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@
return results;
};

var reduceError = 'Reduce of empty array with no initial value';

// **Reduce** builds up a single result from a list of values, aka `inject`,
// or `foldl`.
_.reduce = _.foldl = _.inject = function(obj, iteratee, memo, context) {
Expand All @@ -171,7 +169,6 @@
length = (keys || obj).length,
index = 0, currentKey;
if (arguments.length < 3) {
if (!length) throw new TypeError(reduceError);
memo = obj[keys ? keys[index++] : index++];
}
for (; index < length; index++) {
Expand All @@ -189,10 +186,9 @@
index = (keys || obj).length,
currentKey;
if (arguments.length < 3) {
if (!index) throw new TypeError(reduceError);
memo = obj[keys ? keys[--index] : --index];
}
while (index--) {
while (index-- > 0) {
currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
Expand Down

0 comments on commit 41555ff

Please sign in to comment.