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 - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

src/ask-questions.js

4

CHANGELOG.md

@@ -0,1 +1,5 @@

## 0.2.0 (31 May)
- [feature] `askQuestions`: ask multiple questions.
## 0.1.0 (23 May)

@@ -2,0 +6,0 @@

7

package.json
{
"name": "reloquent",
"version": "0.1.2",
"version": "0.2.0",
"description": "A simple Node.js module to query readline",
"main": "src/index.js",
"scripts": {
"test": "zoroaster test/spec",
"test-watch": "zoroaster test/spec --watch"
"test": "cross-env ZOROASTER_TIMEOUT=20000 zoroaster test/spec",
"test-watch": "cross-env ZOROASTER_TIMEOUT=20000 zoroaster test/spec --watch"
},

@@ -30,2 +30,3 @@ "files": [

"devDependencies": {
"cross-env": "5.0.0",
"zoroaster": "0.4.4"

@@ -32,0 +33,0 @@ },

@@ -32,2 +32,88 @@ # reloquent

## `reloquent.askQuestions(questions:object[], timeout:number, singleValue:string) => Promise<object>`
Ask a series of questions and transform them into answers. `questions` must be an object of
the following structure:
```js
const reloquent = require('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: '',
},
date: {
text: 'Date: ',
getDefault: () => {
return new Promise((resolve) => {
setTimeout(() => resolve(Date.now()), 200)
})
},
},
}
reloquent.askQuestions(questions)
.then((answers) => {
console.log(answers)
})
```
If you provide the following answers (leaving _Date_ as it is):
```fs
Title: title
Description: desc
Date: : [1496188514306]
```
You will get the following object as the result:
```js
{ title: 'title', description: 'desc', date: 1496188514306 }
```
### Question Object
A question object supports the following properties:
* *text*:_string_ - question text
* *validation*:_fn(answer:string)_ - validation function to run against answer. Needs to throw an error if validation does not pass
* *postProcess*:_fn(answer:string)_ - transform answer after it has been submitted and after validation
* *defaultValue*:_string_ - what default value should be recorded as an answer
* *getDefault*:_fn()_ - possibly async function to run before asking the question. Will provide a default answer, e.g., `What is your OS [win]:` can be achieved with `() => Promise.resolve(process.platform)` function.
### Single Value
You can supply a single value argument, which will ensure that only an answer to the requested
question is returned in resolved promise. This can be useful when you want to use `askQuestions`
interface to ask a single question.
```js
const reloquent = require('reloquent')
reloquent.askQuestions({
singleQuestion: {
text: 'This is meant to be a single question. Single just like me.',
validation: () => {
console.log('oh no you don\'t have to say anything, it\'s OK.')
},
defaultValue: true,
postProcess: () => 'programming is an art',
},
}, null, 'singleQuestion')
.then((res) => {
console.log(res) // programming is art
})
```
## todo

@@ -34,0 +120,0 @@

@@ -1,47 +0,11 @@

'use strict'
const ask = require('./ask')
const askQuestions = require('./ask-questions')
const readline = require('readline')
const promto = require('promto')
Object.defineProperties(ask, {
askQuestions: {
get: () => askQuestions,
},
})
/**
* Ask user a question and wait for an answer.
* @param {string} question Question to present to the user
* @return {Promise<string>} An answer from the user
*/
function ask(question, timeout) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
let rejectQuestion
const promise = new Promise((resolve, reject) => {
rejectQuestion = () => {
reject(new Error('Question was rejected')) // no pending promises
}
rl.question(question, (an) => {
resolve(an)
})
})
.then((res) => {
return res
})
.catch((err) => {
throw err
})
const p = timeout ? promto(promise, timeout, `reloquent: ${question}`) : promise
rl.promise = p.then(
(res) => {
rl.close()
rejectQuestion()
return res
},
(err) => {
rl.close()
rejectQuestion()
throw err
}
)
return rl
}
module.exports = ask
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