choice.js 871 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var _ = require('lodash');
  3. /**
  4. * Choice object
  5. * Normalize input as choice object
  6. * @constructor
  7. * @param {String|Object} val Choice value. If an object is passed, it should contains
  8. * at least one of `value` or `name` property
  9. */
  10. var Choice = module.exports = function (val, answers) {
  11. // Don't process Choice and Separator object
  12. if (val instanceof Choice || val.type === 'separator') {
  13. return val;
  14. }
  15. if (_.isString(val)) {
  16. this.name = val;
  17. this.value = val;
  18. this.short = val;
  19. } else {
  20. _.extend(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 (_.isFunction(val.disabled)) {
  27. this.disabled = val.disabled(answers);
  28. } else {
  29. this.disabled = val.disabled;
  30. }
  31. };