-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.vue
46 lines (43 loc) · 1.4 KB
/
parser.vue
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
<template>
<div>
<input type="file" accept="application/json,.csv" @change="loadContent">
<p>Manca ancora
gestione csv formattati strani, con virgole nei valori, con a capo di troppo, etc. <br><strong>necessario
che i csv abbiano gli
header</strong> (sennò come li mappi?)</p>
</div>
</template>
<script>
export default {
data() {
return {
floa: null,
is_csv: false,
parsedContent: null,
}
},
methods: {
loadContent(e) {
this.floa = e.target.files[0];
const reader = new FileReader();
reader.onload = (e) => this.outputContent(e);
reader.readAsText(this.floa);
this.is_csv = this.floa.name.endsWith('.csv');
},
outputContent(e) {
const content = e.target.result;
this.parsedContent = this.is_csv ? this.parseCSV(content) : JSON.parse(content);
this.$emit('contentParsed', this.parsedContent);
},
parseCSV(target) {
const content = target.split('\n').filter((i) => i);
const headers = content.shift().split(',');
return content.reduce((acc, i) => {
return [...acc, Object.assign(...i.split(',').map((k, i) => ({ [headers[i]]: k })))];
}, []);
}
}
}
</script>
<style scoped>
</style>