-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestjava.js
More file actions
47 lines (40 loc) · 1007 Bytes
/
testjava.js
File metadata and controls
47 lines (40 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const fs = require('fs');
const csvFile = 'GunCatalog.csv';
fs.readFile(csvFile, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
const lines = data.split('\n');
lines.forEach((line) => {
const gun = parseCsvLine(line);
console.log(`{
'gun_name': '${gun[0]}',
gun_img: '${gun[1]}',
type: '${gun[2]}',
size: '${gun[3]}',
colors: '${gun[4]}',
price: '${gun[5]}',
description: '${gun[6]}',
ammo: '${gun[7]}',
availability: '${gun[8]}'
}`);
});
});
function parseCsvLine(line) {
const values = [];
let current = '';
let insideQuotes = false;
for (const char of line) {
if (char === '"') {
insideQuotes = !insideQuotes;
} else if (char === ',' && !insideQuotes) {
values.push(current.trim());
current = '';
} else {
current += char;
}
}
values.push(current.trim());
return values;
}