Skip to content

Commit

Permalink
URLSearch param helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Oct 28, 2023
1 parent 9b910ed commit 8b600aa
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 17 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.33.0

### Minor Changes

- urls search params

## 0.32.0

### Minor Changes
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ Contributions always welcome!
- `cleanSpaces()` trims and turns double spaces into single space
- `clamp()` clamp number in a range
- `first()` get the first element of an array
- `firstKey()`
- `firstValue()`
- `getKeys()` get the keys of an object, includes symbols
- `getUrlSearchParam()`
- `getUrlSearchParams()`
- `last()` get the last element of an array
-`merge()` deep merge objects
- `moveToFirst()` move array element to first
Expand All @@ -80,6 +84,7 @@ Contributions always welcome!
- `promiseWithTimeout()` takes a promise, a timeoutMs, and an option error as arguments. Returns a new Promise that either resolves with the value of the input promise or rejects with the provided error or a default error message if the input promise does not resolve or reject within the specified timeoutMs.
- `scrambleText()` replace alpha chars with random chars
- `sleep()` promise-based sleep
- `setUrlSearchParams()`
- `shuffle()` shuffles elements in an array
- `toggleArrayValue()` remove/add value in array
- `truncate()` truncate text, does not break emojis
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.32.0",
"version": "0.33.0",
"description": "Everything you need for Dev",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/arrayIntersection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { uniqueValues } from "./uniqueValues";

