radix3 icon indicating copy to clipboard operation
radix3 copied to clipboard

router lookup ** not matching ""

Open ToBinio opened this issue 4 months ago • 0 comments

Environment

node 20 and bun

Reproduction

const router = createRouter();

router.insert("/test/**:test", { payload: "wildcard" });

console.log([
  router.lookup("/test")
]);

output: [ null ]

expected: [ { payload: 'wildcard', params: { test: '' } } ]

Describe the bug

router does not match ** as nothing instead requires some value

Additional context

This does only happen when ** is not the only part.

/** does match / but /test/** does not match /test/

This behavior is not present in the matcher for the matcher there are also tests that check if ** matches ""

possible fix

I also played around a bit and tried fixing it myself and ended up with this addition to the lookup() function:

  if (node && node.data == null && node.wildcardChildNode) {
    node = node.wildcardChildNode;
    params[node.paramName || "_"] = "";
    paramsFound = true;
  }

tbh I do not completely understand how the lookup works that's why I am not opening a PR right now

Logs

No response

ToBinio avatar Mar 05 '24 16:03 ToBinio