Skip to content
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
23 changes: 23 additions & 0 deletions CsvToJsonConverter/nikunj/README.md
Original file line number Diff line number Diff line change
@@ -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

![Screenshot from 2023-10-06 03-26-25](https://github.com/Nikunj-bisht/javascript-projects/assets/52692588/f942b4ae-4393-4182-91f8-392b403ea931)


24 changes: 24 additions & 0 deletions CsvToJsonConverter/nikunj/fileWorker.js
Original file line number Diff line number Diff line change
@@ -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));
};
};
7 changes: 7 additions & 0 deletions CsvToJsonConverter/nikunj/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<input type="file" id="fileP" />
<h1 id="rem"></h1>
</body>
<script src="./main.js"></script>
</html>
11 changes: 11 additions & 0 deletions CsvToJsonConverter/nikunj/main.js
Original file line number Diff line number Diff line change
@@ -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]);
});