Skip to content

Commit

Permalink
add humanizeUrl preset
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed May 14, 2023
1 parent 947827b commit c69b318
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
8 changes: 5 additions & 3 deletions benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { urlNormalize } from "./src/main"

// node --loader tsm --no-warnings bench.ts
import { humanizeUrl, urlNormalize } from "./src/main"

const bench = (name: string, count: number, fn: () => void) => {
const st = Date.now()
Expand All @@ -21,6 +19,10 @@ const main = () => {
bench("urlNormalize – replace protocol", 1e5, () => {
urlNormalize("www.example.com/foo//bar/../baz?b=2&a=1#tag", { forceProtocol: "sftp" })
})

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

main()
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
{
"type": "module",
"name": "url-normalize",
"version": "1.3.0",
"version": "1.4.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.",
"description": "Normalize URLs to a standardized form. HTTPS by default, flexible configuration, custom protocols, domain extraction, humazing URL, and punycode support. Both CJS & ESM modules available.",
"license": "MIT",
"keywords": [
"canonical",
"domain",
"extract",
"humanization",
"humanize",
"normalization",
"normalize",
"punycode",
"querystrings",
"unicode",
"uri",
"url-normalization",
"url-normalize",
"url"
],
"files": [
Expand Down
30 changes: 21 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@
</a>
</div>

### Features

- HTTPS by default
- Flexible configuration
- Custom protocols (e.g. tg://)
- Domain extraction
- Support both CJS & ESM modules
**Normalize URLs** to a standardized form. **HTTPS** by default, flexible configuration, custom protocols, **domain extraction**, **humazing URL**, and **punycode** support. Both CJS & ESM modules available.

### Install

```sh
yarn add url-normalize
npm install url-normalize
```

### Usage
Expand All @@ -46,6 +40,15 @@ urlNormalize("//www.example.com:443/../foo/bar?b=2&a=1#tag")
// all invalid urls is null
urlNormalize("example")
// -> null

urlNormalize("data:content/type;base64,abc")
// -> null

urlNormalize("tel:+123456789")
// -> null

urlNormalize("mailto:[email protected]")
// -> null
```

### Configuration
Expand Down Expand Up @@ -146,7 +149,7 @@ urlNormalize("example.com/?b=2&b=1", { sortQueryParams: false })
### filterQueryParams

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

Expand Down Expand Up @@ -252,6 +255,15 @@ urlNormalize("example.com/foo#tag")
// -> "http://example.com/foo"
```

#### humanizeUrl

```typescript
import { humanizeUrl } from "url-normalize"

humanizeUrl("https://example.com/foo/bar")
// -> example.com/foo/bar
```

### Similar projects

- [sindresorhus/normalize-url](https://github.com/sindresorhus/normalize-url)
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,5 @@ export const createUrlNormalize = (baseOptions: Options) => {
return urlNormalize(url, { ...baseOptions, ...opts })
}
}

export const humanizeUrl = createUrlNormalize({ keepProtocol: false })
21 changes: 19 additions & 2 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "uvu"
import { equal, not, throws } from "uvu/assert"
import { Options, extractDomain, extractDomainOrFail, urlNormalize, urlNormalizeOrFail } from "../src/main"
import { Options, extractDomain, extractDomainOrFail, humanizeUrl, urlNormalize, urlNormalizeOrFail } from "../src/main"

const t = (url: string, exp: string | null, opts?: Options) => {
return equal(urlNormalize(url, opts), exp, `FAIL: ${url} -> ${exp}`)
Expand Down Expand Up @@ -225,7 +225,7 @@ test("should allow custom options", () => {

// 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" })
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 Expand Up @@ -271,6 +271,7 @@ test("unhappy path", () => {

// https://en.wikipedia.org/wiki/List_of_URI_schemes
t("data:", null)
t("data:asdasdasdasdasd", null)
t("tel:1234", null)
t("mailto:[email protected]", null)

Expand Down Expand Up @@ -313,4 +314,20 @@ test("should throws on extract domain fail", () => {
not.throws(() => extractDomainOrFail("example.com:80"))
})

test("should humanizeUrl", () => {
const t = (url: string, exp: string | null) => equal(humanizeUrl(url), exp, `FAIL: ${url} -> ${exp}`)

t("https://example.com", "example.com")
t("https://www.example.com/", "example.com")
t("https://example.com/?", "example.com")
t("https://example.com:80", "example.com:80")
t("https://user:@example.com", "example.com")
t("https://example.com?", "example.com")
t("https://example.com#", "example.com")
t("https://example.com/foo", "example.com/foo")
t("https://example.com/foo/?b=2&a=1", "example.com/foo/?a=1&b=2")
t("https://example.com/foo/#tag", "example.com/foo/#tag")
t("example.com:443/foo/index.html?a=2&a=1#tag", "example.com/foo/index.html?a=2&a=1#tag")
})

test.run()

0 comments on commit c69b318

Please sign in to comment.