|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Class representing an InterviewQuestion handler. |
| 7 | + */ |
| 8 | +class InterviewQuestion { |
| 9 | + /** |
| 10 | + * Creates an instance of InterviewQuestion. |
| 11 | + * @param {Object} ports - Dependencies or external resources required. |
| 12 | + * @param {Object} [options={}] - Configuration options. |
| 13 | + * @param {boolean} [options.verbose=false] - Enable verbose logging. |
| 14 | + */ |
| 15 | + constructor(ports, options = {}) { |
| 16 | + this.ports = ports; |
| 17 | + this.options = options; |
| 18 | + this.verbose = options.verbose || false; |
| 19 | + this.data = require(path.resolve(__dirname, 'question.json')); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Logs a message if verbose mode is enabled. |
| 24 | + * @param {string} message - The message to log. |
| 25 | + */ |
| 26 | + log(message) { |
| 27 | + if (this.verbose) { |
| 28 | + console.log(message); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Executes the main functionality of filtering and retrieving questions. |
| 34 | + * @param {string} [filter='all'] - The filter type (e.g., 'all', 'top'). |
| 35 | + * @param {number} [amount=0] - The number of questions to retrieve if filter is 'top'. |
| 36 | + * @param {string|null} [level=null] - The difficulty level of questions ('junior', 'middle', 'senior'). |
| 37 | + * @returns {Promise<Array>} The filtered list of questions. |
| 38 | + * @throws {Error} If an invalid level or filter type is provided. |
| 39 | + */ |
| 40 | + async execute(filter = 'all', amount = 0, level = null) { |
| 41 | + let filteredData = this.data; |
| 42 | + |
| 43 | + // Filter by level if provided |
| 44 | + if (level) { |
| 45 | + const validLevels = ['junior', 'middle', 'senior']; |
| 46 | + if (!validLevels.includes(level)) { |
| 47 | + throw new Error(`Invalid level: ${level}. Valid levels are: ${validLevels.join(', ')}`); |
| 48 | + } |
| 49 | + filteredData = filteredData.filter(item => item.level === level); |
| 50 | + } |
| 51 | + |
| 52 | + switch (filter) { |
| 53 | + case 'all': |
| 54 | + return filteredData; |
| 55 | + case 'top': |
| 56 | + return this.getTopQuestions(amount, filteredData); |
| 57 | + default: |
| 58 | + throw new Error(`Invalid filter type: ${filter}`); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Retrieves the top N questions from the dataset. |
| 64 | + * @param {number} amount - The number of questions to retrieve. |
| 65 | + * @param {Array} data - The dataset to retrieve questions from. |
| 66 | + * @returns {Array} The top N questions. |
| 67 | + * @throws {Error} If the amount is not a positive integer. |
| 68 | + */ |
| 69 | + getTopQuestions(amount, data) { |
| 70 | + if (!Number.isInteger(amount) || amount <= 0) { |
| 71 | + throw new Error('Amount must be a positive integer.'); |
| 72 | + } |
| 73 | + |
| 74 | + // Shuffle the data array |
| 75 | + const shuffledData = data.sort(() => 0.5 - Math.random()); |
| 76 | + |
| 77 | + // Return the top N questions from the shuffled array |
| 78 | + return shuffledData.slice(0, amount); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Factory function to create and execute an InterviewQuestion instance. |
| 84 | + * @param {Object} [options={}] - Configuration options. |
| 85 | + * @param {string} [options.filter='all'] - The filter type (e.g., 'all', 'top'). |
| 86 | + * @param {number} [options.amount=0] - The number of questions to retrieve if filter is 'top'. |
| 87 | + * @param {string|null} [options.level=null] - The difficulty level of questions ('junior', 'middle', 'senior'). |
| 88 | + * @returns {Promise<Array>} The filtered list of questions. |
| 89 | + */ |
| 90 | +module.exports = async function (options = {}) { |
| 91 | + const interviewQuestion = new InterviewQuestion(options); |
| 92 | + const { filter = 'all', amount = 0, level = null } = options; |
| 93 | + return interviewQuestion.execute(filter, amount, level); |
| 94 | +}; |
0 commit comments