diff --git a/CsvToJsonConverter/nikunj/README.md b/CsvToJsonConverter/nikunj/README.md new file mode 100644 index 000000000..1e5707ff4 --- /dev/null +++ b/CsvToJsonConverter/nikunj/README.md @@ -0,0 +1,23 @@ + +## Project Information + +This is a small project in which a csv file of any size data is being converted into JSON format and all this task is being done in the worker thread not on the main thread you can look into the code for more details + + + +## Use +- Open index.html with live server + +- Select file by clicking on Choose file button and the result will be displayed in your page.Make sure the file is not corrupted + + +### Tested Platform +- chrome + +- FireFox + +### Screenshot + + + + diff --git a/CsvToJsonConverter/nikunj/fileWorker.js b/CsvToJsonConverter/nikunj/fileWorker.js new file mode 100644 index 000000000..038a13b9a --- /dev/null +++ b/CsvToJsonConverter/nikunj/fileWorker.js @@ -0,0 +1,24 @@ +onmessage = (mssg) => { + let f = mssg.data; + let fs = new FileReader(); + fs.readAsBinaryString(f); + let jsonOutput = []; + let headers = []; + fs.onloadend = function (e) { + const rows = e.target.result.split("\r\n"); + for (let i = 0; i < rows.length; i++) { + var cells = rows[i].split(","); + let obj = {}; + + for (let j = 0; j < cells.length; j++) { + if (i === 0) { + headers.push(cells[j]); + } else { + obj[headers[j]] = cells[j]; + } + } + jsonOutput.push(obj); + } + postMessage(JSON.stringify(jsonOutput)); + }; +}; diff --git a/CsvToJsonConverter/nikunj/index.html b/CsvToJsonConverter/nikunj/index.html new file mode 100644 index 000000000..043da6466 --- /dev/null +++ b/CsvToJsonConverter/nikunj/index.html @@ -0,0 +1,7 @@ + +
+ + + + + diff --git a/CsvToJsonConverter/nikunj/main.js b/CsvToJsonConverter/nikunj/main.js new file mode 100644 index 000000000..cb22ac7ee --- /dev/null +++ b/CsvToJsonConverter/nikunj/main.js @@ -0,0 +1,11 @@ +const fileWorker = new Worker("fileWorker.js"); +fileWorker.onmessage = (mssg) => { + document.getElementById("rem").innerHTML = mssg.data; +}; +document.getElementById("fileP").addEventListener("change", function () { + let re = new FileReader(); + re.onload = function () { + fileWorker.postMessage(document.getElementById("fileP").files[0]); + }; + re.readAsText(this.files[0]); +});