Skip to content

fix(csv-parse): cast hex, octal and binary literals to their value - #524

Merged
wdavidw merged 1 commit into
adaltas:masterfrom
fhgffy:parse-cast-hex
Oct 9, 2026
Merged

wdavidw merged 1 commit into
adaltas:masterfrom
fhgffy:parse-cast-hex

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

With cast: true, hex, octal and binary literals are silently turned into 0:

parse("0x10,0o17,0b11", { cast: true });
// [[0, 0, 0]]

__isFloat() (value - parseFloat(value) + 1 >= 0) returns true for these strings because the subtraction coerces with Number(). The value is then read with parseFloat(), which stops at the x/o/b and returns 0.

This uses Number(field) for the conversion so it agrees with the check. For everything __isFloat() already accepted, Number() and parseFloat() give the same result. The only exception is these prefixed literals, which now come out as [16, 15, 3] instead of 0.

I added a case to test/option.cast.ts. It fails before the change, and npm test in packages/csv-parse passes (616 passing).

__isFloat() accepts anything Number() can convert, including 0x10,
0o17 and 0b11, but the value was then read with parseFloat(), which
stops at the prefix and returns 0.
@wdavidw
wdavidw merged commit 0adbcaf into adaltas:master Oct 9, 2026
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