mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-19 05:59:34 +02:00
27 lines
777 B
TypeScript
27 lines
777 B
TypeScript
/**
|
|
* 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;
|
|
}
|