feat: support multiple optinal parameters (#1940)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat: support multiple optinal parameters * fix && denoify
authored by Yusuke Wada · committed by GitHub · parent d469ed9 · Browse files · 3 files·+63 added−12 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+63 added−12 removed
- tests:+9 added−0 removed
- other:+54 added−12 removed
Provenance
- Commit
ff34b1b- Tree
169fdad- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d469ed9
3 files changed+63 added−12 removed
modified:deno_dist/utils/url.ts+27 added-6 removed
121 unmodified lines122 /*123 If path is `/api/animals/:type?` it will return:124 [`/api/animals`, `/api/animals/:type`]125 in other cases it will return null126- */127- const match = path.match(/^(.+|)(\/\:[^\/]+)\?$/)128- if (!match) return null126+ */127 130- const base = match[1]131- const optional = base + match[2]132- return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128+ if (!path.match(/\:.+\?$/)) return null129+130+ const segments = path.split('/')131+ const results: string[] = []132+ let basePath = ''133+134+ segments.forEach((segment) => {135+ if (segment !== '' && !/\:/.test(segment)) {136+ basePath += '/' + segment137+ } else if (/\:/.test(segment)) {138+ if (/\?/.test(segment)) {139+ if (results.length === 0 && basePath === '') {140+ results.push('/')141+ } else {142+ results.push(basePath)143+ }144+ const optionalSegment = segment.replace('?', '')145+ basePath += '/' + optionalSegment146+ results.push(basePath)147+ } else {148+ basePath += '/' + segment149+ }150+ }151+ })152+153+ return results.filter((v, i, a) => a.indexOf(v) === i)154 }155 156 // Optimized157 const _decodeURI = (value: string) => {107 unmodified linesmodified:src/utils/url.test.ts+9 added
152 unmodified lines153 expect(checkOptionalParameter('/api/animals')).toBeNull()154 expect(checkOptionalParameter('/api/:animals?/type')).toBeNull()155 expect(checkOptionalParameter('/api/animals/:type?/')).toBeNull()156 expect(checkOptionalParameter('/:optional?')).toEqual(['/', '/:optional'])157+ expect(checkOptionalParameter('/v1/leaderboard/:version?/:platform?')).toEqual([158+ '/v1/leaderboard',159+ '/v1/leaderboard/:version',160+ '/v1/leaderboard/:version/:platform',161+ ])162+ expect(checkOptionalParameter('/api/:version/animal/:type?')).toEqual([163+ '/api/:version/animal',164+ '/api/:version/animal/:type',165+ ])166 })167 })168 169 describe('getQueryParam', () => {99 unmodified linesmodified:src/utils/url.ts+27 added-6 removed
121 unmodified lines122 /*123 If path is `/api/animals/:type?` it will return:124 [`/api/animals`, `/api/animals/:type`]125 in other cases it will return null126- */127- const match = path.match(/^(.+|)(\/\:[^\/]+)\?$/)128- if (!match) return null126+ */127 130- const base = match[1]131- const optional = base + match[2]132- return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128+ if (!path.match(/\:.+\?$/)) return null129+130+ const segments = path.split('/')131+ const results: string[] = []132+ let basePath = ''133+134+ segments.forEach((segment) => {135+ if (segment !== '' && !/\:/.test(segment)) {136+ basePath += '/' + segment137+ } else if (/\:/.test(segment)) {138+ if (/\?/.test(segment)) {139+ if (results.length === 0 && basePath === '') {140+ results.push('/')141+ } else {142+ results.push(basePath)143+ }144+ const optionalSegment = segment.replace('?', '')145+ basePath += '/' + optionalSegment146+ results.push(basePath)147+ } else {148+ basePath += '/' + segment149+ }150+ }151+ })152+153+ return results.filter((v, i, a) => a.indexOf(v) === i)154 }155 156 // Optimized157 const _decodeURI = (value: string) => {107 unmodified lines