Skip to content

Commit

Permalink
feat: remove undefined from withQuery (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonlinU committed Nov 14, 2022
1 parent c548aa6 commit f8900d5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
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

0 comments on commit f8900d5

Please sign in to comment.