Created
June 7, 2018 20:48
-
-
Save colophonemes/ac673f9f8cfffa915f1080faee9db036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {allResponses} = inputData | |
const responsesRaw = allResponses | |
.split('###') | |
.filter(r => r) | |
.map(r => r.trim()) | |
const responses = {} | |
for (let responseRaw of responsesRaw) { | |
// split the question and answer at the first linebreak | |
let matches = responseRaw.match(/^(.*?)\n+(.*)/m) | |
if (matches) { | |
let question = matches[1] | |
let answer = (matches[2] || '').trim() | |
// if the question is formatted as "Blah (Actual Question)", | |
// strip out "Blah" and use "Actual Question" as the question, | |
// then append the result to the question set | |
const multipleChoiceRE = /^.*?\((.*?)\)$/ | |
if (multipleChoiceRE.test(question)) { | |
question = question.replace(multipleChoiceRE, '$1') | |
responses[question] = responses[question] || [] | |
responses[question].push(answer) | |
} else { | |
responses[question] = answer | |
} | |
} | |
} | |
Object.keys(responses).forEach(question => { | |
if (Array.isArray(responses[question])) { | |
responses[`${question} spaces`] = responses[question].join(', ') | |
responses[`${question} linebreaks`] = responses[question].join('\n') | |
responses[`${question} md`] = `- ${responses[question].join('\n- ')}` | |
} | |
}) | |
output = {...responses} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment