Skip to content

fix(compat/size): count array-like objects by their length - #1766

Merged
raon0211 merged 1 commit into
toss:mainfrom
sarathfrancis90:fix-compat-size-v2
Jun 12, 2026
Merged

raon0211 merged 1 commit into
toss:mainfrom
sarathfrancis90:fix-compat-size-v2

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

Summary

compat/size returns the number of own enumerable keys for array-like objects, so it diverges from lodash for values that have a valid length but aren't real arrays:

import { size } from 'es-toolkit/compat';
import _ from 'lodash';

size({ length: 3 });             // 1   (expected 3)
_.size({ length: 3 });           // 3

size({ 0: 'a', 1: 'b', length: 2 }); // 3   (expected 2)
_.size({ 0: 'a', 1: 'b', length: 2 }); // 2

lodash's size checks isArrayLike(collection) first and returns collection.length for array-like values, only falling back to the own-key count for plain objects. es-toolkit skipped that check and went straight to Object.keys(target).length.

Fix

Route array-like values through isArrayLike and return their length, matching lodash. Objects whose length is not a valid array length (negative, fractional, non-number, or above MAX_SAFE_INTEGER) are still not treated as array-like and fall back to the own-key count, exactly as before — the existing size tests covering those cases keep passing.

Arrays, strings, and arguments objects are unaffected (they were already array-like and produced the same result). Maps and Sets are not array-like, so they still return .size.

Tests

Added cases asserting array-like objects are counted by length. The new tests fail on main and pass with the fix. The full src/compat suite (3093 tests), tsc --noEmit, and lint all pass.

`size` returned the number of own enumerable keys for array-like
objects, so `size({ length: 3 })` was 1 instead of 3. lodash treats
array-like values by their `length`, so this diverged.

Route array-like values through `isArrayLike` and return `length`,
matching lodash. Objects whose `length` is not a valid array length
(negative, fractional, non-number, or above MAX_SAFE_INTEGER) still
fall back to the own-key count, as before.
@vercel

vercel Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
es-toolkit Ready Ready Preview, Comment Jun 12, 2026 3:50am

Request Review

@raon0211 raon0211 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@raon0211
raon0211 merged commit 2c95fc3 into toss:main Jun 12, 2026
10 checks passed

This branch was successfully deployed

1 active deployment
Preview — 34ce2741 Deployed Jun 12, 2026 by vercel[bot]
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.

2 participants