0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-29 17:46:30 +01:00

fix(utils/url): Fix behavior when parameters with no value specified are mixed. (#1023)

* refactor(utils/url): Reduce a few characters.

* fix(utils/url): Fix behavior when parameters with no value specified are mixed.

* chore: denoify
This commit is contained in:
Taku Amano 2023-03-31 17:57:24 +09:00 committed by GitHub
parent 5f4eac8466
commit 0560f27bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 14 deletions

View File

@ -157,8 +157,7 @@ const _getQueryParam = (
if (trailingKeyCode === 61) {
const valueIndex = keyIndex + key.length + 2
const endIndex = url.indexOf('&', valueIndex)
const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)
return _decodeURI(value)
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex))
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
return ''
}
@ -177,19 +176,30 @@ const _getQueryParam = (
let keyIndex = url.indexOf('?', 8)
while (keyIndex !== -1) {
const valueIndex = url.indexOf('=', keyIndex)
let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)
const nextKeyIndex = url.indexOf('&', keyIndex + 1)
let valueIndex = url.indexOf('=', keyIndex)
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
valueIndex = -1
}
let name = url.slice(
keyIndex + 1,
valueIndex === -1 ? (nextKeyIndex === -1 ? undefined : nextKeyIndex) : valueIndex
)
if (encoded) {
name = _decodeURI(name)
}
keyIndex = nextKeyIndex
if (name === '') {
continue
}
let value
if (valueIndex === -1) {
keyIndex = -1
value = ''
} else {
keyIndex = url.indexOf('&', valueIndex)
value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex)
if (encoded) {
value = _decodeURI(value)
}

View File

@ -164,6 +164,10 @@ describe('url', () => {
age: '20',
tall: '170',
})
expect(getQueryParam('http://example.com/?pretty&&&&q=1%2b1=2')).toEqual({
pretty: '',
q: '1+1=2',
})
expect(getQueryParam('http://example.com/?pretty', 'pretty')).toBe('')
expect(getQueryParam('http://example.com/?pretty', 'prtt')).toBe(undefined)
expect(getQueryParam('http://example.com/?name=sam&name=tom', 'name')).toBe('sam')
@ -218,6 +222,10 @@ describe('url', () => {
age: ['20', '30'],
tall: ['170', '180'],
})
expect(getQueryParams('http://example.com/?pretty&&&&q=1%2b1=2&q=2%2b2=4')).toEqual({
pretty: [''],
q: ['1+1=2', '2+2=4'],
})
expect(getQueryParams('http://example.com/?pretty', 'pretty')).toEqual([''])
expect(getQueryParams('http://example.com/?pretty', 'prtt')).toBe(undefined)
})

View File

@ -157,8 +157,7 @@ const _getQueryParam = (
if (trailingKeyCode === 61) {
const valueIndex = keyIndex + key.length + 2
const endIndex = url.indexOf('&', valueIndex)
const value = url.slice(valueIndex, endIndex === -1 ? undefined : endIndex)
return _decodeURI(value)
return _decodeURI(url.slice(valueIndex, endIndex === -1 ? undefined : endIndex))
} else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
return ''
}
@ -177,19 +176,30 @@ const _getQueryParam = (
let keyIndex = url.indexOf('?', 8)
while (keyIndex !== -1) {
const valueIndex = url.indexOf('=', keyIndex)
let name = url.slice(keyIndex + 1, valueIndex === -1 ? undefined : valueIndex)
const nextKeyIndex = url.indexOf('&', keyIndex + 1)
let valueIndex = url.indexOf('=', keyIndex)
if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
valueIndex = -1
}
let name = url.slice(
keyIndex + 1,
valueIndex === -1 ? (nextKeyIndex === -1 ? undefined : nextKeyIndex) : valueIndex
)
if (encoded) {
name = _decodeURI(name)
}
keyIndex = nextKeyIndex
if (name === '') {
continue
}
let value
if (valueIndex === -1) {
keyIndex = -1
value = ''
} else {
keyIndex = url.indexOf('&', valueIndex)
value = url.slice(valueIndex + 1, keyIndex === -1 ? undefined : keyIndex)
value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? undefined : nextKeyIndex)
if (encoded) {
value = _decodeURI(value)
}