choice.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const schema_1 = require("../schema");
  4. const utils_1 = require("../utils");
  5. class ChoiceSchema extends schema_1.Schema {
  6. constructor(parameters) {
  7. super(parameters);
  8. this._choices = utils_1.mapFromArray(parameters.choices.map(choice => choice && typeof choice === 'object' ? choice : { value: choice }), 'value');
  9. }
  10. expected({ descriptor }) {
  11. const choiceValues = Array.from(this._choices.keys())
  12. .map(value => this._choices.get(value))
  13. .filter(choiceInfo => !choiceInfo.deprecated)
  14. .map(choiceInfo => choiceInfo.value)
  15. .sort(utils_1.comparePrimitive)
  16. .map(descriptor.value);
  17. const head = choiceValues.slice(0, -2);
  18. const tail = choiceValues.slice(-2);
  19. return head.concat(tail.join(' or ')).join(', ');
  20. }
  21. validate(value) {
  22. return this._choices.has(value);
  23. }
  24. deprecated(value) {
  25. const choiceInfo = this._choices.get(value);
  26. return choiceInfo && choiceInfo.deprecated ? { value } : false;
  27. }
  28. forward(value) {
  29. const choiceInfo = this._choices.get(value);
  30. return choiceInfo ? choiceInfo.forward : undefined;
  31. }
  32. redirect(value) {
  33. const choiceInfo = this._choices.get(value);
  34. return choiceInfo ? choiceInfo.redirect : undefined;
  35. }
  36. }
  37. exports.ChoiceSchema = ChoiceSchema;