.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / prompts / quiz.js
1 'use strict';
2
3 const SelectPrompt = require('./select');
4
5 class Quiz extends SelectPrompt {
6   constructor(options) {
7     super(options);
8     if (typeof this.options.correctChoice !== 'number' || this.options.correctChoice < 0) {
9       throw new Error('Please specify the index of the correct answer from the list of choices');
10     }
11   }
12
13   async toChoices(value, parent) {
14     let choices = await super.toChoices(value, parent);
15     if (choices.length < 2) {
16       throw new Error('Please give at least two choices to the user');
17     }
18     if (this.options.correctChoice > choices.length) {
19       throw new Error('Please specify the index of the correct answer from the list of choices');
20     }
21     return choices;
22   }
23
24   check(state) {
25     return state.index === this.options.correctChoice;
26   }
27
28   async result(selected) {
29     return {
30       selectedAnswer: selected,
31       correctAnswer: this.options.choices[this.options.correctChoice].value,
32       correct: await this.check(this.state)
33     };
34   }
35 }
36
37 module.exports = Quiz;