choice.js 1.1 KB

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