diff --git a/Scripts/tsvToJson.js b/Scripts/tsvToJson.js new file mode 100644 index 00000000..8a175b9b --- /dev/null +++ b/Scripts/tsvToJson.js @@ -0,0 +1,28 @@ +/** + { + "api":1, + "name":"TSV to JSON", + "description":"Converts TSV to JSON", + "author":"Quddus George", + "icon":"table", + "tags":"tab, tsv, json, table" + } +**/ + +//credit for tsv function: https://gist.github.com/iwek/7154706#gistcomment-3369283 + +function main(state) { + function tsvJSON(tsv) { + return tsv + .split("\n") + .map((line) => line.split("\t")) + .reduce((a, c, i, d) => { + if (i) { + const item = Object.fromEntries(c.map((val, j) => [d[0][j], val])); + return a ? [...a, item] : [item]; + } + }, []); + } + let json = JSON.stringify(tsvJSON(state.fullText)); + state.fullText = json; +}