All Projects → sindresorhus → Normalize Url

sindresorhus / Normalize Url

Licence: mit
Normalize a URL

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Normalize Url

Core
The internationalization (i18n) library for Angular
Stars: ✭ 4,027 (+686.52%)
Mutual labels:  npm-package
Cloudinary npm
Cloudinary NPM for node.js integration
Stars: ✭ 450 (-12.11%)
Mutual labels:  npm-package
Angular Translate
Translating your AngularJS 1.x apps
Stars: ✭ 4,414 (+762.11%)
Mutual labels:  npm-package
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (-17.77%)
Mutual labels:  npm-package
Sindresorhus Cli
The Sindre Sorhus CLI
Stars: ✭ 436 (-14.84%)
Mutual labels:  npm-package
Electron Reloader
Simple auto-reloading for Electron apps during development
Stars: ✭ 463 (-9.57%)
Mutual labels:  npm-package
Npm Module Boilerplate
Boilerplate for npm modules with ES6 features and all the best practices
Stars: ✭ 374 (-26.95%)
Mutual labels:  npm-package
Crypto Hash
Tiny hashing module that uses the native crypto API in Node.js and the browser
Stars: ✭ 501 (-2.15%)
Mutual labels:  npm-package
Figures
Unicode symbols with Windows CMD fallbacks
Stars: ✭ 438 (-14.45%)
Mutual labels:  npm-package
Vanilla Framework
From community websites to web applications, this CSS framework will help you achieve a consistent look and feel.
Stars: ✭ 476 (-7.03%)
Mutual labels:  npm-package
Node Thermal Printer
This npm package was made to control epson and star thermal printers
Stars: ✭ 424 (-17.19%)
Mutual labels:  npm-package
Trex
Package Manager for deno 🦕
Stars: ✭ 433 (-15.43%)
Mutual labels:  npm-package
Tfjs Yolo Tiny
In-Browser Object Detection using Tiny YOLO on Tensorflow.js
Stars: ✭ 465 (-9.18%)
Mutual labels:  npm-package
Negative Array
Negative array index support `array[-1]` using ES2015 Proxy
Stars: ✭ 420 (-17.97%)
Mutual labels:  npm-package
Type Fest
A collection of essential TypeScript types
Stars: ✭ 6,623 (+1193.55%)
Mutual labels:  npm-package
Cpx
A cli tool to watch and copy file globs.
Stars: ✭ 394 (-23.05%)
Mutual labels:  npm-package
Electron Better Ipc
Simplified IPC communication for Electron apps
Stars: ✭ 463 (-9.57%)
Mutual labels:  npm-package
Synp
Convert yarn.lock to package-lock.json and vice versa
Stars: ✭ 510 (-0.39%)
Mutual labels:  npm-package
Strip Json Comments
Strip comments from JSON. Lets you use comments in your JSON files!
Stars: ✭ 492 (-3.91%)
Mutual labels:  npm-package
Ignite Ui
Ignite UI by Infragistics
Stars: ✭ 468 (-8.59%)
Mutual labels:  npm-package

normalize-url Coverage Status

Normalize a URL

Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.

Install

$ npm install normalize-url

If you need to use this in the browser, use version 4: npm i [email protected]

Usage

const normalizeUrl = require('normalize-url');

normalizeUrl('sindresorhus.com');
//=> 'http://sindresorhus.com'

normalizeUrl('//www.sindresorhus.com:80/../baz?b=bar&a=foo');
//=> 'http://sindresorhus.com/baz?a=foo&b=bar'

API

normalizeUrl(url, options?)

url

Type: string

URL to normalize, including data URL.

options

Type: object

defaultProtocol

Type: string
Default: http:

normalizeProtocol

Type: boolean
Default: true

Prepend defaultProtocol to the URL if it's protocol-relative.

normalizeUrl('//sindresorhus.com:80/');
//=> 'http://sindresorhus.com'

normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});
//=> '//sindresorhus.com'
forceHttp

Type: boolean
Default: false

Normalize https: to http:.

normalizeUrl('https://sindresorhus.com:80/');
//=> 'https://sindresorhus.com'

normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});
//=> 'http://sindresorhus.com'
forceHttps

Type: boolean
Default: false

Normalize http: to https:.

normalizeUrl('https://sindresorhus.com:80/');
//=> 'https://sindresorhus.com'

normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true});
//=> 'https://sindresorhus.com'

This option can't be used with the forceHttp option at the same time.

stripAuthentication

Type: boolean
Default: true

Strip the authentication part of the URL.

normalizeUrl('user:[email protected]');
//=> 'https://sindresorhus.com'

normalizeUrl('user:[email protected]', {stripAuthentication: false});
//=> 'https://user:[email protected]'
stripHash

Type: boolean
Default: false

Strip the hash part of the URL.

normalizeUrl('sindresorhus.com/about.html#contact');
//=> 'http://sindresorhus.com/about.html#contact'

normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true});
//=> 'http://sindresorhus.com/about.html'
stripProtocol

Type: boolean
Default: false

Remove HTTP(S) protocol from the URL: http://sindresorhus.comsindresorhus.com.

normalizeUrl('https://sindresorhus.com');
//=> 'https://sindresorhus.com'

normalizeUrl('https://sindresorhus.com', {stripProtocol: true});
//=> 'sindresorhus.com'
stripWWW

Type: boolean
Default: true

Remove www. from the URL.

normalizeUrl('http://www.sindresorhus.com');
//=> 'http://sindresorhus.com'

normalizeUrl('http://www.sindresorhus.com', {stripWWW: false});
//=> 'http://www.sindresorhus.com'
removeQueryParameters

Type: Array<RegExp | string>
Default: [/^utm_\w+/i]

Remove query parameters that matches any of the provided strings or regexes.

normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {
	removeQueryParameters: ['ref']
});
//=> 'http://sindresorhus.com/?foo=bar'
removeTrailingSlash

Type: boolean
Default: true

Remove trailing slash.

Note: Trailing slash is always removed if the URL doesn't have a pathname unless the removeSingleSlash option is set to false.

normalizeUrl('http://sindresorhus.com/redirect/');
//=> 'http://sindresorhus.com/redirect'

normalizeUrl('http://sindresorhus.com/redirect/', {removeTrailingSlash: false});
//=> 'http://sindresorhus.com/redirect/'

normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false});
//=> 'http://sindresorhus.com'
removeSingleSlash

Type: boolean
Default: true

Remove a sole / pathname in the output. This option is independant of removeTrailingSlash.

normalizeUrl('https://sindresorhus.com/');
//=> 'https://sindresorhus.com'

normalizeUrl('https://sindresorhus.com/', {removeSingleSlash: false});
//=> 'https://sindresorhus.com/'
removeDirectoryIndex

Type: boolean | Array<RegExp | string>
Default: false

Removes the default directory index file from path that matches any of the provided strings or regexes. When true, the regex /^index\.[a-z]+$/ is used.

normalizeUrl('www.sindresorhus.com/foo/default.php', {
	removeDirectoryIndex: [/^default\.[a-z]+$/]
});
//=> 'http://sindresorhus.com/foo'
sortQueryParameters

Type: boolean
Default: true

Sorts the query parameters alphabetically by key.

normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', {
	sortQueryParameters: false
});
//=> 'http://sindresorhus.com/?b=two&a=one&c=three'

Related


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].