Skip to content

Commit

Permalink
setUrlSearchParams fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jun 14, 2024
1 parent 049d252 commit f32422c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# deverything

## 0.51.1

### Patch Changes

- setUrlSearchParams fix

## 0.51.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/helpers/setUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 4 additions & 1 deletion src/helpers/setUrlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Maybe } from "../types";

export const setUrlSearchParams = (
currentURL: string,
searchParams: Record<string, string | number | boolean> = {}
searchParams: Record<string, Maybe<string | number | boolean>> = {}
) => {
const isRelativeUrl = currentURL.startsWith("/");
const url = new URL(
Expand All @@ -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());
});

Expand Down

0 comments on commit f32422c

Please sign in to comment.