-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
140 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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¶m2=asd") | ||
).toStrictEqual({ | ||
param1: "123", | ||
param2: "asd", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters