All files / src/question-formats qal.ts

100% Statements 15/15
100% Branches 4/4
100% Functions 7/7
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 394x     4x 12x   2x 8x 8x           2x           4x 8x 2x           4x       1x     1x      
import { Question, createChoice, createQuestion } from '../question'
import { QuestionFormat } from './format'
 
const parseQuestion = (text: string) => {
  const [question, ...answers] = text.split('\n').filter((line) => !line.startsWith('//'))
 
  const choices = answers.map((answer) => {
    const isCorrect = answer.startsWith('*')
    return createChoice({
      answer: (isCorrect ? answer.slice(1) : answer).trim(),
      isCorrect,
    })
  })
 
  return createQuestion({
    question: question.trim(),
    choices,
  })
}
 
const formatQuestion = (question: Question, idx: number) => {
  const answers = question.choices.map((answer) => (answer.isCorrect ? `*${answer.answer}` : ` ${answer.answer}`))
  return `// question ${idx + 1}\n${question.question}\n${answers.join('\n')}`
}
 
/**
 * QuestionAnswerLine format
 */
export const qal: QuestionFormat = {
  fileType: 'text/plain',
  fileExtension: 'txt',
  import: (text: string): Question[] => {
    return text.split('\n\n').map(parseQuestion)
  },
  export: (questions: Question[]) => {
    return questions.map(formatQuestion).join('\n\n')
  },
}