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

feat: allow trailing zeros & only specifying decimal component when using string.numeric.parse #1220

Conversation

TizzySaurus
Copy link
Contributor

@TizzySaurus TizzySaurus commented Dec 3, 2024

What

Add support for trailing zeros when using string.numeric.parse, such that e.g. type("string.numeric.parse")("3.00") and type("string.numeric.parse")(".5") works.

Why

Stemmed from conversation with @ssalbdivad on Discord here.

How

Add new numericStringMatcher regex, which has the same restrictions as the pre-existing wellFormedNumberMatcher bar the trailing zeros, and update the string.numeric.parse keyword to use it instead of wellFormedNumberMatcher.

Here's the regex change to allow trailing zeros:

- /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/
+ /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d+)?$/

Test

pnpm i && pnpm build && node

then within the node repl:

const { type } = await import("arktype");
type("string.numeric.parse")("3.00") // doesn't return ArkErrors like it used to before this PR;

ark/util/numbers.ts Outdated Show resolved Hide resolved
@TizzySaurus TizzySaurus changed the title feat: allow trailing zeros when using string.numeric.parse feat: allow trailing zeros & only specifying decimal component when using string.numeric.parse Dec 3, 2024
@ssalbdivad ssalbdivad merged commit 3ac3fd8 into arktypeio:main Dec 4, 2024
6 checks passed
@ssalbdivad
Copy link
Member

Definitely a QOL improvement, thanks so much 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (merged or closed)
Development

Successfully merging this pull request may close these issues.

2 participants