Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(csvToJson): address CSV complexities #205

Merged
merged 3 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions Boop/Boop/scripts/CSVtoJSON.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
/**
{
"api":1,
"api":2,
"name":"CSV to JSON",
"description":"Converts comma-separated tables to JSON.",
"author":"Ivan",
"icon":"table",
"tags":"table,convert",
"bias": -0.2
"bias": -0.2
}
**/

// Inspired by https://github.com/30-seconds/30-seconds-of-code
const Papa = require('@boop/papaparse.js');

function main(state) {
try {
const delimiter = ','
const data = state.text
const titles = data.slice(0, data.indexOf('\n')).split(delimiter);
state.text = JSON.stringify(data
.slice(data.indexOf('\n') + 1)
.split('\n')
.map(v => {
const values = v.split(delimiter);
return titles.reduce((obj, title, index) => ((obj[title] = values[index]), obj), {});
}), null, 2);
}
catch(error) {
state.postError("Invalid CSV")
}


try {
const { data } = Papa.parse(state.text, { header:true });
state.text = JSON.stringify(data, null, 2);
}
catch(error) {
state.postError("Invalid CSV")
}
}
7 changes: 7 additions & 0 deletions Boop/Boop/scripts/lib/papaparse.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading