From f32422c3db77136d012b664f0a859dacf7316651 Mon Sep 17 00:00:00 2001 From: Orlando Date: Fri, 14 Jun 2024 18:42:12 +0100 Subject: [PATCH] setUrlSearchParams fix --- CHANGELOG.md | 6 ++++++ package.json | 2 +- src/helpers/setUrlSearchParams.test.ts | 6 ++++++ src/helpers/setUrlSearchParams.ts | 5 ++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f439d3e..eaaaf46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # deverything +## 0.51.1 + +### Patch Changes + +- setUrlSearchParams fix + ## 0.51.0 ### Minor Changes diff --git a/package.json b/package.json index 3e1fba2..c70cc85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deverything", - "version": "0.51.0", + "version": "0.51.1", "description": "Everything you need for Dev", "main": "./dist/index.js", "module": "./dist/index.mjs", diff --git a/src/helpers/setUrlSearchParams.test.ts b/src/helpers/setUrlSearchParams.test.ts index 2e18bc6..1a4ba0c 100644 --- a/src/helpers/setUrlSearchParams.test.ts +++ b/src/helpers/setUrlSearchParams.test.ts @@ -2,6 +2,12 @@ import { expect, describe, test } from "@jest/globals"; import { setUrlSearchParams } from "./setUrlSearchParams"; describe("setUrlSearchParams", () => { + test("no nullish", () => { + expect( + setUrlSearchParams("/signin?token#hash", { n: null, u: undefined, z: 0 }) + ).toBe("/signin?token=&z=0#hash"); + }); + test("relative url", () => { expect(setUrlSearchParams("/signin")).toBe("/signin"); expect(setUrlSearchParams("/signin?")).toBe("/signin"); diff --git a/src/helpers/setUrlSearchParams.ts b/src/helpers/setUrlSearchParams.ts index e07b45f..567ed36 100644 --- a/src/helpers/setUrlSearchParams.ts +++ b/src/helpers/setUrlSearchParams.ts @@ -1,6 +1,8 @@ +import { Maybe } from "../types"; + export const setUrlSearchParams = ( currentURL: string, - searchParams: Record = {} + searchParams: Record> = {} ) => { const isRelativeUrl = currentURL.startsWith("/"); const url = new URL( @@ -9,6 +11,7 @@ export const setUrlSearchParams = ( ); Object.entries(searchParams).forEach(([paramKey, paramValue]) => { + if (paramValue === null || paramValue === undefined) return; url.searchParams.set(paramKey, paramValue.toString()); });