choice.js 1011 B

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