1.0.1 • Published 3 years ago

json-inquiry v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Json Inquiry

npm install json-inquiry
yarn add json-inquiry

For when you want to have a user select an option from an object of data instead of just a list. For instance if you had data under different categories.

Also even if you use this for a list, it's much clearer than making a list of options with inquirer since you only have to make one method.

First you create a JsonInquiry Object Next you activate it with <INQUIRY>.activate You can activate the inquiry as many times as you want.

Usage

import JsonInquiry from 'json-inquiry';
const inquiry = new JsonInquiry(<TITLE>, <CHOICES>, <VALIDATOR?>)

async function start() {
    // Await the inquiry and you can use the validated result.
    const result = await inquiry.activate();
    console.log({result})
}

start();

TITLE - This is the title of your inquirer prompt that the user will see.

CHOICES - These are the choices can choose from when the inquiry is displayed.

VALIDATOR - A custom function that can validate the data as you wish, it's optional but can be useful for something like the recommended option in the example.

Example

import JsonInquiry from 'json-inquiry'
new JsonInquiry("Select Cheese", {
    recommended: "#gouda",
    gouda: 1,
    blue: 2,
    regular: 3,
    stinky: 4
}, (selection, options) => {
    /**
     * This code makes it so that if our selection value starts with a # we return a different value from the object.
     */
    if(typeof selection === "string" && selection.startsWith("#")) {
        selection = selection.substring(1);
        return options[selection];
    }
}).activate().then(result => {
    /**
     * Possible result values for our example are
     * 1, 2, 3, 4, or #gouda
     */
    // If we chose #gouda, the value of {result: 1} would print
    console.log({result});
})