Skip to content

Commit

Permalink
add ability to filter query params by filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed May 14, 2023
1 parent 6cca2de commit 947827b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
26 changes: 26 additions & 0 deletions benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { urlNormalize } from "./src/main"

// node --loader tsm --no-warnings bench.ts

const bench = (name: string, count: number, fn: () => void) => {
const st = Date.now()
for (let i = 0; i < count; ++i) fn()
const dt = Date.now() - st
console.log(`${name} x ${count} = ${dt / 1000}s (${dt / count}ms per it)`)
}

const main = () => {
bench("urlNormalize – simple", 1e5, () => {
urlNormalize("https://www.google.com")
})

bench("urlNormalize – harder", 1e5, () => {
urlNormalize("://www.example.com/foo//bar/../baz?b=2&a=1#tag")
})

bench("urlNormalize – replace protocol", 1e5, () => {
urlNormalize("www.example.com/foo//bar/../baz?b=2&a=1#tag", { forceProtocol: "sftp" })
})
}

main()
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
{
"type": "module",
"name": "url-normalize",
"version": "1.2.1",
"version": "1.3.0",
"author": "Vlad Pronsky <[email protected]>",
"repository": "vladkens/url-normalize",
"description": "Normalize URLs to a standardized form by removing unnecessary characters, decoding encoded characters, removing default index pages, removing trailing slashes, and more.",
"license": "MIT",
"keywords": [
"url",
"normalize",
"canonical",
"domain",
"extract",
"humanize",
"normalization",
"normalize",
"punycode",
"querystrings",
"unicode",
"uri",
"url-normalization",
"url-normalize",
"querystrings",
"domain",
"extract",
"canonical"
"url"
],
"files": [
"dist"
Expand All @@ -26,6 +29,7 @@
"test": "uvu -r tsm tests/",
"test-cov": "c8 --include=src yarn test",
"test-watch": "watchexec -e ts 'clear && yarn test'",
"bench": "node --loader tsm --no-warnings benchmark.ts",
"format": "prettier --write '{src,tests}/**/*.{js,jsx,ts,tsx}'",
"format-check": "prettier --check '{src,tests}/**/*.{js,jsx,ts,tsx}'"
},
Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ urlNormalize("example.com/?b=2&b=1", { sortQueryParams: false })
// -> "https://example.com/?b=2&b=1"
```

### filterQueryParams

```typescript
urlNormalize("example.com/?c=3&b=2&a=1", { filterQueryParams: (k, v) => k === "a" || v == "3" })
// -> https://example.com/?a=1&c=3
```

#### keepHash `(default: true)`

```typescript
Expand Down Expand Up @@ -189,7 +196,7 @@ urlNormalize("tg://example.com", { forceProtocol: "we" })
// -> "we://example.com"
```

#### unicodeDomain (default: false)
#### unicodeDomain `(default: false)`

```typescript
urlNormalize("👻💥.ws")
Expand Down
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { toUnicode } from "punycode"
export type Options = {
allowCustomProtocol?: boolean
defaultProtocol?: string
filterQueryParams?: (key: string, value: string) => boolean
forceProtocol?: string
keepAuth?: boolean
keepDirectoryIndex?: boolean
Expand Down Expand Up @@ -96,6 +97,14 @@ export const urlNormalizeOrFail = (url: string, options?: Options): string => {
obj.search = obj.search.replace(/&{2,}/, "&") // replace multiple "&"
obj.search = obj.search.replace(/%20/g, "+") // replace "%20" to "+"

if (options.filterQueryParams) {
const params = new URLSearchParams(obj.search)
for (const [key, value] of params.entries()) {
if (!options.filterQueryParams(key, value)) params.delete(key)
}
obj.search = params.toString()
}

if (options.sortQueryParams) {
obj.searchParams.sort()
canFail(() => (obj.search = decodeURIComponent(obj.searchParams.toString())))
Expand Down
4 changes: 4 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ test("should allow custom options", () => {
t("example.com/#:~:text=hello%20world", "https://example.com/#:~:text=hello%20world", {
keepTextFragment: true,
})

// filter query params
t("example.com/?b=2&a=1", "https://example.com/?a=1", { filterQueryParams: (k, v) => k === "a" })
t("example.com/?c=3&b=2&a=1", "https://example.com/?a=1&c=3", { filterQueryParams: (k, v) => k === "a" || v == "3" })
})

test("custom protocol", () => {
Expand Down

0 comments on commit 947827b

Please sign in to comment.