|
| 1 | +import { cleanStringAndParseFloat, isNumeric } from '../../../shared/infrastructure/utils/string-utils.js'; |
| 2 | +import lodash from 'lodash'; |
| 3 | +import { applyPreTreatments, applyTreatments } from './validation-treatments.js'; |
| 4 | +import { validateAnswer } from './string-comparison-service.js'; |
| 5 | +import { AnswerStatus } from '../models/validator/AnswerStatus.js'; |
| 6 | +import { getEnabledTreatments, useLevenshteinRatio } from './services-utils.js'; |
| 7 | + |
| 8 | +const CHALLENGE_NUMBER_FORMAT = 'nombre'; |
| 9 | +const { every, isEmpty, isString, map } = lodash; |
| 10 | + |
| 11 | +const match = function ({ answer, challengeFormat, solutions }) { |
| 12 | + const deactivations = solutions.deactivations; |
| 13 | + const qrocBlocksTypes = solutions.qrocBlocksTypes || {}; |
| 14 | + const shouldApplyTreatments = qrocBlocksTypes[Object.keys(qrocBlocksTypes)[0]] === 'select' ? false : true; |
| 15 | + |
| 16 | + const isIncorrectAnswerFormat = !isString(answer); |
| 17 | + const isIncorrectSolutionFormat = solutions.value?.some((s) => !isString(s) || isEmpty(s)); |
| 18 | + |
| 19 | + if (isIncorrectAnswerFormat || isIncorrectSolutionFormat) { |
| 20 | + return AnswerStatus.KO; |
| 21 | + } |
| 22 | + |
| 23 | + const solutionsValue = solutions.value; |
| 24 | + const areAllNumericSolutions = every(solutionsValue, (solution) => { |
| 25 | + return isNumeric(solution); |
| 26 | + }); |
| 27 | + |
| 28 | + if (isNumeric(answer) && areAllNumericSolutions && challengeFormat === CHALLENGE_NUMBER_FORMAT) { |
| 29 | + return _getAnswerStatusFromNumberMatching(answer, solutionsValue); |
| 30 | + } |
| 31 | + |
| 32 | + return _getAnswerStatusFromStringMatching(answer, solutionsValue, deactivations, shouldApplyTreatments); |
| 33 | +}; |
| 34 | + |
| 35 | +export { match }; |
| 36 | + |
| 37 | +function _getAnswerStatusFromNumberMatching(answer, solutions) { |
| 38 | + const treatedSolutions = solutions.map((solution) => cleanStringAndParseFloat(solution)); |
| 39 | + const treatedAnswer = cleanStringAndParseFloat(answer); |
| 40 | + const indexOfSolution = treatedSolutions.indexOf(treatedAnswer); |
| 41 | + const isAnswerMatchingSolution = indexOfSolution !== -1; |
| 42 | + if (isAnswerMatchingSolution) { |
| 43 | + return AnswerStatus.OK; |
| 44 | + } |
| 45 | + return AnswerStatus.KO; |
| 46 | +} |
| 47 | + |
| 48 | +function _getAnswerStatusFromStringMatching(answer, solutions, deactivations, shouldApplyTreatments) { |
| 49 | + const enabledTreatments = getEnabledTreatments(shouldApplyTreatments, deactivations); |
| 50 | + const treatedAnswer = applyTreatments(applyPreTreatments(answer), enabledTreatments); |
| 51 | + const treatedSolutions = map(solutions, (solution) => applyTreatments(solution, enabledTreatments)); |
| 52 | + |
| 53 | + return validateAnswer(treatedAnswer, treatedSolutions, useLevenshteinRatio(enabledTreatments)) |
| 54 | + ? AnswerStatus.OK |
| 55 | + : AnswerStatus.KO; |
| 56 | +} |
0 commit comments