conversikit
Advanced tools
Comparing version 1.8.3 to 1.8.4
55
index.js
@@ -156,23 +156,60 @@ const fs = require('fs').promises; | ||
} | ||
let data = await fs.readFile(path, 'utf8'); | ||
// console.log("data ==== ", data) | ||
// Split the CSV data into an array of rows | ||
const rows = data.split('\n'); | ||
const header = rows[0]; | ||
const header = rows[0].split(','); | ||
//let create a json data for this csv file | ||
let jsonArray = []; | ||
for (let i = 1; i < rows.length; i++) { | ||
for (let i = 0; i < rows.length; i++) { | ||
let csvData = rows[i].split(','); | ||
// console.log("csvData == ", csvData) | ||
let obj = {}; | ||
for (let j = 0; j < header.length; j++) { | ||
if (header.split(",")[j]) { | ||
obj[header.split(",")[j]] = csvData[j] || ""; | ||
let concatData = ""; | ||
let dataOccur = 0; | ||
let occure = false; | ||
for (let j = 0; j < csvData.length; j++) { | ||
// console.log("==========================================================") | ||
// console.log("csvData length === ", csvData.length); | ||
// console.log("data occure = ", dataOccur); | ||
// console.log("header length = ", header[j - dataOccur]) | ||
// console.log("--------------------------------------------------------") | ||
if (occure) { | ||
if (csvData[j].endsWith('"')) { | ||
concatData += csvData[j]; | ||
obj[header[j - dataOccur]] = concatData || ""; | ||
concatData = ""; | ||
occure = false; | ||
} else { | ||
dataOccur++; | ||
concatData += csvData[j] + ","; | ||
} | ||
continue; | ||
} | ||
if (csvData[j].startsWith('"')) { | ||
// console.log("data == ", csvData[j]) | ||
concatData += csvData[j] + ","; | ||
dataOccur++; | ||
occure = true; | ||
// console.log("oc json -- ", obj); | ||
continue; | ||
} | ||
if (header[j - dataOccur]) { | ||
obj[header[j - dataOccur]] = csvData[j] || ""; | ||
} | ||
// console.log("ppp json -- ", obj); | ||
} | ||
if (obj) jsonArray.push(obj) | ||
} | ||
// console.log("allll -------> ", jsonArray) | ||
return jsonArray; | ||
} catch (error) { | ||
@@ -179,0 +216,0 @@ throw error; |
{ | ||
"name": "conversikit", | ||
"version": "1.8.3", | ||
"version": "1.8.4", | ||
"description": "convert anything", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
11233
182