SBoudrias/Inquirer.js

Feature request? input pattern

Open

#1.328 geöffnet am 2. Nov. 2023

Auf GitHub ansehen
 (8 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (19.291 Stars) (1.253 Forks)batch import
featuregood first issue

Beschreibung

Hallo, i've been using inquirer for a while now and this it#s quite elegant (slim & flexible).

That said I never got to warm with the input prompt as it seems to slim to me 😅 Let's assume i want a number input, or a floating point input or a phone address input, currently with the input there#s no good way to archive it.

Let's assume a simple prompt:

> Your most loved float: ..., where the expected input is some float.

When using the input component a user can enter whatever he wants and the options I have to fix this are:

  1. use transform to just discard any invalid characters visually -> afterwards "cleanup the input" (which can be quite hard as ppl might be using . or , or sprinkle spaces, letter etc)
  2. use validate to ensure the input is valid after enter-press ... which is not so great either as you then have to start fixing the input

Imo it would be nicer, if there was a way to just directly specify an allowed input pattern.

What i usually end up doing is adding a custom input component which in essence is a copy of the current input with the addition of:

pattern?: RegExp;
patternError?: string;

inside of useKeyPress i then, just omit keys when the pattern would be invalidated:

if(!pattern.test(rl.line) {
      const line = rl.line;
      rl.clearLine(0);
      rl.write(line.slice(0, -1));
      setError(patternError);
} else {
// current event handling
}

I'm wondering if that would be a welcome addition to the project? I'm not sold on the naming of paramters. For me the RegExp was sufficient, but perhaps a more generic: discardKey: (key, line) => {} could be considered

Contributor Guide