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

Any way to cast the input? #1383

Open
ValYouW opened this issue Apr 7, 2024 · 2 comments
Open

Any way to cast the input? #1383

ValYouW opened this issue Apr 7, 2024 · 2 comments

Comments

@ValYouW
Copy link

ValYouW commented Apr 7, 2024

In the old inquirer there was a Number input, Im trying to do something similar with the new @inquirer/prompts and wonder if there is a way to cast the value before it gets to the validate method. Any idea?
I know I can do the cast after calling await input but:

  1. I think it is nicer to have all the input-related code in one place (the input options)
  2. Currently the validate method gets a string and I need to "parse" it there as well.

Thx.

@SBoudrias
Copy link
Owner

Hey, I'm not against re-adding a good number prompt if you want to send a PR! I didn't reimplement as I found the previous UX bad, and didn't care much about it.

With the input prompt, for now I would do something:

  const answer = parseInt(
    await input({
      message: 'Provide a number:',
      validate: (value) => !Number.isNaN(value) || 'You must provide a number',
    }),
    10,
  );

@ValYouW
Copy link
Author

ValYouW commented Apr 8, 2024

Yes, workarounds are possible but less nicer than the old number prompt.
My validate really needs a number to check it is within range etc. I know it's still doable but...
I wonder whether a "cast"/"filter" method would be a better addition to the api, that way if someone wants a number he may cast the string to number, if someone wants a date she may cast the string to date, all before the validate stage, and the result would also be the result of the "cast".

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

No branches or pull requests

2 participants