Skip to content

Commit

Permalink
Move max iteration check after initialize
Browse files Browse the repository at this point in the history
Sometimes there an iteration step that doesn't move either cursor forward.

E.g. when returning to initialize after identifying one of the parameters is complete.
  • Loading branch information
JAForbes committed Feb 11, 2024
1 parent ee940f2 commit a8e1937
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/fromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,10 @@ export function safe(
let mode: Mode = "initialize";
let prevMode: Mode = "initialize";
let score = 0;
const maxIterations = path.length + pattern.length + 2;
const maxIterations = path.length + pattern.length;
let iterations = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
if (iterations >= maxIterations) {
throw new Error(
`Unexpected recursive logic while parsing path '${_path}' using pattern '${_pattern}'`
);
}
iterations++;

if (mode == "initialize") {
while (pattern[patternI] === "/") {
patternI++;
Expand Down Expand Up @@ -129,6 +122,13 @@ export function safe(
}
}

if (iterations >= maxIterations) {
throw new Error(
`Unexpected recursive logic while parsing path '${_path}' using pattern '${_pattern}'`
);
}
iterations++;

if (
(mode == "collectLiteralName" || mode == "collectVarName") &&
pattern[patternI] === "/"
Expand Down
15 changes: 11 additions & 4 deletions test/fromPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,16 @@ test("fromPath: complex", () => {

test("fromPath: odin", () => {

const res = safe("/data/schedules", "/data/schedules/:schedule_id")
{
const res = safe("/data/schedules", "/data/schedules/:schedule_id")

assert( res.tag == 'Left')
assert.match(res.value.message, /variable ':schedule_id'/)
}

assert( res.tag == 'Left')
assert.match(res.value.message, /variable ':schedule_id'/)
console.log(res)
{
const res = safe('/data/schedules', '/data/schedules/create')
assert( res.tag == 'Left')
assert.match(res.value.message, /literal path segment '\/create'/)
}
})

0 comments on commit a8e1937

Please sign in to comment.