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

sumBy returns boolean instead of number #4878

Closed
marian-galik opened this issue Jul 28, 2020 · 6 comments
Closed

sumBy returns boolean instead of number #4878

marian-galik opened this issue Jul 28, 2020 · 6 comments
Labels
change issue bankruptcy Closing the issue/PR to start fresh

Comments

@marian-galik
Copy link

_.sumBy([true], (i) => i === true)

Actual returns:

true

Expected:

1

@maximillianfx
Copy link

Hi! The behavior is really wrong, I made a simple solution here, can I take this like a bug-fix?

@LarryBattle
Copy link

Easiest solution is to always make sure your iteratee function always return a number.

Example: _.sumBy([true], (i) => +(i === true)) // returns 1

I created a PR for the fix here #4883

Anyone know how to run all the tests for lodash? I didn't have time to figure it out.

npm install && npm test

fails because it can't create node lib/main/build-dist.js.... :-(

@dopecodez
Copy link

Same here, I was looking at #4865 , and could not figure out how to run all the tests.
Only test script that worked was test:wip and that only seems to run a certain number of tests. In my case, the at.test.js was running but not the pullAt.js test. It'd be helpful for maintenance if you guys could clear up the tests.

@bnjmnt4n
Copy link
Contributor

I believe this behaviour is working as intended for now. Although _.sum and _.sumBy are Math methods, they have been able to perform _.sum(["a", "b"]). I believe that summing should also occur with strictly defined and consistent types, and passing booleans or null and undefined to _.sum is always going to lead to unexpected issues.

@tshma-lex
Copy link

I am not sure how is this issue is closed (as well as the closed "duplicates" for null), as I can sum strings (it will concatenate). It might be expected behaviour, but if so - the documentation should change to reflect the return value is not always a number (it can also still return a null or undefined, if all the values are such)

@tshma-lex
Copy link

Where is the "fresh start" for this issue? I see many like issues (for instance, the sum() return 0 for null, and [null, null] yet return null for [null]), but all are closed, or marked as duplicate to other closed one. It seems no solution was expected. We use the overhead of lodash in our codebase, as it is stable - on opposition to many quirks of pure javascript. So, it's not an excuse that js has unexpected behavior to leave it like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
change issue bankruptcy Closing the issue/PR to start fresh
Development

No branches or pull requests

7 participants