|
| 1 | +"use strict"; |
| 2 | +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { |
| 3 | + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { |
| 4 | + if (ar || !(i in from)) { |
| 5 | + if (!ar) ar = Array.prototype.slice.call(from, 0, i); |
| 6 | + ar[i] = from[i]; |
| 7 | + } |
| 8 | + } |
| 9 | + return to.concat(ar || Array.prototype.slice.call(from)); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +var Category_1 = require("./Category"); |
| 13 | +var Util_1 = require("./Util"); |
| 14 | +/** |
| 15 | + * @class Class for transforming raw API data to developer friendly data. |
| 16 | + * @private |
| 17 | + */ |
| 18 | +var Constructor = /** @class */ (function () { |
| 19 | + function Constructor() { |
| 20 | + } |
| 21 | + /** |
| 22 | + * Parses a raw category to be more JavaScript friendly and less verbose. |
| 23 | + * @param {RawCategoryResponse} rawCategoryData The raw category. |
| 24 | + * @returns {CategoryData} |
| 25 | + */ |
| 26 | + Constructor.category = function (rawCategoryData) { |
| 27 | + return { |
| 28 | + id: rawCategoryData.category_id, |
| 29 | + name: Category_1.default.nameById(rawCategoryData.category_id), |
| 30 | + questionCount: { |
| 31 | + total: rawCategoryData.category_question_count.total_question_count, |
| 32 | + easy: rawCategoryData.category_question_count.total_easy_question_count, |
| 33 | + medium: rawCategoryData.category_question_count.total_medium_question_count, |
| 34 | + hard: rawCategoryData.category_question_count.total_hard_question_count, |
| 35 | + }, |
| 36 | + }; |
| 37 | + }; |
| 38 | + /** |
| 39 | + * Parses each object in a raw question array to be more JavaScript friendly. |
| 40 | + * @param {RawQuestion[]} rawQuestions An array of raw questions. |
| 41 | + * @returns {Question<unknown>[]} |
| 42 | + */ |
| 43 | + Constructor.questions = function (rawQuestions) { |
| 44 | + return rawQuestions.map(function (question) { |
| 45 | + return { |
| 46 | + value: question.question, |
| 47 | + category: { |
| 48 | + id: Category_1.default.idByName(Category_1.default.decodeEncodedCategoryName(question.category)), |
| 49 | + name: question.category, |
| 50 | + getData: function () { |
| 51 | + return Category_1.default.getCategory(this.id); |
| 52 | + }, |
| 53 | + }, |
| 54 | + type: question.type, |
| 55 | + difficulty: question.difficulty, |
| 56 | + correctAnswer: question.type === "multiple" |
| 57 | + ? question.correct_answer |
| 58 | + : question.correct_answer.toLowerCase(), |
| 59 | + incorrectAnswers: question.type === "multiple" |
| 60 | + ? question.incorrect_answers |
| 61 | + : question.incorrect_answers[0].toLowerCase(), |
| 62 | + allAnswers: Util_1.default.shuffleArray(__spreadArray([ |
| 63 | + question.correct_answer |
| 64 | + ], (question.type === "multiple" |
| 65 | + ? question.incorrect_answers |
| 66 | + : question.incorrect_answers.map(function (s) { return s.toLowerCase(); })), true)), |
| 67 | + checkAnswer: function (str, caseSensitive) { |
| 68 | + if (caseSensitive === void 0) { caseSensitive = false; } |
| 69 | + if (!caseSensitive) { |
| 70 | + return str.toLowerCase() === this.correctAnswer.toLowerCase(); |
| 71 | + } |
| 72 | + return str === this.correctAnswer; |
| 73 | + }, |
| 74 | + }; |
| 75 | + }); |
| 76 | + }; |
| 77 | + return Constructor; |
| 78 | +}()); |
| 79 | +exports.default = Constructor; |
0 commit comments