Created
December 7, 2023 00:18
-
-
Save guerrerocarlos/28263ee86bf8a9f945a3caa718c1873c 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: string | number) { | |
// 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 | |
} | |
} | |
export const flattenObj = (results: any, parentKey: string, obj: any): any => { | |
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: any) => typeof item === "string" || typeof item === "number") | |
results[newKey] = results[newKey] || []; | |
results[newKey] = results[newKey].concat(numbersOrStrings.map(trimNewlinesAndSpaces)) | |
let otherObjects = obj[key].filter((item: any) => typeof item !== "string" && typeof item !== "number") | |
otherObjects.map(flattenObj.bind(null, results, newKey)) | |
} | |
} | |
} | |
return results; | |
}; | |
export const flatObjectToCsvLines = (opts: any, data: any) => { | |
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) | |
} | |
if(opts.skipHeader) { | |
lines.shift() | |
} | |
return lines.join("\n") | |
} | |
export function getMaxHeaders(objects: any[]) { | |
let allHeaders = [] as string[] | |
for (let obj of objects) { | |
let headers = Object.keys(obj) | |
for (let header of headers) { | |
if (!allHeaders.includes(header)) { | |
allHeaders.push(header) | |
} | |
} | |
} | |
return allHeaders | |
} | |
export function tidyObject(allHeaders: string[], obj: any) { | |
let newObj = {} as any | |
for(let header of allHeaders) { | |
if(!obj[header]) { | |
newObj[header] = [] | |
} else { | |
newObj[header] = obj[header] | |
} | |
} | |
return newObj | |
} | |
if (require.main === module) { | |
let input1 = { | |
"name": "John", | |
"age": 30, | |
"cars": [{ | |
"brand": "Ford", | |
"km": 5000, | |
}] | |
} | |
let input2 = { | |
"name": "Mari", | |
"age": 31, | |
"pets": [ | |
{ | |
"animal": "dog", | |
"name": "Fido" | |
}, | |
{ | |
"animal": "cat", | |
"name": "Felix" | |
} | |
] | |
} | |
let flatenedObjects = [] | |
flatenedObjects.push(flattenObj({}, "", input1)) | |
flatenedObjects.push(flattenObj({}, "", input2)) | |
// console.log("flatenedObjects", flatenedObjects) | |
let maxHeaders = getMaxHeaders(flatenedObjects) | |
// console.log("maxHeaders", maxHeaders) | |
flatenedObjects = flatenedObjects.map(tidyObject.bind(null, maxHeaders)) | |
console.log(flatenedObjects) | |
console.log(flatenedObjects.map(flatObjectToCsvLines)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment