Created
June 1, 2018 17:16
-
-
Save mrded/e6b7e7d73e7048f2a58e96c9669cb3ea to your computer and use it in GitHub Desktop.
Export test to CSV files.
This file contains hidden or 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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const csvLine = require('csv-line'); | |
const readFile = function(file) { | |
return new Promise(function(resolve, reject) { | |
fs.readFile(file, 'utf8', function(err, data) { | |
err ? reject(err) : resolve(JSON.parse(data)); | |
}); | |
}); | |
}; | |
const concat = function(items, attachments = {}) { | |
const output = items.map(function(item) { | |
switch (item.type) { | |
case 'text': | |
return item.data; | |
case 'textList': | |
return item.data.join(' '); | |
case 'attachment': | |
return concat([attachments[item.data]], attachments); | |
} | |
}); | |
return output.join(' '); | |
}; | |
const getQuestions = function(test) { | |
const attachments = test.attachments; | |
return test.questions.map(function(question, id) { | |
return { | |
id: id, | |
question: concat(question.question, attachments), | |
explanation: concat(question.explanation, attachments), | |
answer: question.answers.reduce(function(output, answer) { | |
return answer.correct ? answer.value : output; | |
}, '') | |
}; | |
}); | |
}; | |
const printQuestions = function(questions) { | |
const header = Object.keys(questions[0]); | |
const items = []; | |
console.log(csvLine.encode(header)); | |
for (question of questions) { | |
let item = header.map(function(key) { | |
return question[key]; | |
}); | |
console.log(csvLine.encode(item)); | |
} | |
}; | |
readFile(process.argv[2]).then(function(test) { | |
const questions = getQuestions(test); | |
printQuestions(questions); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment