-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate-overview-from-responses.js
75 lines (64 loc) · 1.81 KB
/
generate-overview-from-responses.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs')
const path = require('path')
const csvjson = require('csvjson')
const csvJsonOptions = {
delimiter: ',',
quote: ''
}
const questionsData = require('./data/questions.json')
const OUTPUT_FILENAME = '2018.json'
fs.readFile(path.join(__dirname, 'responses.csv'), (error, results) => {
if (error) {
return console.log(err)
}
const data = results.toString()
// const responses = csvjson.toObject(data, csvJsonOptions)
const responses = require('./responses.json')
// Remove empty results
const strippedResponses =
responses
.filter((response) => {
const emptyResponse = (
Object
.entries(response)
.map(response => {
return response[1]
})
.join('')
.length
) === 0
return !emptyResponse
})
let answersOverview = questionsData
strippedResponses.forEach(response => {
Object.keys(response).forEach(question => {
const questionKey = response[question]
if (!questionKey) {
return
}
const matchedQuestion = answersOverview.find(answer => {
return answer.id == question
})
if (matchedQuestion) {
matchedQuestion.answers = (matchedQuestion.answers || {})
const keys = questionKey.split(',').map(key => {
return key.trim()
})
keys.forEach(key => {
if(matchedQuestion.answers[key]) {
matchedQuestion.answers[key] += 1
} else {
matchedQuestion.answers[key] = 1
}
})
}
})
})
const output = JSON.stringify(answersOverview, null, 2)
fs.writeFile(`data/${OUTPUT_FILENAME}`, output, (error) => {
if (error) {
return console.log(err)
}
console.log(`data/${OUTPUT_FILENAME} updated`)
})
})