feat: csv-rows-to-columns

This commit is contained in:
Chesterkxng
2025-03-28 17:01:12 +00:00
parent 105a85a5ef
commit f3400dc12c
4 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
function compute(rows: string[][], columnCount: number): string[][] {
const result: string[][] = [];
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
for (let j = 0; j < columnCount; j++) {
if (!result[j]) {
result[j] = [];
}
result[j][i] = row[j];
}
}
return result;
}
export function csvRowsToColumns(
input: string,
emptyValuesFilling: boolean,
customFiller: string,
commentCharacter: string
): string {
const rows = input
.split('\n')
.map((row) => row.split(','))
.filter((row) => !row[0].trim().startsWith(commentCharacter));
const columnCount = Math.max(...rows.map((row) => row.length));
for (let i = 0; i < rows.length; i++) {
for (let j = 0; j < columnCount; j++) {
if (!rows[i][j]) {
rows[i][j] = emptyValuesFilling ? '' : customFiller;
}
}
}
const result = compute(rows, columnCount);
return result.join('\n');
}