qb-answer-checker
Version:
A package to automatically check/judge answers against quizbowl answerlines.
30 lines (24 loc) • 1.24 kB
JavaScript
import { DIRECTIVES_FLATTENED } from './constants.js';
/**
* Removes parentheses and square brackets from a string.
* @param {string} string - The input string.
* @returns {string} The string with parentheses and square brackets removed.
*/
function removeParentheses (string) {
return string.replace(/\([^)]*\)/g, '').replace(/\[[^\]]*\]/g, '');
}
/**
*
* @param {string} answerline
* @returns {string[]} An array of **answer sections**, where the first element is the **main answer section**.
*/
export default function splitIntoSections (answerline) {
const mainSection = removeParentheses(answerline);
let bracketSections = answerline.match(/\[[^\]]*(?=\])/g) ?? [];
bracketSections = bracketSections.map(section => section.slice(1));
let parenthesisSections = answerline.match(/\([^)]*(?=\))/g) ?? [];
parenthesisSections = parenthesisSections.map(section => section.slice(1));
bracketSections = bracketSections.filter(section => DIRECTIVES_FLATTENED.some(directive => section.startsWith(directive)));
parenthesisSections = parenthesisSections.filter(section => DIRECTIVES_FLATTENED.some(directive => section.startsWith(directive)));
return [mainSection, ...bracketSections, ...parenthesisSections];
}