Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: decodeQueryValue and decode query params with support for space #11

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const EQUAL_RE = /=/g // %3D
const IM_RE = /\?/g // %3F
const PLUS_RE = /\+/g // %2B

const BACKTICK_RE = /`/g
const CURLY_OPEN_RE = /\{/g
const CURLY_CLOSE_RE = /\}/g
const CARET_RE = /\^/g

const ENC_BRACKET_OPEN_RE = /%5B/g // [
const ENC_BRACKET_CLOSE_RE = /%5D/g // ]
const ENC_CARET_RE = /%5E/g // ^
Expand Down Expand Up @@ -115,6 +120,24 @@ export function decode (text: string | number = ''): string {
}
}

/**
* Decode query value (taking care to handle vue-router substitutions).
*
* @param text - string to decode
* @returns decoded string
*/
export function decodeQueryValue (text: string): string {
return decode(
text
// Decode plus as space
.replace(PLUS_RE, encodeURIComponent(' '))
danielroe marked this conversation as resolved.
Show resolved Hide resolved
.replace(BACKTICK_RE, encodeURIComponent('`'))
.replace(CURLY_OPEN_RE, encodeURIComponent('{'))
.replace(CURLY_CLOSE_RE, encodeURIComponent('}'))
.replace(CARET_RE, encodeURIComponent('^'))
)
}

export function encodeHost (name: string = '') {
return toASCII(name)
}
9 changes: 7 additions & 2 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { decode, encodeQueryKey, encodeQueryValue } from './encoding'
import {
decode,
decodeQueryValue,
encodeQueryKey,
encodeQueryValue
} from './encoding'

export type QueryValue = string | string[] | undefined
export type QueryObject = Record<string, QueryValue>
Expand All @@ -12,7 +17,7 @@ export function parseQuery (paramsStr: string = ''): QueryObject {
const s = (param.match(/([^=]+)=?(.*)/) || [])
if (s.length < 2) { continue }
const key = decode(s[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if key is having space? The space is replaced with + sign while encoding but on decode it doesn't replace back to space.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind to make a PR adding test case?

Copy link
Contributor

@rajkadhi10 rajkadhi10 Jun 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pi0 Raised PR #150

const value = decode(s[2] || '')
const value = decodeQueryValue(s[2] || '')
if (obj[key]) {
if (Array.isArray(obj[key])) {
(obj[key] as string[]).push(value)
Expand Down
2 changes: 1 addition & 1 deletion test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('normalizeURL', () => {
'/http:/': '/http:/',
'http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/': 'http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/',
'http://localhost/?redirect=http://google.com?q=test': 'http://localhost/?redirect=http://google.com?q=test',
'http://localhost/?email=some+v1@email.com': 'http://localhost/?email=some%2Bv1@email.com',
'http://localhost/?email=some+v1@email.com': 'http://localhost/?email=some+v1@email.com',
pi0 marked this conversation as resolved.
Show resolved Hide resolved
'http://localhost/?email=some%2Bv1%40email.com': 'http://localhost/?email=some%2Bv1@email.com'
}

Expand Down
10 changes: 10 additions & 0 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ describe('withQuery', () => {
{ input: '/?test', query: {}, out: '/?test' },
{ input: '/?test', query: { foo: 1 }, out: '/?test&foo=1' },
{ input: '/?foo=1', query: { foo: 2 }, out: '/?foo=2' },
{
input: '/',
query: { email: 'some email.com' },
out: '/?email=some+email.com'
},
{
input: '/',
query: { str: '&', str2: '%26' },
out: '/?str=%26&str2=%2526'
},
{ input: '/?x=1,2,3', query: { y: '1,2,3' }, out: '/?x=1,2,3&y=1,2,3' }
]

Expand Down