SBoudrias/Inquirer.js

Any way to cast the input?

ValYouW opened this issue · 2 comments

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.

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,
  );

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".