Skip to content

Commit

Permalink
add more configuration options; fix types import
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed May 14, 2023
1 parent be1a22f commit 299991d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "url-normalize",
"version": "1.1.1",
"version": "1.2.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.",
Expand All @@ -26,6 +26,7 @@
"main": "dist/main.cjs",
"exports": "dist/main.mjs"
},
"types": "./dist/main.d.ts",
"exports": {
"types": "./dist/main.d.ts",
"import": "./dist/main.mjs",
Expand Down
28 changes: 27 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<!-- <a href="https://npmjs.org/package/url-normalize">
<img src="https://badgen.net/npm/dm/url-normalize" alt="downloads" />
</a> -->
<a href="https://npmjs.org/package/url-normalize">
<a href="https://github.com/vladkens/url-normalize/blob/main/LICENSE">
<img src="https://badgen.net/npm/licence/url-normalize" alt="license" />
</a>
</div>
Expand Down Expand Up @@ -66,9 +66,25 @@ urlNormalize("https://example.com", { defaultProtocol: "http" })
// -> "https://example.com"
```

#### keepProtocol `(default: true)`

```typescript
urlNormalize("https://example.com")
// -> "https://example.com"

urlNormalize("https://example.com", { keepProtocol: false })
// -> "example.com"

urlNormalize("https://example.com/foo?bar=baz", { keepProtocol: false })
// -> "example.com/foo?bar=baz"
```

#### keepWWW `(default: false)`

```typescript
urlNormalize("www.example.com")
// -> "https://example.com"

urlNormalize("www.example.com", { keepWWW: true })
// -> "https://www.example.com"
```
Expand Down Expand Up @@ -163,6 +179,16 @@ urlNormalize("tg://example.com", { allowCustomProtocol: true })
// -> "tg://example.com"
```

#### forceProtocol

```typescript
urlNormalize("https://example.com", { forceProtocol: "sftp" })
// -> "sftp://example.com"

urlNormalize("tg://example.com", { forceProtocol: "we" })
// -> "we://example.com"
```

### Advanced

#### urlNormalizeOrFail
Expand Down
26 changes: 21 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export type Options = {
allowCustomProtocol?: boolean
defaultProtocol?: string
forceProtocol?: string
keepAuth?: boolean
keepDirectoryIndex?: boolean
keepHash?: boolean
keepPort?: boolean // default port always removed
keepProtocol?: boolean
keepQueryParams?: boolean
keepTextFragment?: boolean
keepWWW?: boolean
Expand All @@ -18,6 +20,7 @@ const DefaultOptions: Options = {
keepDirectoryIndex: true,
keepHash: true,
keepPort: true,
keepProtocol: true,
keepQueryParams: true,
keepTextFragment: false,
keepWWW: false,
Expand Down Expand Up @@ -46,7 +49,8 @@ export const urlNormalizeOrFail = (url: string, options?: Options): string => {

if (
!options.allowCustomProtocol &&
!["http:", "https:", `${options.defaultProtocol}:`].includes(obj.protocol)
!["http:", "https:", `${options.defaultProtocol}:`].includes(obj.protocol) &&
!options.forceProtocol // URL in nodejs not allow to change protocol, so do it on the end
) {
throw new Error("Invalid protocol")
}
Expand Down Expand Up @@ -105,12 +109,24 @@ export const urlNormalizeOrFail = (url: string, options?: Options): string => {
let res = obj.toString()
if (res.endsWith("/") && !res.endsWith("/#/")) res = res.slice(0, -1)

let protocol = obj.protocol
if (options.forceProtocol) {
const regex = new RegExp(`^${obj.protocol}//`, "i")
res = res.replace(regex, `${options.forceProtocol}://`)
protocol = `${options.forceProtocol}:`
}

if (!options.keepProtocol) {
const regex = new RegExp(`^${protocol}//`, "i")
res = res.replace(regex, "")
}

return res
}

export const urlNormalize = (url: string, options?: Options): string | null => {
export const urlNormalize = (url?: string | null, options?: Options): string | null => {
try {
return urlNormalizeOrFail(url, options)
return urlNormalizeOrFail(url ?? "", options)
} catch {
return null
}
Expand All @@ -122,9 +138,9 @@ export const extractDomainOrFail = (url: string): string => {
return obj.hostname
}

export const extractDomain = (url: string): string | null => {
export const extractDomain = (url?: string | null): string | null => {
try {
return extractDomainOrFail(url)
return extractDomainOrFail(url ?? "")
} catch {
return null
}
Expand Down
17 changes: 17 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ test("should allow custom options", () => {
t(`abc://www.example.com/foo/bar`, null, { allowCustomProtocol: false })
t(`abc://www.example.com/foo/bar`, null, { allowCustomProtocol: false })

// keep protocol
t("https://example.com", "https://example.com", { keepProtocol: true })
t("https://example.com", "example.com", { keepProtocol: false })
t("https://example.com/abc", "example.com/abc", { keepProtocol: false })
t("https://example.com:80/abc", "example.com:80/abc", { keepProtocol: false })
t("https://example.com/foo?bar=baz", "example.com/foo?bar=baz", { keepProtocol: false })
t("abc://example.com:80/abc", "example.com:80/abc", { keepProtocol: false, allowCustomProtocol: true })
t("abc://example.com:80/abc", null, { keepProtocol: false, allowCustomProtocol: false })

// force protocol
t("https://example.com", "http://example.com", { forceProtocol: "http" })
t("https://example.com", "https://example.com", { forceProtocol: "https" })
t("https://example.com", "abc://example.com", { forceProtocol: "abc" })
t("https://example.com", "example.com", { forceProtocol: "abc", keepProtocol: false })
t("https://example.com", "sftp://example.com", { forceProtocol: "sftp" })
t("tg://example.com", "we://example.com", { forceProtocol: "we" })

// keep www
t("www.example.com", "https://www.example.com", { keepWWW: true })
t("www.example.com", "https://example.com", { keepWWW: false })
Expand Down

0 comments on commit 299991d

Please sign in to comment.