Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

choices.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. var assert = require('assert');
  3. var _ = require('lodash');
  4. var Separator = require('./separator');
  5. var Choice = require('./choice');
  6. /**
  7. * Choices collection
  8. * Collection of multiple `choice` object
  9. * @constructor
  10. * @param {Array} choices All `choice` to keep in the collection
  11. */
  12. module.exports = class Choices {
  13. constructor(choices, answers) {
  14. this.choices = choices.map(val => {
  15. if (val.type === 'separator') {
  16. if (!(val instanceof Separator)) {
  17. val = new Separator(val.line);
  18. }
  19. return val;
  20. }
  21. return new Choice(val, answers);
  22. });
  23. this.realChoices = this.choices
  24. .filter(Separator.exclude)
  25. .filter(item => !item.disabled);
  26. Object.defineProperty(this, 'length', {
  27. get() {
  28. return this.choices.length;
  29. },
  30. set(val) {
  31. this.choices.length = val;
  32. }
  33. });
  34. Object.defineProperty(this, 'realLength', {
  35. get() {
  36. return this.realChoices.length;
  37. },
  38. set() {
  39. throw new Error('Cannot set `realLength` of a Choices collection');
  40. }
  41. });
  42. }
  43. /**
  44. * Get a valid choice from the collection
  45. * @param {Number} selector The selected choice index
  46. * @return {Choice|Undefined} Return the matched choice or undefined
  47. */
  48. getChoice(selector) {
  49. assert(_.isNumber(selector));
  50. return this.realChoices[selector];
  51. }
  52. /**
  53. * Get a raw element from the collection
  54. * @param {Number} selector The selected index value
  55. * @return {Choice|Undefined} Return the matched choice or undefined
  56. */
  57. get(selector) {
  58. assert(_.isNumber(selector));
  59. return this.choices[selector];
  60. }
  61. /**
  62. * Match the valid choices against a where clause
  63. * @param {Object} whereClause Lodash `where` clause
  64. * @return {Array} Matching choices or empty array
  65. */
  66. where(whereClause) {
  67. return _.filter(this.realChoices, whereClause);
  68. }
  69. /**
  70. * Pluck a particular key from the choices
  71. * @param {String} propertyName Property name to select
  72. * @return {Array} Selected properties
  73. */
  74. pluck(propertyName) {
  75. return _.map(this.realChoices, propertyName);
  76. }
  77. // Expose usual Array methods
  78. indexOf() {
  79. return this.choices.indexOf.apply(this.choices, arguments);
  80. }
  81. forEach() {
  82. return this.choices.forEach.apply(this.choices, arguments);
  83. }
  84. filter() {
  85. return this.choices.filter.apply(this.choices, arguments);
  86. }
  87. find(func) {
  88. return _.find(this.choices, func);
  89. }
  90. push() {
  91. var objs = _.map(arguments, val => new Choice(val));
  92. this.choices.push.apply(this.choices, objs);
  93. this.realChoices = this.choices.filter(Separator.exclude);
  94. return this.choices;
  95. }
  96. };