choice.js 814 B

12345678910111213141516171819202122232425262728293031323334
  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. } else {
  19. _.extend(this, val, {
  20. name: val.name || val.value,
  21. value: val.hasOwnProperty('value') ? val.value : val.name
  22. });
  23. }
  24. if (_.isFunction(val.disabled)) {
  25. this.disabled = val.disabled(answers);
  26. } else {
  27. this.disabled = val.disabled;
  28. }
  29. };