I'm processing the data in chunks using WritableStream
. The decoded data is a json string and in case it starts with ,
I need to remove the comma.
But here's the problem, after the chunk is being decoded to string I'm checking the first character
const startsWithComma = chunk.at(0) === ','
and SOMETIMES it returns true although the chunk doesn't start with ,
and causes the JSON.parse
to fail later on. See the attached image.
Things I tried:
- used
.at()
alternatives like.charAt()
,.startsWith()
,chunk[0]
The issue is intermittent meaning sometimes it can process the entire data and sometimes might fail mid through.
CodePudding user response:
so, expanding on my comment:
from your image, is it possible that the debug is running after the comma was already taken out? is it also possible that the chunk may begin with more than 1 comma so sometimes debugging at that exact spot would still show a comma?
The solution would be to take off the commas using a while loop such as
while( chunk.at(0)===',' ){
chunk = chunk.slice(1).trim();
}
now I do not know the reason for doing it if isFirstChunk
so I'd leave that alone, but the above loop should solve your startsWithComma
issue :D