Skip to content

Commit e674f9f

Browse files
committed
Reset regex index to match from start of string
1 parent d734767 commit e674f9f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/msgAggregatorWorker.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export const parseSerialMessages = (
3333
// so we need to discard it and start aggregating from the first encountered separator
3434
let joinMessages = messages.join("");
3535
if (discardFirstLine) {
36+
separatorRegex.lastIndex = 0; // Reset lastIndex to ensure match happens from beginning of string
3637
const separatorMatch = separatorRegex.exec(joinMessages);
37-
if (separatorMatch && separatorMatch.index > -1) {
38+
if (separatorMatch && separatorMatch.index > -1) {
3839
joinMessages = joinMessages.substring(
3940
separatorMatch.index + separatorMatch[0].length
4041
);
@@ -54,6 +55,7 @@ export const parseSerialMessages = (
5455

5556
// remove the previous buffer
5657
buffer = "";
58+
separatorRegex.lastIndex = 0;
5759
// check if the last message contains the delimiter, if not, it's an incomplete string that needs to be added to the buffer
5860
if (!separatorRegex.test(messagesAndBuffer[messagesAndBuffer.length - 1])) {
5961
buffer = messagesAndBuffer[messagesAndBuffer.length - 1];
@@ -64,11 +66,12 @@ export const parseSerialMessages = (
6466
const parsedLines: { [key: string]: number }[] = [];
6567

6668
// for each line, explode variables
69+
separatorRegex.lastIndex = 0;
6770
messagesAndBuffer
68-
.filter((message) => !separatorRegex.test(message))
71+
.filter((message) => !separatorRegex.test(message))
6972
.forEach((message) => {
7073
const parsedLine: { [key: string]: number } = {};
71-
74+
7275
// Part Separator symbols i.e. Space, Tab & Comma are fully supported
7376
// SerialPlotter protocol specifies 3 message formats. The following 2 formats are supported
7477
// Value only format: <value1> <value2> <value3>

0 commit comments

Comments
 (0)