Skip to content
Myr

fix(route): support a root-level optional parameter (#975)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(route): support a root level optional parameter * denoify

authored by Yusuke Wada · committed by GitHub · parent d273210 · Browse files · 3 files+5 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+5 added−4 removed

  • tests:+1 added−0 removed
  • other:+4 added−4 removed

3 files+5 added−4 removed

modified:deno_dist/utils/url.ts+2 added-2 removed
123 unmodified lines
124 If path is `/api/animals/:type?` it will return:125 [`/api/animals`, `/api/animals/:type`]126 in other cases it will return null127 */128 const match = path.match(/(^.+)(\/\:[^\/]+)\?$/)128 const match = path.match(/^(.+|)(\/\:[^\/]+)\?$/)129 if (!match) return null130131 const base = match[1]132 const optional = base + match[2]133 return [base, optional]133 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]134}135136const removeFragment = (queryString: string): string => {137 const fragIndex = queryString.indexOf('#')
64 unmodified lines
modified:src/utils/url.test.ts+1 added
146 unmodified lines
147 expect(checkOptionalParameter('/api/animals/:type')).toBeNull()148 expect(checkOptionalParameter('/api/animals')).toBeNull()149 expect(checkOptionalParameter('/api/:animals?/type')).toBeNull()150 expect(checkOptionalParameter('/api/animals/:type?/')).toBeNull()151 expect(checkOptionalParameter('/:optional?')).toEqual(['/', '/:optional'])152 })153 })154155 describe('getQueryParam', () => {
47 unmodified lines
modified:src/utils/url.ts+2 added-2 removed
123 unmodified lines
124 If path is `/api/animals/:type?` it will return:125 [`/api/animals`, `/api/animals/:type`]126 in other cases it will return null127 */128 const match = path.match(/(^.+)(\/\:[^\/]+)\?$/)128 const match = path.match(/^(.+|)(\/\:[^\/]+)\?$/)129 if (!match) return null130131 const base = match[1]132 const optional = base + match[2]133 return [base, optional]133 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]134}135136const removeFragment = (queryString: string): string => {137 const fragIndex = queryString.indexOf('#')
64 unmodified lines