export const arrayIntersection = (arr1: any[], arr2: any[]) => {
export const arrayIntersection = <T>(arr1: T[], arr2: T[]): T[] => {
return uniqueValues(arr1.filter((value) => arr2.includes(value)));
};
2 changes: 1 addition & 1 deletion src/helpers/getUrlSearchParam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("getUrlSearchParam", () => {
"1"
);
expect(getUrlSearchParam("http://pippo.com?param=2&param=1", "param")).toBe(
"2"
"1"
);
expect(
getUrlSearchParam("http://localhost/?param=1&param2=2", "param2")
Expand Down
15 changes: 6 additions & 9 deletions src/helpers/getUrlSearchParam.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Maybe } from "../types/Maybe";
import { getUrlSearchParams } from "./getUrlSearchParams";

export const getUrlSearchParam = (urlString: Maybe<string>, param: string) => {
if (!urlString) return undefined;
try {
const url = new URL(urlString);
const value = url.searchParams.get(param);
return value ?? undefined;
} catch (_e) {
return undefined;
}
export const getUrlSearchParam = (
urlString: Maybe<string>,
param: string
): string | undefined => {
return getUrlSearchParams(urlString)[param];
};
20 changes: 20 additions & 0 deletions src/helpers/getUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, test } from "@jest/globals";
import { getUrlSearchParams } from "./getUrlSearchParams";

describe("getUrlSearchParams", () => {
test("found", async () => {
expect(getUrlSearchParams("https")).toStrictEqual({});
expect(getUrlSearchParams("https://www.ciao.com")).toStrictEqual({});
expect(getUrlSearchParams("https://www.ciao.com/")).toStrictEqual({});
expect(getUrlSearchParams("https://www.ciao.com/#")).toStrictEqual({});
expect(getUrlSearchParams("https://www.ciao.com/?param=")).toStrictEqual({
param: "",
});
expect(
getUrlSearchParams("https://www.ciao.com/?param1=123&param2=asd")
).toStrictEqual({
param1: "123",
param2: "asd",
});
});
});
13 changes: 13 additions & 0 deletions src/helpers/getUrlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Maybe } from "../types/Maybe";

export const getUrlSearchParams = (
urlString: Maybe<string>
): Record<string, string> => {
if (!urlString) return {};
try {
const url = new URL(urlString);
return Object.fromEntries(url.searchParams);
} catch (_e) {
return {};
}
};
3 changes: 3 additions & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export * from "./first";
export * from "./firstKey";
export * from "./firstValue";
export * from "./getKeys";
export * from "./getUrlSearchParam";
export * from "./getUrlSearchParams";
export * from "./last";
export * from "./merge";
export * from "./moveToFirst";
Expand All @@ -21,6 +23,7 @@ export * from "./pretty";
export * from "./promiseWithTimeout";
export * from "./scrambleText";
export * from "./serialize";
export * from "./setUrlSearchParams";
export * from "./shuffle";
export * from "./sleep";
export * from "./toggleArrayValue";
Expand Down
57 changes: 57 additions & 0 deletions src/helpers/setUrlSearchParams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { expect, describe, test } from "@jest/globals";
import { setUrlSearchParams } from "./setUrlSearchParams";

describe("setUrlSearchParams", () => {
test("relative url", () => {
expect(setUrlSearchParams("/signin")).toBe("/signin");
expect(setUrlSearchParams("/signin?")).toBe("/signin");
expect(setUrlSearchParams("/signin?in")).toBe("/signin?in");
expect(setUrlSearchParams("/signin?in#sec")).toBe("/signin?in#sec");
expect(setUrlSearchParams("/signin?in#sec", { UPPER: "CASE" })).toBe(
"/signin?in=&UPPER=CASE#sec"
);
expect(
setUrlSearchParams("/signin?in#sec", { and: "&", equals: "=" })
).toBe("/signin?in=&and=%26&equals=%3D#sec");
});

test("ip", () => {
expect(setUrlSearchParams("http://127.127.127.127", {})).toBe(
"http://127.127.127.127/"
);

expect(setUrlSearchParams("http://127.127.127.127/ok", {})).toBe(
"http://127.127.127.127/ok"
);
expect(
setUrlSearchParams("http://127.127.127.127/ok?go", { ng: true })
).toBe("http://127.127.127.127/ok?go=&ng=true");
});

test("abs url", () => {
expect(setUrlSearchParams("http://localhost", {})).toBe(
"http://localhost/"
);
expect(setUrlSearchParams("http://dev.dev/hop", { ciao: "tu" })).toBe(
"http://dev.dev/hop?ciao=tu"
);
expect(
setUrlSearchParams("https://secure.gong/?ciao=tu", { ciao: "tu" })
).toBe("https://secure.gong/?ciao=tu");
expect(
setUrlSearchParams("http://localhost/?ciao=tu", { ciao: "tu", a: "b" })
).toBe("http://localhost/?ciao=tu&a=b");
expect(
setUrlSearchParams("http://localhost/?ciao=tu", { ciao: "tu", a: "" })
).toBe("http://localhost/?ciao=tu&a=");
expect(
setUrlSearchParams("http://localhost/?ciao=tu", { ciao: "tu", a: 1 })
).toBe("http://localhost/?ciao=tu&a=1");
expect(
setUrlSearchParams("http://localhost/?kikko=tu#", { ciao: "tu", a: 1 })
).toBe("http://localhost/?kikko=tu&ciao=tu&a=1#");
expect(setUrlSearchParams("http://localhost/?over=1", { over: "2" })).toBe(
"http://localhost/?over=2"
);
});
});
20 changes: 20 additions & 0 deletions src/helpers/setUrlSearchParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const setUrlSearchParams = (
currentURL: string,
searchParams: Record<string, string | number | boolean> = {}
) => {
const isRelativeUrl = currentURL.startsWith("/");
const url = new URL(
currentURL,
isRelativeUrl ? "https://deverything.dev/" : undefined // add base to make relative urls work
);

Object.entries(searchParams).forEach(([paramKey, paramValue]) => {
url.searchParams.set(paramKey, paramValue.toString());
});

if (isRelativeUrl) {
return url.pathname + url.search + url.hash;
}

return url.toString();
};
1 change: 0 additions & 1 deletion src/random/randomEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import { DOMAIN_NAMES } from "../constants/domains";
import { randomArrayItem } from "./randomArrayItem";
import { randomHandle } from "./randomHandle";

//Use randomId() to generate a unique email
export const randomEmail = () =>
`${randomHandle()}@${randomArrayItem(DOMAIN_NAMES)}`;
4 changes: 2 additions & 2 deletions src/random/randomHandle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { WESTERN_FIRST_NAMES, WESTERN_LAST_NAMES } from "../constants/names";
import { randomArrayItem } from "./randomArrayItem";
import { randomInt } from "./randomInt";
import { randomNumericId } from "./randomNumericId";

export const randomHandle = () =>
(
randomArrayItem(WESTERN_FIRST_NAMES) +
"." +
randomArrayItem(WESTERN_LAST_NAMES)
).toLowerCase() + randomInt(11, 99);
).toLowerCase() + randomNumericId(); // use randomNumericId too keep handles process-unique
3 changes: 3 additions & 0 deletions src/validators/isURL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ describe("isURL", function () {
expect(isURL("[email protected]")).toBe(false);
expect(isURL("")).toBe(false);
expect(isURL(" ")).toBe(false);
expect(isURL("/localhost")).toBe(false);
expect(isURL("file://../localhost")).toBe(false);
expect(isURL("../localhost")).toBe(false);
expect(isURL("//localhost")).toBe(false);
});

Expand Down
4 changes: 2 additions & 2 deletions src/validators/isURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const pattern = new RegExp(
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
"(\\#[-a-z\\d_]*)?$",
"(\\#[-a-z\\d_]*)?$", // fragment locator
"i"
); // fragment locator
);

export const isURL = (arg: string) => {
return !!arg && pattern.test(arg);
Expand Down

0 comments on commit 8b600aa

Please sign in to comment.