mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-09 09:55:07 +05:30
utils: adding delimiter flag for custom delimiters in csv
This commit is contained in:
parent
3d243665f3
commit
bafaccc25b
1 changed files with 9 additions and 4 deletions
|
|
@ -8,9 +8,10 @@ export function splitCsv(
|
||||||
input: string,
|
input: string,
|
||||||
deleteComment: boolean,
|
deleteComment: boolean,
|
||||||
commentCharacter: string,
|
commentCharacter: string,
|
||||||
deleteEmptyLines: boolean
|
deleteEmptyLines: boolean,
|
||||||
|
delimiter: string = ','
|
||||||
): string[][] {
|
): string[][] {
|
||||||
let rows = input.split('\n').map((row) => row.split(','));
|
let rows = input.split('\n').map((row) => row.split(delimiter));
|
||||||
|
|
||||||
// Remove comments if deleteComment is true
|
// Remove comments if deleteComment is true
|
||||||
if (deleteComment && commentCharacter) {
|
if (deleteComment && commentCharacter) {
|
||||||
|
|
@ -28,9 +29,13 @@ export function splitCsv(
|
||||||
/**
|
/**
|
||||||
* get the headers from a CSV string .
|
* get the headers from a CSV string .
|
||||||
* @param {string} input - The CSV input string.
|
* @param {string} input - The CSV input string.
|
||||||
|
* @param {string} csvSeparator - The character used to separate values in the CSV.
|
||||||
* @returns {string[]} - The CSV header as a 1D array.
|
* @returns {string[]} - The CSV header as a 1D array.
|
||||||
*/
|
*/
|
||||||
export function getCsvHeaders(csvString: string): string[] {
|
export function getCsvHeaders(
|
||||||
const rows = csvString.split('\n').map((row) => row.split(','));
|
csvString: string,
|
||||||
|
csvSeparator: string = ','
|
||||||
|
): string[] {
|
||||||
|
const rows = csvString.split('\n').map((row) => row.split(csvSeparator));
|
||||||
return rows.length > 0 ? rows[0].map((header) => header.trim()) : [];
|
return rows.length > 0 ? rows[0].map((header) => header.trim()) : [];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue