2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
2022-04-07 07:06:43 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const _ = {
|
|
|
|
filter: require('lodash/filter'),
|
|
|
|
map: require('lodash/map'),
|
|
|
|
};
|
|
|
|
const Separator = require('./separator');
|
|
|
|
const Choice = require('./choice');
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Choices collection
|
|
|
|
* Collection of multiple `choice` object
|
|
|
|
*/
|
|
|
|
module.exports = class Choices {
|
2022-04-07 07:06:43 +00:00
|
|
|
/** @param {Array} choices All `choice` to keep in the collection */
|
2022-01-21 08:28:41 +00:00
|
|
|
constructor(choices, answers) {
|
2022-04-07 07:06:43 +00:00
|
|
|
this.choices = choices.map((val) => {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (val.type === 'separator') {
|
|
|
|
if (!(val instanceof Separator)) {
|
|
|
|
val = new Separator(val.line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Choice(val, answers);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.realChoices = this.choices
|
|
|
|
.filter(Separator.exclude)
|
2022-04-07 07:06:43 +00:00
|
|
|
.filter((item) => !item.disabled);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
Object.defineProperty(this, 'length', {
|
|
|
|
get() {
|
|
|
|
return this.choices.length;
|
|
|
|
},
|
|
|
|
set(val) {
|
|
|
|
this.choices.length = val;
|
2022-04-07 07:06:43 +00:00
|
|
|
},
|
2022-01-21 08:28:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'realLength', {
|
|
|
|
get() {
|
|
|
|
return this.realChoices.length;
|
|
|
|
},
|
|
|
|
set() {
|
|
|
|
throw new Error('Cannot set `realLength` of a Choices collection');
|
2022-04-07 07:06:43 +00:00
|
|
|
},
|
2022-01-21 08:28:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a valid choice from the collection
|
|
|
|
* @param {Number} selector The selected choice index
|
|
|
|
* @return {Choice|Undefined} Return the matched choice or undefined
|
|
|
|
*/
|
|
|
|
|
|
|
|
getChoice(selector) {
|
2022-04-07 07:06:43 +00:00
|
|
|
assert(typeof selector === 'number');
|
2022-01-21 08:28:41 +00:00
|
|
|
return this.realChoices[selector];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a raw element from the collection
|
|
|
|
* @param {Number} selector The selected index value
|
|
|
|
* @return {Choice|Undefined} Return the matched choice or undefined
|
|
|
|
*/
|
|
|
|
|
|
|
|
get(selector) {
|
2022-04-07 07:06:43 +00:00
|
|
|
assert(typeof selector === 'number');
|
2022-01-21 08:28:41 +00:00
|
|
|
return this.choices[selector];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match the valid choices against a where clause
|
|
|
|
* @param {Object} whereClause Lodash `where` clause
|
|
|
|
* @return {Array} Matching choices or empty array
|
|
|
|
*/
|
|
|
|
|
|
|
|
where(whereClause) {
|
|
|
|
return _.filter(this.realChoices, whereClause);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pluck a particular key from the choices
|
|
|
|
* @param {String} propertyName Property name to select
|
|
|
|
* @return {Array} Selected properties
|
|
|
|
*/
|
|
|
|
|
|
|
|
pluck(propertyName) {
|
|
|
|
return _.map(this.realChoices, propertyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expose usual Array methods
|
2022-04-07 07:06:43 +00:00
|
|
|
indexOf(...args) {
|
|
|
|
return this.choices.indexOf(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
forEach(...args) {
|
|
|
|
return this.choices.forEach(...args);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:06:43 +00:00
|
|
|
filter(...args) {
|
|
|
|
return this.choices.filter(...args);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:06:43 +00:00
|
|
|
reduce(...args) {
|
|
|
|
return this.choices.reduce(...args);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
find(func) {
|
2022-04-07 07:06:43 +00:00
|
|
|
return this.choices.find(func);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 07:06:43 +00:00
|
|
|
push(...args) {
|
|
|
|
const objs = args.map((val) => new Choice(val));
|
|
|
|
this.choices.push(...objs);
|
|
|
|
this.realChoices = this.choices
|
|
|
|
.filter(Separator.exclude)
|
|
|
|
.filter((item) => !item.disabled);
|
2022-01-21 08:28:41 +00:00
|
|
|
return this.choices;
|
|
|
|
}
|
|
|
|
};
|