Created
December 7, 2023 00:17
-
-
Save guerrerocarlos/64cdd49f04dcaf7e305f820c8a073711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trimNewlinesAndSpaces(inputString) { | |
// Remove newlines and replace multiple spaces with a single space | |
if (typeof inputString === "string") { | |
return inputString.replace(/\n/g, '').replace(/ +/g, ' ').replace(/\,/g, ''); | |
} else { | |
return "" + inputString | |
} | |
} | |
const flattenObj = (results, parentKey, obj) => { | |
for (let key in obj) { | |
// console.log("key> ", key, obj[key], typeof obj[key]) | |
let newKey = parentKey ? `${parentKey}.${key}` : key; | |
if (typeof obj[key] === "string" || typeof obj[key] === "number") { | |
results[newKey] = results[newKey] || []; | |
results[newKey].push(trimNewlinesAndSpaces(obj[key])) | |
} else { | |
if (Array.isArray(obj[key])) { | |
let numbersOrStrings = obj[key].filter(item => typeof item === "string" || typeof item === "number") | |
results[newKey] = results[newKey] || []; | |
results[newKey] = results[newKey].concat(numbersOrStrings.map(trimNewlinesAndSpaces)) | |
let otherObjects = obj[key].filter(item => typeof item !== "string" && typeof item !== "number") | |
otherObjects.map(flattenObj.bind(null, results, newKey)) | |
} | |
} | |
} | |
return results; | |
}; | |
const objectToCsv = (data) => { | |
let longestArray = -1 | |
for (let key in data) { | |
if (data[key].length > longestArray) { | |
longestArray = data[key].length + 1 | |
} | |
} | |
let columns = [] | |
for (let key in data) { | |
if (data[key].length > 0) { | |
let row = new Array(longestArray).fill('') | |
row[0] = key | |
for (let i = 0; i < data[key].length; i++) { | |
row[i + 1] = data[key][i] | |
} | |
columns.push(row) | |
} | |
} | |
let lines = [] | |
for (let a = 0; a < longestArray; a++) { | |
let line = [] | |
for (let j = 0; j < columns.length; j++) { | |
line.push(columns[j][a]) | |
} | |
lines.push(line) | |
} | |
console.log(lines.join("\n")) | |
} | |
if (require.main === module) { | |
// Usage example | |
let inputData = { | |
name: "John", | |
age: 30, | |
hobbies: ["reading", "games", "coding"], | |
experience: [{ | |
companyName: "Google", | |
position: "Software Engineer", | |
years: 2 | |
}, | |
{ | |
companyName: "Amazon", | |
position: "QA Engineer", | |
years: 3 | |
}] | |
}; | |
let output = [ | |
"name, John", | |
"age, 30", | |
"hobbies, reading, games, coding", | |
"experience.companyName, Google, Amazon", | |
"experience.position, Software Engineer, QA Engineer", | |
"experience.years, 2, 3", | |
] | |
const outputData = flattenObj({}, "", inputData); | |
console.log("outputData", outputData) | |
objectToCsv(outputData) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment