From f46795e0c0b9b03e1dbed5007e59361505bd1cc4 Mon Sep 17 00:00:00 2001 From: Brandon Antonio Lorenzo Date: Wed, 3 Aug 2022 18:48:02 -0500 Subject: [PATCH] feat: remove undefined from withQuery --- src/query.ts | 4 ++-- test/query.test.ts | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/query.ts b/src/query.ts index bbcec7f8..047c5d16 100644 --- a/src/query.ts +++ b/src/query.ts @@ -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 export function parseQuery (paramsStr: string = ''): QueryObject { @@ -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('&') } diff --git a/test/query.test.ts b/test/query.test.ts index e4fecec3..74615292 100644 --- a/test/query.test.ts +++ b/test/query.test.ts @@ -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' },