mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-20 06:29:32 +02:00
feat: csv-rows-to-columns
This commit is contained in:
34
src/pages/tools/csv/csv-rows-to-columns/service.ts
Normal file
34
src/pages/tools/csv/csv-rows-to-columns/service.ts
Normal 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');
|
||||
}
|
Reference in New Issue
Block a user