-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
60 lines (46 loc) · 1.11 KB
/
index.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
58
59
60
'use strict';
var debug = require('debug')('prompt-radio');
var Checkbox = require('prompt-checkbox');
/**
* Radio prompt
*/
function Radio(/*question, answers, rl*/) {
debug('initializing from <%s>', __filename);
Checkbox.apply(this, arguments);
this.question.type = 'radio';
this.action('i', function(pos) {
return pos;
});
this.action('a', function(pos) {
return pos;
});
this.action('number', function(pos, key) {
return enable(this, pos, key);
});
this.action('space', function(pos) {
return enable(this, pos);
});
function enable(actions, pos, key) {
pos = actions.position(pos, key) + (key ? -1 : 0);
actions.choices.uncheck();
actions.choices.check(pos);
return pos;
}
}
/**
* Inherit Checkbox prompt
*/
Checkbox.extend(Radio);
/**
* Get selected choice
*/
Radio.prototype.getAnswer = function(input) {
if (Array.isArray(this.options.default)) {
throw new TypeError('expected options.default to be a string or number');
}
return Checkbox.prototype.getAnswer.call(this, arguments)[0];
};
/**
* Module exports
*/
module.exports = Radio;