feat: swap-csv-columns

This commit is contained in:
Chesterkxng
2025-03-31 18:08:22 +00:00
parent dd8ed76dcf
commit cafddb7cbf
6 changed files with 566 additions and 1 deletions

26
src/utils/csv.ts Normal file
View File

@@ -0,0 +1,26 @@
/**
* Splits a CSV string into rows, skipping any blank lines.
* @param {string} input - The CSV input string.
* @param {string} commentCharacter - The character used to denote comments.
* @returns {string[][]} - The CSV rows as a 2D array.
*/
export function splitCsv(
input: string,
deleteComment: boolean,
commentCharacter: string,
deleteEmptyLines: boolean
): string[][] {
let rows = input.split('\n').map((row) => row.split(','));
// Remove comments if deleteComment is true
if (deleteComment) {
rows = rows.filter((row) => !row[0].trim().startsWith(commentCharacter));
}
// Remove empty lines if deleteEmptyLines is true
if (deleteEmptyLines) {
rows = rows.filter((row) => row.some((cell) => cell.trim() !== ''));
}
return rows;
}