mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 14:09:31 +02:00
feat: swap-csv-columns
This commit is contained in:
26
src/utils/csv.ts
Normal file
26
src/utils/csv.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user