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: remove undefined from withQuery #71

Merged
merged 1 commit into from Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/query.ts
Expand Up @@ -5,7 +5,7 @@ import {
encodeQueryValue
} from './encoding'

export type QueryValue = string | string[] | undefined
export type QueryValue = string | string[] | undefined | null
export type QueryObject = Record<string, QueryValue>

export function parseQuery (paramsStr: string = ''): QueryObject {
Expand Down Expand Up @@ -50,5 +50,5 @@ export function encodeQueryItem (key: string, val: QueryValue): string {
}

export function stringifyQuery (query: QueryObject) {
return Object.keys(query).map(k => encodeQueryItem(k, query[k])).join('&')
return Object.keys(query).filter(k => query[k] !== undefined).map(k => encodeQueryItem(k, query[k])).join('&')
}
3 changes: 3 additions & 0 deletions test/query.test.ts
Expand Up @@ -10,8 +10,11 @@ describe('withQuery', () => {
{ input: '/?test', query: { foo: '0' }, out: '/?test&foo=0' },
{ input: '/?test', query: { foo: 0 }, out: '/?test&foo=0' },
{ input: '/?test', query: { foo: 1 }, out: '/?test&foo=1' },
{ input: '/?test', query: { test: undefined }, out: '/' },
{ input: '/?foo=1', query: { foo: 2 }, out: '/?foo=2' },
{ input: '/?foo=1', query: { foo: true, bar: false }, out: '/?foo=true&bar=false' },
{ input: '/?foo=1', query: { foo: undefined }, out: '/' },
{ input: '/?foo=1', query: { foo: null }, out: '/?foo' },
{
input: '/',
query: { email: 'some email.com' },
Expand Down