-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
50 additions
and
18 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
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,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": [ | ||
|
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
``` | ||
|
||
|
@@ -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) |
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,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}`) | ||
|
@@ -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", () => { | ||
|
@@ -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) | ||
|
||
|
@@ -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() |