npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

question-cache

v0.7.0

Published

A wrapper around inquirer that makes it easy to create and selectively reuse questions.

Downloads

269,718

Readme

question-cache NPM version NPM downloads Build Status

A wrapper around inquirer that makes it easy to create and selectively reuse questions.

Install

Install with npm:

$ npm install --save question-cache

Example

'use strict';

var Store = require('data-store');
var hints = new Store('example-hints-store');
var questions = require('question-cache');

questions()
  .use(function() {
    this.on('ask', function(val, key, question) {
      question.default = hints.get(key);
    });
    this.on('answer', function(val, key) {
      hints.set(key, val);
    });
  })
  .set('first', 'What is your first name?')
  .set('last', 'What is your last name?')
  .set('foo', 'What is foo?', {
    when: function() {
      // console.log(arguments)
    }
  })
  .ask(function(err, answers) {
    console.log(answers);
  });

Screen capture

See the working examples.

Basic Usage

See the working examples.

var questions = require('question-cache')();

// question type "input" is used by default
questions
  .set('name', 'What is your name?')
  .ask('name', function (err, answers) {
    console.log(answers);
  });

inquirer2

You may optionally pass your own instance of inquirer to the constructor:

// on the options
var questions = require('question-cache');
var questions = new Questions({
  inquirer: require('inquirer2')
});

// or if inquirer is the only thing passed
var questions = new Questions(require('inquirer2'));

Getting started

question-cache is a wrapper around inquirer2. If you have any issues related to the interface (like scrolling, colors, styling, etc), then please create an issue on the inquirer2 project.

Asking questions

The simplest way to ask a question is by passing a string and a callback:

questions.ask('name', function (err, answers) {
  console.log(answers);
});

Ask all cached questions

questions.ask(function (err, answers) {
  console.log(answers);
});

API

Questions

Create an instance of Questions with the given options.

Params

  • options {Object}: question cache options

Example

var Questions = new Questions(options);

.set

Calls addQuestion, with the only difference being that .set returns the questions instance and .addQuestion returns the question object. So use .set if you want to chain questions, or .addQuestion if you need the created question object.

Params

  • name {Object|String}: Question name, message (string), or question/options object.
  • value {Object|String}: Question message (string), or question/options object.
  • options {Object|String}: Question/options object.

Example

questions
  .set('drink', 'What is your favorite beverage?')
  .set('color', 'What is your favorite color?')
  .set('season', 'What is your favorite season?');

// or
questions.set('drink', {
  type: 'input',
  message: 'What is your favorite beverage?'
});

// or
questions.set({
  name: 'drink'
  type: 'input',
  message: 'What is your favorite beverage?'
});

.addQuestion

Add a question to be asked at a later point. Creates an instance of Question, so any Question options or settings may be used. Also, the default type is input if not defined by the user.

Params

  • name {Object|String}: Question name, message (string), or question/options object.
  • value {Object|String}: Question message (string), or question/options object.
  • options {Object|String}: Question/options object.

Example

questions.addQuestion('drink', 'What is your favorite beverage?');

// or
questions.addQuestion('drink', {
  type: 'input',
  message: 'What is your favorite beverage?'
});

// or
questions.addQuestion({
  name: 'drink'
  type: 'input',
  message: 'What is your favorite beverage?'
});

.choices

Create a "choices" question from an array of values.

Params

  • key {String}: Question key
  • msg {String}: Question message
  • items {Array}: Choice items
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.choices('foo', ['a', 'b', 'c']);

// or
questions.choices('foo', {
  message: 'Favorite letter?',
  choices: ['a', 'b', 'c']
});

.list

Create a "list" question from an array of values.

Params

  • key {String}: Question key
  • msg {String}: Question message
  • list {Array}: List items
  • queue {String|Array}: Name or array of question names.
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.list('foo', ['a', 'b', 'c']);

// or
questions.list('foo', {
  message: 'Favorite letter?',
  choices: ['a', 'b', 'c']
});

.rawlist

Create a "rawlist" question from an array of values.

