Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reduce error throwing #1961

Merged
merged 1 commit into from
Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update these tests to prove they don't throw

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, updated.


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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to never use the result of a postfix increment/decrement (or use them at all). Let's do while(--index >= 0).

currentKey = keys ? keys[index] : index;
memo = iteratee(memo, obj[currentKey], currentKey, obj);
}
Expand Down