I’m working with CSV files and need to check newline character.
This function works fine:
function detectNewlineCharacter(csvContent)
{
if (csvContent.includes('rn')) {
return 'rn'; // Windows-style CRLF
} else if (csvContent.includes('n')) {
return 'n'; // Unix-style LF
} else if (csvContent.includes('r')) {
return 'r'; // Old Mac-style CR
} else {
return null; // No recognizable newline characters found
}
}
function fixCsv()
{
// ...code...
const newlineCharacter = detectNewlineCharacter(fileContent);
const rows = fileContent.split(newlineCharacter);
// ...code...
}
Problem:
csvContent
is very large. I need a method that stops immediately at the first found, just like .some()
for array. ChatGPT said .includes()
stops at the first found, but I’m no sure, can’t find that in the documentation. So does .match()
, .indexOf()
, etc.
My last resort would be to limit the string using .substring()
before searching.