Skip to content

wilnersson/terminal-input-collector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Input Collector

Collect user input in your terminal input quickly and easily.

Motivation

Instead of building your own functions for listening to the input stream, just request a type of input and supply the question. Input Collector handles collecting and validating the answers for you. Choose from five types of input collection:

  • Strings
  • Integers
  • Passwords
  • Single choice
  • Multiple choice

Example usage

Note that Input Collector is an ESM-only module, it can not be imported with require().

import collector from 'input-collector'

const userAge = await collector.requestIntegerInput('What year were you born?', 1900, 2030) // Answers outside supplied min and max range throws an exception.

console.log(userAge) // Logs user answer in terminal.

Installation

Installation through npm is not available at this time, please clone the repository and import per the instructions below.

Alternatively if you simply clone the Github repository, copy all files in the src folder to a destination of your choice, and import the default export from the index.js-file.

Error handling

Throws an Error with name ValidationError when user input is invalid. Recommended usage is looping until you get a valid response.

Example

try {
  const userAge = await collector.requestIntegerInput('What year were you born?', 1900, 2030) // <-- User supplies 'hello world'
} catch (e) {
  console.log(e.name) // -> ValidationError
}

Known issues

[bug] Erasing password input breaks output line (#1)

Testing

Package has been tested and confirmed to be working on the following version of node: 16.8.0

Run the built in unit tests with:

npm test

Of course this only works if you have cloned the repository and installed all dev-dependencies.

npm install

Version

1.0.0

Licence

MIT

About

Laboration 1 in 1DV610, LNU 2022.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published