Skip to content

Commit

Permalink
Skip ahead to relevant key -- broken
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdbeek committed Jan 14, 2025
1 parent c3d9ac7 commit 674920c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions client/src/components/Tool/extractEmbeddedJs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,18 @@ describe("extractEmbeddedJs", () => {
const result = extractEmbeddedJs(input);
expect(result).toEqual([]);
});

it("should skip shell_command prefix", () => {
const input = "shell_command: $(first)";
const regex = /shell_command:\s+/;
const result = extractEmbeddedJs(input, regex);
expect(result).toEqual([{ fragment: "$(first)", start: 15 }]);
});

it("should skip shell_command prefix and stop at next line", () => {
const input = "shell_command: $(first)\n$(second)";
const regex = /shell_command:(.*)$/m;
const result = extractEmbeddedJs(input, regex);
expect(result).toEqual([{ fragment: "$(first)", start: 15 }]);
});
});
16 changes: 12 additions & 4 deletions client/src/components/Tool/extractEmbeddedJs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
export function extractEmbeddedJs(inputString: string): { fragment: string; start: number }[] {
const matches: { fragment: string; start: number }[] = [];
export function extractEmbeddedJs(inputString: string, fieldRegex?: RegExp): { fragment: string; start: number }[] {
let i = 0;
let end = inputString.length;
if (fieldRegex) {
const fieldMatch = fieldRegex.exec(inputString);
if (fieldMatch && fieldMatch.length) {
i = fieldMatch.index;
end = i + fieldMatch[0].length;
}
}
const matches: { fragment: string; start: number }[] = [];

while (i < inputString.length) {
while (i < end) {
if (inputString[i] === "$" && inputString[i + 1] === "(") {
let depth = 0;
const startIndex = i;
i += 2; // Skip past "$("

while (i < inputString.length) {
while (i < end) {
if (inputString[i] === "(") {
depth++;
} else if (inputString[i] === ")") {
Expand Down

0 comments on commit 674920c

Please sign in to comment.