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:
parent
5f4eac8466
commit
0560f27bde
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user