Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reloquent

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reloquent

Ask user configurable questions via read-line.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14
Maintainers
1
Weekly downloads
 
Created
Source

reloquent

npm version

reloquent allows to ask user a question, or a series of questions via the read-line interface.

yarn add -E reloquent

API

There are 3 types of calls to the API:

  • ask a single question as a string
  • ask a single question as an object
  • ask multiple questions

Question

When asking a question which is not a string, the question object should have the following structure:

PropertyTypeDescription
textstringDisplay text. Required.
validationasync functionA function which needs to throw an error if validation does not pass.
postProcessasync functionA function to transform the answer.
defaultValuestringDefault answer.
getDefaultasync functionA function to get default value.

askSingle(question: string|Question, timeout?: number) => Promise.<string>

Ask a question and wait for the answer. If a timeout is passed, the promise will expire after the specified number of milliseconds if answer was not given.

A question can be a simple string:

/* yarn example/string.js */
import { askSingle } from 'reloquent'

(async () => {
  try {
    const answer = await askSingle('What brought you her', 10000)
    console.log(`You've answered: ${answer}`)
  } catch (err) {
    console.log(err)
    console.log('Nevermind...')
  }
})()
What brought you her: I guess Art is the cause.
I guess Art is the cause.

Or it can be an object:

/* yarn example/single.js */
import { askSingle } from 'reloquent'

(async () => {
  const answer = await askSingle({
    text: 'Do you wish me to stay so long?',
    validation(a) {
      if (a.length < 5) {
        throw new Error('The answer is too short')
      }
    },
    defaultValue: 'I desire it much',
    postProcess(a) {
      return `${a}!`
    },
  })
  console.log(answer)
})()
Do you wish me to stay so long? [I desire it much]
I desire it much!

ask(questions: <string, Question>, timeout:number) => Promise.<object>

Ask a series of questions and transform them into answers.

/* yarn example/questions.js */
import ask from 'reloquent'

const questions = {
  title: {
    text: 'Title',
    validation: (a) => {
      if (!a) {
        throw new Error('Please enter a title.')
      }
    },
  },
  description: {
    text: 'Description',
    postProcess: s => s.trim(),
    defaultValue: 'A test default value',
  },
  date: {
    text: 'Date',
    async getDefault() {
      await new Promise(r => setTimeout(r, 200))
      return new Date().toLocaleString()
    },
  },
}

;(async () => {
  try {
    const answers = await ask(questions)
    console.log(answers)
  } catch (err) {
    console.log()
    console.log(err)
  }
})()

If you provide the following answers (leaving Date as it is):

Title: hello
Description: [A test default value] world
Date: [2018-6-9 07:11:03]

You will get the following object as the result:

{ title: 'hello',
  description: 'world',
  date: '2018-6-9 07:11:03' }

(c) Art Deco Code 2018

Keywords

FAQs

Package last updated on 11 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc