-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyesno.js
57 lines (39 loc) · 1.63 KB
/
yesno.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const readline = require('readline');
const options = {
yes: [ 'yes', 'y' ],
no: [ 'no', 'n' ]
};
function defaultInvalidHandler ({ question, defaultValue, yesValues, noValues }) {
var yValues = (yesValues || options.yes);
var nValues = (noValues || options.no);
process.stdout.write('\nInvalid Response.\n');
process.stdout.write('Answer either yes : (' + yValues.join(', ') + ') \n');
process.stdout.write('Or no: (' + nValues.join(', ') + ') \n\n');
}
async function ask ({ question, defaultValue, yesValues, noValues, invalid }) {
if (!invalid || typeof invalid !== 'function')
invalid = defaultInvalidHandler;
var yValues = (yesValues || options.yes).map((v) => v.toLowerCase());
var nValues = (noValues || options.no).map((v) => v.toLowerCase());
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(function (resolve, reject) {
rl.question(question + ' ', async function (answer) {
rl.close();
const cleaned = answer.trim().toLowerCase();
if (cleaned == '' && defaultValue != null)
return resolve(defaultValue);
if (yValues.indexOf(cleaned) >= 0)
return resolve(true);
if (nValues.indexOf(cleaned) >= 0)
return resolve(false);
invalid({ question, defaultValue, yesValues, noValues });
const result = await ask({ question, defaultValue, yesValues, noValues, invalid });
resolve(result);
});
});
}
module.exports = ask;