console-questions
console-questions
is a simple way to make questions on the console.
Instalation
-
npm:
Fromnpm install console-questions
-
GitHub:
Fromnpm install https://github.com/javalsai/console-questions
Code Sample
Just as simple as that.
Structure
The returned module is a EventEmitter
overwritten.
{
-
ask:
[Function: ask]
, -
setDefaultOptions:
[Function: setDefaultOptions]
-
getDefaultOptions:
[Function: getDefaultOptions]
-
getCustomizedOptions:
[Function: getCustomizedOptions]
-
restartOptions:
[Function: restartOptions]
}
// The events of the EventEmitter
are at events.
Options
This are the default options used in the module.
{
after: '\n', // A string to put after every question
before: '', // A string to put before every question
limit: null, // The limit of time to answer a question in ms (if exceeded return null)
showTyping: true, // If show what the user is typing or not
callback: () => {} // Just a callback
}
Functions
ask (question: String, options: Object)
- Return value:
Promise
- Arguments:
-
String
(the question) -
Object
the options.
-
- Resolved value:
String
(the user response for the question).
// The options will be assigned with Object.assign()
to the default options.
setDefaultOptions (options)
- Return value: options (the argument).
- Argumets:
-
Object
: Options
-
// Set the default options instead of using the default options.
getDefaultOptions ()
- Return value: The default options.
- Arguments: None.
// IMPORTANT: Returns the default options, not your customized default options.
getCustomizedOptions ()
- Return value: Your customized default options.
- Arguments: None.
// IMPORTANT: Returns your customized default options not the default options.
restartOptions ()
- Return value: The default options.
- Arguments: None.
// This restart your customized default options to the default options.
Events
"keypress"
Emited when a key is pressed.
const cq = require('console-questions');
cq.on('keypress', key => {
console.log('You pressed: ' + key);
});
"input"
Emited when the user press Enter
.
const cq = require('console-questions');
cq.on('input', sentence => {
console.log('You written: \n' + sentence);
});