lukeed / trouter

:fish: A fast, small-but-mighty, familiar fish...errr, router*
MIT License
634 stars 23 forks source link

wildcard matches the whole path, but param['*'] only returns a segment #14

Closed hgl closed 3 years ago

hgl commented 4 years ago

In this example

const router = new Trouter();
router
  .get('/foo/*', _ => {})

let obj = router.find('GET', '/foo/a/b/c.js');
console.log(obj)

obj.params['*'] is "a", but I'd argue it should be a/b/c.js.

The example in regexparam's readme file also suggests it should be a/b/c.js:

function exec(path, result) {
    let i=0, out={};
    let matches = result.pattern.exec(path);
    while (i < result.keys.length) {
        out[ result.keys[i] ] = matches[++i] || null;
    }
    return out;
}

let foo = regexparam('/foo/*')
console.log(exec('/foo/a/b/c.js', foo));

outputs

{ wild: 'a/b/c.js' }

Is the current value intended?

lukeed commented 4 years ago

Hey, can you check params.wild -- wildcard globs are special-cased and written to wild always (as seen in your final snippet)

hgl commented 4 years ago

Thanks for the quick response.

I just realized I was testing on trouter v2.0.1, since that's what the current stable version of polka depends on.

I'll check if it's still a problem on v3.

BTW, is it possible to update polka to use trouter v3?

hgl commented 4 years ago

Verified, problem doesn't exist on v3.

lukeed commented 4 years ago

Polka@next is using trouter@3 -- trouter itself was breaking (use/wildcards were a big reason for it) however IIRC wildcards should still have their values sent to params.wild

hgl commented 4 years ago

params.wild doesn't exist on Polka@stable, only params['*'] does.

I'll check @next out, thanks a lot.

I'll leave the issue open for params.wild, feel free to close it.

lukeed commented 3 years ago

Closing as this is working as expected in trouter@3