Skip to content
Myr

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

3 files+63 added−12 removed

modified:deno_dist/utils/url.ts+27 added-6 removed
121 unmodified lines
122 /*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 */127130 const base = match[1]131 const optional = base + match[2]132 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128 if (!path.match(/\:.+\?$/)) return null129130 const segments = path.split('/')131 const results: string[] = []132 let basePath = ''133134 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 })152153 return results.filter((v, i, a) => a.indexOf(v) === i)154}155156// Optimized157const _decodeURI = (value: string) => {
107 unmodified lines
modified:src/utils/url.test.ts+9 added
152 unmodified lines
153 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 })168169 describe('getQueryParam', () => {
99 unmodified lines
modified:src/utils/url.ts+27 added-6 removed
121 unmodified lines
122 /*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 */127130 const base = match[1]131 const optional = base + match[2]132 return [base === '' ? '/' : base.replace(/\/$/, ''), optional]128 if (!path.match(/\:.+\?$/)) return null129130 const segments = path.split('/')131 const results: string[] = []132 let basePath = ''133134 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 })152153 return results.filter((v, i, a) => a.indexOf(v) === i)154}155156// Optimized157const _decodeURI = (value: string) => {
107 unmodified lines