Giter Site home page Giter Site logo

vladkens / url-normalize Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 118 KB

๐Ÿ”—๐Ÿงน 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.

Home Page: https://npm.im/url-normalize

License: MIT License

TypeScript 100.00%
cjs normalization normalizer npm-package url url-normalization url-normalizer esm typescript punycode

url-normalize's Introduction

url-normalize

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

yarn add url-normalize

Usage

import { urlNormalize } from "url-normalize"

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

urlNormalize("//www.example.com:443/../foo/bar?b=2&a=1#tag")
// -> "https://example.com/foo/bar?a=1&b=2#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

defaultProtocol (default: https)

Default supported protocols are: http, https

urlNormalize("example.com", { defaultProtocol: "http" })
// -> "http://example.com"

urlNormalize("example.com", { defaultProtocol: "ftps" })
// -> ftps://example.com

// BUT keeps original protocol if it in url
urlNormalize("https://example.com", { defaultProtocol: "http" })
// -> "https://example.com"

protocol (default: true)

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

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

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

www (default: false)

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

urlNormalize("www.example.com", { www: true })
// -> "https://www.example.com"

auth (default: false)

urlNormalize("https://user:[email protected]")
// -> "https://example.com"

urlNormalize("https://user:[email protected]", { auth: true })
// -> "https://user:[email protected]"

port (default: false)

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

urlNormalize("https://example.com:8080", { port: true })
// -> "https://example.com:8080"

// BUT for HTTP - 80 & HTTPS - 443 always without port
urlNormalize("https://example.com:443", { port: true })
// -> "https://example.com"

index (default: true)

urlNormalize("example.com/index.html")
// -> "https://example.com/index.html"

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

search (default: true)

urlNormalize("example.com/?a=1&b=2")
// -> "https://example.com/?a=1&b=2"

urlNormalize("example.com/?a=1&b=2", { search: false })
// -> "https://example.com"

sortSearch (default: true)

urlNormalize("example.com/?b=2&b=1")
// -> "https://example.com/?a=1&b=2"

urlNormalize("example.com/?b=2&b=1", { sortSearch: false })
// -> "https://example.com/?b=2&b=1"

filterSearch

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

fragment (default: true)

urlNormalize("example.com/#foo")
// -> "https://example.com/#foo"

urlNormalize("example.com/?b=2&b=1", { fragment: false })
// -> "https://example.com"

textFragment (default: false)

urlNormalize("example.com/#:~:text=hello")
// -> "https://example.com"

urlNormalize("example.com/?b=2&b=1", { textFragment: true })
// -> "https://example.com/#:~:text=hello"

customProtocol (default: false)

urlNormalize("ftps://example.com")
// -> null

urlNormalize("tg://example.com")
// -> null

urlNormalize("ftps://example.com", { customProtocol: true })
// -> "ftps://example.com"

urlNormalize("tg://example.com", { customProtocol: true })
// -> "tg://example.com"

forceProtocol

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

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

unicode (default: false)

urlNormalize("๐Ÿ‘ป๐Ÿ’ฅ.ws")
// -> "https://xn--9q8huc.ws"

urlNormalize("๐Ÿ‘ป๐Ÿ’ฅ.ws", { unicode: true })
// -> "https://๐Ÿ‘ป๐Ÿ’ฅ.ws"

urlNormalize("https://xn--9q8huc.ws", { unicode: true })
// -> "https://๐Ÿ‘ป๐Ÿ’ฅ.ws"

Advanced

createUrlNormalize

import { createUrlNormalize } from "url-normalize"

const urlNormalize = createUrlNormalize({
  defaultProtocol: "http",
  fragment: false,
})

urlNormalize("example.com/foo#tag")
// -> "http://example.com/foo"

urlNormalizeOrFail

import { urlNormalize, urlNormalizeOrFail } from "url-normalize"

urlNormalize("invalid")
// -> null

urlNormalizeOrFail("invalid")
// throws Exception

extractDomain

import { extractDomain, extractDomainOrFail } from "url-normalize"

extractDomain("https://example.com:8080/?a=1&b=2#tag")
// -> "example.com"

extractDomain("invalid")
// -> null

extractDomainOrFail("invalid")
// throws Exception

humanizeUrl

import { humanizeUrl, humanizeUrlOrFail } from "url-normalize"

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

humanizeUrl("invalid")
// -> null

humanizeUrlOrFail("invalid")
// throws Exception

Similar projects

url-normalize's People

Contributors

vladkens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.