fix(utils/url): Fix behavior when parameters with no value specified are mixed. (#1023)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* refactor(utils/url): Reduce a few characters. * fix(utils/url): Fix behavior when parameters with no value specified are mixed. * chore: denoify
authored by Taku Amano · committed by GitHub · parent 5f4eac8 · Browse files · 3 files·+42 added−14 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+42 added−14 removed
- tests:+8 added−0 removed
- other:+34 added−14 removed
Provenance
- Commit
0560f27- Tree
f4e4d38- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
5f4eac8
3 files changed+42 added−14 removed
modified:deno_dist/utils/url.ts+17 added-7 removed
155 unmodified lines156 const trailingKeyCode = url.charCodeAt(keyIndex + key.length + 1)157 if (trailingKeyCode === 61) {158 const valueIndex = keyIndex + key.length + 2159 const endIndex = url.indexOf('&', valueIndex)160- const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)161- return _decodeURI(value)160+ return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex))161 } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {162 return ''163 }164 keyIndex = url.indexOf(`&${key}`, keyIndex)10 unmodified lines175 encoded ??= /[%+]/.test(url)176 177 let keyIndex = url.indexOf('?', 8)178 while (keyIndex !== -1) {180- const valueIndex = url.indexOf('=', keyIndex)181- let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)179+ const nextKeyIndex = url.indexOf('&', keyIndex + 1)180+ let valueIndex = url.indexOf('=', keyIndex)181+ if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {182+ valueIndex = -1183+ }184+ let name = url.slice(185+ keyIndex + 1,186+ valueIndex === -1 ? (nextKeyIndex === -1 ? undefined : nextKeyIndex) : valueIndex187+ )188 if (encoded) {189 name = _decodeURI(name)190 }191 192+ keyIndex = nextKeyIndex193+194+ if (name === '') {195+ continue196+ }197+198 let value199 if (valueIndex === -1) {188- keyIndex = -1200 value = ''201 } else {191- keyIndex = url.indexOf('&', valueIndex)192- value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)202+ value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex)203 if (encoded) {204 value = _decodeURI(value)205 }206 }25 unmodified linesmodified:src/utils/url.test.ts+8 added
162 unmodified lines163 name: 'hey',164 age: '20',165 tall: '170',166 })167+ expect(getQueryParam('http://example.com/?pretty&&&&q=1%2b1=2')).toEqual({168+ pretty: '',169+ q: '1+1=2',170+ })171 expect(getQueryParam('http://example.com/?pretty', 'pretty')).toBe('')172 expect(getQueryParam('http://example.com/?pretty', 'prtt')).toBe(undefined)173 expect(getQueryParam('http://example.com/?name=sam&name=tom', 'name')).toBe('sam')174 expect(getQueryParam('http://example.com/?name=sam&name=tom')).toEqual({46 unmodified lines221 name: ['hey', 'foo'],222 age: ['20', '30'],223 tall: ['170', '180'],224 })225+ expect(getQueryParams('http://example.com/?pretty&&&&q=1%2b1=2&q=2%2b2=4')).toEqual({226+ pretty: [''],227+ q: ['1+1=2', '2+2=4'],228+ })229 expect(getQueryParams('http://example.com/?pretty', 'pretty')).toEqual([''])230 expect(getQueryParams('http://example.com/?pretty', 'prtt')).toBe(undefined)231 })232 })1 unmodified linemodified:src/utils/url.ts+17 added-7 removed
155 unmodified lines156 const trailingKeyCode = url.charCodeAt(keyIndex + key.length + 1)157 if (trailingKeyCode === 61) {158 const valueIndex = keyIndex + key.length + 2159 const endIndex = url.indexOf('&', valueIndex)160- const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)161- return _decodeURI(value)160+ return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex))161 } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {162 return ''163 }164 keyIndex = url.indexOf(`&${key}`, keyIndex)10 unmodified lines175 encoded ??= /[%+]/.test(url)176 177 let keyIndex = url.indexOf('?', 8)178 while (keyIndex !== -1) {180- const valueIndex = url.indexOf('=', keyIndex)181- let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)179+ const nextKeyIndex = url.indexOf('&', keyIndex + 1)180+ let valueIndex = url.indexOf('=', keyIndex)181+ if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {182+ valueIndex = -1183+ }184+ let name = url.slice(185+ keyIndex + 1,186+ valueIndex === -1 ? (nextKeyIndex === -1 ? undefined : nextKeyIndex) : valueIndex187+ )188 if (encoded) {189 name = _decodeURI(name)190 }191 192+ keyIndex = nextKeyIndex193+194+ if (name === '') {195+ continue196+ }197+198 let value199 if (valueIndex === -1) {188- keyIndex = -1200 value = ''201 } else {191- keyIndex = url.indexOf('&', valueIndex)192- value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)202+ value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex)203 if (encoded) {204 value = _decodeURI(value)205 }206 }25 unmodified lines