Comparing version 0.2.0 to 1.0.0
@@ -0,1 +1,8 @@ | ||
## 1.0.0 (9 June) | ||
- [ecma] update to modules | ||
- [api] change the api to allow to ask single questions. | ||
- [doc] update doc | ||
- [package] move to [Art Deco Code](https://artdeco.bz) | ||
## 0.2.0 (31 May) | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "reloquent", | ||
"version": "0.2.0", | ||
"description": "A simple Node.js module to query readline", | ||
"main": "src/index.js", | ||
"version": "1.0.0", | ||
"description": "Ask user configurable questions via read-line.", | ||
"main": "build", | ||
"scripts": { | ||
"test": "cross-env ZOROASTER_TIMEOUT=20000 zoroaster test/spec", | ||
"test-watch": "cross-env ZOROASTER_TIMEOUT=20000 zoroaster test/spec --watch" | ||
"t": "zoroaster -b", | ||
"test": "zoroaster test/spec -b", | ||
"test-build": "BABEL_ENV=test-build zoroaster test/spec -b", | ||
"build": "babel src --out-dir build", | ||
"e": "node example", | ||
"example/single.js": "yarn e example/single.js", | ||
"example/string.js": "yarn e example/string.js", | ||
"example/questions.js": "yarn e example/questions.js", | ||
"lint": "eslint ." | ||
}, | ||
"files": [ | ||
"src/" | ||
"build" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Sobesednik/reloquent.git" | ||
"url": "git://github.com/artdecocode/reloquent.git" | ||
}, | ||
"keywords": [ | ||
"promise", | ||
"timeout", | ||
"readline", | ||
"mnp" | ||
"ask", | ||
"questions", | ||
"stdin", | ||
"read", | ||
"input" | ||
], | ||
"author": "Anton <anton@sobesednik.media>", | ||
"author": "Anton <anton@adc.sh>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Sobesednik/reloquent/issues" | ||
"url": "https://github.com/artdecocode/reloquent/issues" | ||
}, | ||
"homepage": "https://github.com/Sobesednik/reloquent#readme", | ||
"homepage": "https://github.com/artdecocode/reloquent#readme", | ||
"devDependencies": { | ||
"@babel/cli": "7.0.0-beta.49", | ||
"@babel/core": "7.0.0-beta.49", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.49", | ||
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.49", | ||
"@babel/register": "7.0.0-beta.49", | ||
"babel-plugin-transform-rename-import": "2.2.0", | ||
"cross-env": "5.0.0", | ||
"zoroaster": "0.4.4" | ||
"eslint": "4.19.1", | ||
"eslint-config-artdeco": "1.0.0", | ||
"zoroaster": "2.1.0" | ||
}, | ||
@@ -33,0 +50,0 @@ "dependencies": { |
198
README.md
@@ -5,123 +5,157 @@ # reloquent | ||
`reloquent` allows to ask user a question. | ||
`reloquent` allows to ask user a question, or a series of questions via the read-line interface. | ||
## `reloquent(question:string, timeout?:number) => Interface` | ||
```sh | ||
yarn add -E reloquent | ||
``` | ||
The function returns a readline instance, with `.promise` property, which will resolve | ||
when the user answers the question. You can also optionally supply a timeout argument after | ||
which you want the promise to be rejected. | ||
## API | ||
```js | ||
const reloquent = require('reloquent') | ||
There are 3 types of calls to the API: | ||
const rl = reloquent('How are you today? ') | ||
rl.promise.then((answer) => { | ||
console.log(`You've answered: %s`, answer) // no timeout | ||
}) | ||
``` | ||
- 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: | ||
| Property | Type | Description | | ||
|--------------|----------------|-----------------------------------------------------------------------| | ||
| **text** | string | Display text. Required. | | ||
| validation | async function | A function which needs to throw an error if validation does not pass. | | ||
| postProcess | async function | A function to transform the answer. | | ||
| defaultValue | string | Default answer. | | ||
| getDefault | async function | A 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: | ||
```js | ||
const reloquent = require('reloquent') | ||
/* yarn example/string.js */ | ||
import { askSingle } from 'reloquent' | ||
const rl = reloquent('How are you today? ', 5000) // 5s timeout | ||
rl.promise.catch((err) => { | ||
(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...') | ||
}) | ||
} | ||
})() | ||
``` | ||
## `reloquent.askQuestions(questions:object[], timeout:number, singleValue:string) => Promise<object>` | ||
```fs | ||
What brought you her: I guess Art is the cause. | ||
``` | ||
Ask a series of questions and transform them into answers. `questions` must be an object of | ||
the following structure: | ||
```fs | ||
I guess Art is the cause. | ||
``` | ||
Or it can be an object: | ||
```js | ||
const reloquent = require('reloquent') | ||
/* yarn example/single.js */ | ||
import { askSingle } from 'reloquent' | ||
const questions = { | ||
title: { | ||
text: 'Title: ', | ||
validation: (a) => { | ||
if (!a) { | ||
throw new Error('Please enter a title.') | ||
} | ||
}, | ||
(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') | ||
} | ||
}, | ||
description: { | ||
text: 'Description: ', | ||
postProcess: s => s.trim(), | ||
defaultValue: '', | ||
defaultValue: 'I desire it much', | ||
postProcess(a) { | ||
return `${a}!` | ||
}, | ||
date: { | ||
text: 'Date: ', | ||
getDefault: () => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(Date.now()), 200) | ||
}) | ||
}, | ||
}, | ||
} | ||
}) | ||
console.log(answer) | ||
})() | ||
``` | ||
reloquent.askQuestions(questions) | ||
.then((answers) => { | ||
console.log(answers) | ||
}) | ||
```fs | ||
Do you wish me to stay so long? [I desire it much] | ||
``` | ||
If you provide the following answers (leaving _Date_ as it is): | ||
```fs | ||
Title: title | ||
Description: desc | ||
Date: : [1496188514306] | ||
I desire it much! | ||
``` | ||
You will get the following object as the result: | ||
### `ask(questions: <string, Question>, timeout:number) => Promise.<object>` | ||
Ask a series of questions and transform them into answers. | ||
```js | ||
{ title: 'title', description: 'desc', date: 1496188514306 } | ||
``` | ||
/* yarn example/questions.js */ | ||
import ask from 'reloquent' | ||
### Question Object | ||
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() | ||
}, | ||
}, | ||
} | ||
A question object supports the following properties: | ||
;(async () => { | ||
try { | ||
const answers = await ask(questions) | ||
console.log(answers) | ||
} catch (err) { | ||
console.log() | ||
console.log(err) | ||
} | ||
})() | ||
``` | ||
* *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. | ||
If you provide the following answers (leaving _Date_ as it is): | ||
### Single Value | ||
```fs | ||
Title: hello | ||
Description: [A test default value] world | ||
Date: [2018-6-9 07:11:03] | ||
``` | ||
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. | ||
You will get the following object as the result: | ||
```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 | ||
}) | ||
{ title: 'hello', | ||
description: 'world', | ||
date: '2018-6-9 07:11:03' } | ||
``` | ||
## todo | ||
<!-- ## todo | ||
* show timer on the right | ||
* accept other ios | ||
* reject when closed without answer | ||
* reject when closed without answer --> | ||
--- | ||
(c) [Sobesednik-Media](https://sobesednik.media) 2017 | ||
(c) [Art Deco Code][1] 2018 | ||
[1]: https://artdeco.bz |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11365
137
0
161
10
1