Skip to content

fix(compat/parseInt): coerce input to string before parsing - #1771

Merged
raon0211 merged 1 commit into
toss:mainfrom
sarathfrancis90:fix-compat-parseint-coercion
Jun 17, 2026
Merged

raon0211 merged 1 commit into
toss:mainfrom
sarathfrancis90:fix-compat-parseint-coercion

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

While migrating some code from lodash I noticed compat/parseInt diverges from lodash on non-string inputs.

import { parseInt } from 'es-toolkit/compat';

parseInt(null);          // lodash: NaN   es-toolkit: 23  (parses "null" as base 25 via map iteratee, "null" otherwise)
parseInt(null, 25);      // lodash: NaN   es-toolkit: 23
parseInt(Symbol('x'));   // lodash: NaN   es-toolkit: throws TypeError

lodash's parseInt runs the input through its own toString before handing it to the native parseInt, so null/undefined become '' and symbols stringify instead of crashing. es-toolkit was passing the raw value straight to Number.parseInt, which relies on native coercion (null → "null", symbols throw).

The fix routes the input through the existing compat/util/toString, matching lodash. Added tests for the null/undefined and symbol cases (they fail on main, pass with the change). Full compat suite, tsc, and lint are green.

Mirror lodash by routing the input through compat's toString before
calling Number.parseInt, so null/undefined become '' (NaN) and symbols
return NaN instead of throwing a TypeError.
@vercel

vercel Bot commented Jun 13, 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 13, 2026 8:00am

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 2424182 into toss:main Jun 17, 2026
10 checks passed

This branch was successfully deployed

1 active deployment
Preview — f8731db1 Deployed Jun 13, 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