8000 docs: Document current string concatenation behavior in sum methods (#5818) by flavioespinoza · Pull Request #6097 · lodash/lodash · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@flavioespinoza
Copy link
@flavioespinoza flavioespinoza commented Jan 27, 2026

Context

This PR is a direct response to @ljharb's feedback on #6095:

"In the meantime, it would be better to have a PR that adds tests for the current behavior, and updates the docs to match."

Done. ✅


Summary

Documents the current behavior where sum, sumBy, mean, and meanBy perform string concatenation when given non-numeric values, as reported in #5818.

Changes

JSDoc Updates

  • Added Note sections explaining that non-numeric values cause string concatenation
  • Updated @returns from {number} to {number|string} to accurately reflect possible return types
  • Added examples showing the non-numeric behavior
  • Referenced Method sumBy returns string  #5818 for context

New Tests

QUnit.test('with non-numeric values performs string concatenation (current behavior, see #5818)', function(assert) {
  assert.strictEqual(func(['a', 'b']), 'ab', 'strings are concatenated');
  assert.strictEqual(func(['1', '2', '3']), '123', 'numeric strings are concatenated');
  assert.strictEqual(func([1, '2']), '12', 'number + string results in string');
  assert.strictEqual(func([1, 'f', null]), '1fnull', 'mixed types produce unexpected results');
});

Example Documentation Added

// Non-numeric values result in string concatenation (not recommended)
_.sum(['1', '2']);
// => '12'

_.sumBy([{ 'a': '1' }, { 'a': '2' }], 'a');
// => '12'

Test Plan

  • npm run build passes
  • npm test passes (6808 + 327 = 7135 tests)
  • No breaking changes - behavior is unchanged, only documentation added

Related

…odash#5818)

Update JSDoc and add tests to document the current behavior where non-numeric
values in sum/sumBy/mean/meanBy result in string concatenation rather than
numeric addition.

This documents the existing behavior as reported in lodash#5818. The behavior may
change in a future major version (see lodash#6095 for the proposed fix).

Changes:
- Update JSDoc for sum, sumBy, mean, meanBy to note string concatenation behavior
- Update @returns to reflect that string may be returned
- Add examples showing the non-numeric behavior
- Add tests documenting string concatenation with various non-numeric inputs
- Reference lodash#5818 in documentation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Include updated documentation for sum, sumBy, mean, and meanBy
methods describing current string concatenation behavior.
Copy link
Member
@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

I'm not sure if the dist file should be updated in individual PRs; typically that sort of thing is only updated for releases. Otherwise, LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0