choice.js 1.0 KB

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