Skip to content

Commit

Permalink
Create csv.js
Browse files Browse the repository at this point in the history
  • Loading branch information
inventionpro authored Nov 3, 2024
1 parent ca3ef1d commit 22922e0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions media/parsers/csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function fromObjecrWithPrefix(obj, prefix) {
return {};
}

export function fromObject(obj) {
let csv = 'id,'+Object.keys(obj).join(',');
return csv;
}

export function toObject(csv) {
let obj = {};
let lines = csv.split('\n');
let names = [];
lines[0].split(',').slice(1, lines[0].split(',').length).map(l => {obj[l] = {};names.push(l)});
lines.slice(1, lines.length).map(l => {
let v = l.split(',');
for (let i = 1; i<v.length; i++) {
obj[names[i-1]] = v[i];
}
})
return obj;
}

0 comments on commit 22922e0

Please sign in to comment.