Params

  • key {String}: Question key
  • msg {String}: Question message
  • list {Array}: List items
  • queue {String|Array}: Name or array of question names.
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.rawlist('foo', ['a', 'b', 'c']);

// or
questions.rawlist('foo', {
  message: 'Favorite letter?',
  choices: ['a', 'b', 'c']
});

.expand

Create an "expand" question from an array of values.

Params

  • key {String}: Question key
  • msg {String}: Question message
  • list {Array}: List items
  • queue {String|Array}: Name or array of question names.
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.expand('foo', ['a', 'b', 'c']);

// or
questions.expand('foo', {
  message: 'Favorite letter?',
  choices: ['a', 'b', 'c']
});

.confirm

Create a "choices" question from an array of values.

Params

  • queue {String|Array}: Name or array of question names.
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.choices('foo', ['a', 'b', 'c']);
// or
questions.choices('foo', {
  message: 'Favorite letter?',
  choices: ['a', 'b', 'c']
});

.get

Get question name, or group name if question is not found. You can also do a direct lookup using quesions.cache['foo'].

Params

  • name {String}
  • returns {Object}: Returns the question object.

Example

var name = questions.get('name');
//=> question object

.has

Returns true if questions.cache or questions.groups has question name.

  • returns {String}: The name of the question to check

Example

var name = questions.has('name');
//=> true

.del

Delete the given question or any questions that have the given namespace using dot-notation.

  • returns {String}: The name of the question to delete

Example

questions.del('name');
questions.get('name');
//=> undefined

// using dot-notation
questions.del('author');
questions.get('author.name');
//=> undefined

.clearAnswers

Clear all cached answers.

Example

questions.clearAnswers();

.clearQuestions

Clear all questions from the cache.

Example

questions.clearQuestions();

.clear

Clear all cached questions and answers.

Example

questions.clear();

.ask

Ask one or more questions, with the given options and callback.

Params

  • queue {String|Array}: Name or array of question names.
  • options {Object|Function}: Question options or callback function
  • callback {Function}: callback function

Example

questions.ask(['name', 'description'], function(err, answers) {
  console.log(answers);
});

.normalize

Normalize the given value to return an array of question keys.

Params

  • {[type]}: key
  • returns {[type]}

Dot notation

See the working examples.

Qestions may be cached using object-path notatation (e.g. a.b.c).

Example

All of the following will be cached on the name object:

questions
  .set('name.first', 'What is your first name?')
  .set('name.middle', 'What is your middle name?')
  .set('name.last', 'What is your last name?')

Dot notation usage

When cached using dot-notation, there are a few different ways questions that may be asked.

Dot notation usage

ask one

Ask a single name question:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
});

ask all "name" questions

Ask all name questions, first, middle and last:

questions.ask('name', function (err, answers) {
  console.log(answers);
});

array of questions

Ask specific questions on name:

questions.ask(['name.first', 'name.last'], function (err, answers) {
  console.log(answers);
});

ask all questions

Ask specific questions on name:

questions
  .set('name.first', {
    message: 'What is your first name?',
  })
  .set('name.last', {
    message: 'What is your last name?',
  })
  .set('foo', {
    message: 'Any thoughts about foo?',
  })

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});

nested questions

Ask one question at a time, based on feedback:

questions.ask('name.first', function (err, answers) {
  console.log(answers);
  //=> {name: { first: 'Brian' }}

  questions.ask('name.last', function (err, answers) {
    console.log(answers);
    //=> {name: { last: 'Woodward' }}
  });
});

More examples

Mixture of dot notation and non-dot-notation

Given you have the following questions:

questions
  .set('name.first', 'What is your first name?')
  .set('name.last', 'What is your last name?')
  .set('foo', 'Any thoughts about foo?')
  .set('bar', 'Any thoughts about bar?')

The following will ask questions: name.first, name.last and foo

questions.ask(['name', 'foo'], function (err, answers) {
  console.log(answers);
});

About

Related projects

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.1.30, on August 17, 2016.