UNPKG

1.18 kBJavaScriptView Raw
1'use strict';
2const _ = {
3 isString: require('lodash/isString'),
4 isNumber: require('lodash/isNumber'),
5 extend: require('lodash/extend'),
6 isFunction: require('lodash/isFunction'),
7};
8
9/**
10 * Choice object
11 * Normalize input as choice object
12 * @constructor
13 * @param {Number|String|Object} val Choice value. If an object is passed, it should contains
14 * at least one of `value` or `name` property
15 */
16
17module.exports = class Choice {
18 constructor(val, answers) {
19 // Don't process Choice and Separator object
20 if (val instanceof Choice || val.type === 'separator') {
21 // eslint-disable-next-line no-constructor-return
22 return val;
23 }
24
25 if (_.isString(val) || _.isNumber(val)) {
26 this.name = String(val);
27 this.value = val;
28 this.short = String(val);
29 } else {
30 _.extend(this, val, {
31 name: val.name || val.value,
32 value: 'value' in val ? val.value : val.name,
33 short: val.short || val.name || val.value,
34 });
35 }
36
37 if (_.isFunction(val.disabled)) {
38 this.disabled = val.disabled(answers);
39 } else {
40 this.disabled = val.disabled;
41 }
42 }
43};