normalize-url
Advanced tools
Comparing version 3.3.0 to 4.0.0
68
index.js
@@ -9,4 +9,4 @@ 'use strict'; | ||
module.exports = (urlString, opts) => { | ||
opts = Object.assign({ | ||
module.exports = (urlString, options) => { | ||
options = { | ||
defaultProtocol: 'http:', | ||
@@ -16,3 +16,4 @@ normalizeProtocol: true, | ||
forceHttps: false, | ||
stripHash: true, | ||
stripAuthentication: true, | ||
stripHash: false, | ||
stripWWW: true, | ||
@@ -22,16 +23,17 @@ removeQueryParameters: [/^utm_\w+/i], | ||
removeDirectoryIndex: false, | ||
sortQueryParameters: true | ||
}, opts); | ||
sortQueryParameters: true, | ||
...options | ||
}; | ||
// Backwards compatibility | ||
if (Reflect.has(opts, 'normalizeHttps')) { | ||
opts.forceHttp = opts.normalizeHttps; | ||
// TODO: Remove this at some point in the future | ||
if (Reflect.has(options, 'normalizeHttps')) { | ||
throw new Error('options.normalizeHttps is renamed to options.forceHttp'); | ||
} | ||
if (Reflect.has(opts, 'normalizeHttp')) { | ||
opts.forceHttps = opts.normalizeHttp; | ||
if (Reflect.has(options, 'normalizeHttp')) { | ||
throw new Error('options.normalizeHttp is renamed to options.forceHttps'); | ||
} | ||
if (Reflect.has(opts, 'stripFragment')) { | ||
opts.stripHash = opts.stripFragment; | ||
if (Reflect.has(options, 'stripFragment')) { | ||
throw new Error('options.stripFragment is renamed to options.stripHash'); | ||
} | ||
@@ -46,3 +48,3 @@ | ||
if (!isRelativeUrl) { | ||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, opts.defaultProtocol); | ||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol); | ||
} | ||
@@ -52,16 +54,22 @@ | ||
if (opts.forceHttp && opts.forceHttps) { | ||
if (options.forceHttp && options.forceHttps) { | ||
throw new Error('The `forceHttp` and `forceHttps` options cannot be used together'); | ||
} | ||
if (opts.forceHttp && urlObj.protocol === 'https:') { | ||
if (options.forceHttp && urlObj.protocol === 'https:') { | ||
urlObj.protocol = 'http:'; | ||
} | ||
if (opts.forceHttps && urlObj.protocol === 'http:') { | ||
if (options.forceHttps && urlObj.protocol === 'http:') { | ||
urlObj.protocol = 'https:'; | ||
} | ||
// Remove auth | ||
if (options.stripAuthentication) { | ||
urlObj.username = ''; | ||
urlObj.password = ''; | ||
} | ||
// Remove hash | ||
if (opts.stripHash) { | ||
if (options.stripHash) { | ||
urlObj.hash = ''; | ||
@@ -88,11 +96,11 @@ } | ||
// Remove directory index | ||
if (opts.removeDirectoryIndex === true) { | ||
opts.removeDirectoryIndex = [/^index\.[a-z]+$/]; | ||
if (options.removeDirectoryIndex === true) { | ||
options.removeDirectoryIndex = [/^index\.[a-z]+$/]; | ||
} | ||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length > 0) { | ||
if (Array.isArray(options.removeDirectoryIndex) && options.removeDirectoryIndex.length > 0) { | ||
let pathComponents = urlObj.pathname.split('/'); | ||
const lastComponent = pathComponents[pathComponents.length - 1]; | ||
if (testParameter(lastComponent, opts.removeDirectoryIndex)) { | ||
if (testParameter(lastComponent, options.removeDirectoryIndex)) { | ||
pathComponents = pathComponents.slice(0, pathComponents.length - 1); | ||
@@ -108,4 +116,3 @@ urlObj.pathname = pathComponents.slice(1).join('/') + '/'; | ||
// Remove `www.` | ||
// eslint-disable-next-line no-useless-escape | ||
if (opts.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z\.]{2,5})$/.test(urlObj.hostname)) { | ||
if (options.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z.]{2,5})$/.test(urlObj.hostname)) { | ||
// Each label should be max 63 at length (min: 2). | ||
@@ -119,5 +126,5 @@ // The extension should be max 5 at length (min: 2). | ||
// Remove query unwanted parameters | ||
if (Array.isArray(opts.removeQueryParameters)) { | ||
if (Array.isArray(options.removeQueryParameters)) { | ||
for (const key of [...urlObj.searchParams.keys()]) { | ||
if (testParameter(key, opts.removeQueryParameters)) { | ||
if (testParameter(key, options.removeQueryParameters)) { | ||
urlObj.searchParams.delete(key); | ||
@@ -129,3 +136,3 @@ } | ||
// Sort query parameters | ||
if (opts.sortQueryParameters) { | ||
if (options.sortQueryParameters) { | ||
urlObj.searchParams.sort(); | ||
@@ -138,3 +145,3 @@ } | ||
// Remove ending `/` | ||
if (opts.removeTrailingSlash || urlObj.pathname === '/') { | ||
if (options.removeTrailingSlash || urlObj.pathname === '/') { | ||
urlString = urlString.replace(/\/$/, ''); | ||
@@ -144,7 +151,12 @@ } | ||
// Restore relative protocol, if applicable | ||
if (hasRelativeProtocol && !opts.normalizeProtocol) { | ||
if (hasRelativeProtocol && !options.normalizeProtocol) { | ||
urlString = urlString.replace(/^http:\/\//, '//'); | ||
} | ||
// Remove http/https | ||
if (options.stripProtocol) { | ||
urlString = urlString.replace(/^(?:https?:)?\/\//, ''); | ||
} | ||
return urlString; | ||
}; |
{ | ||
"name": "normalize-url", | ||
"version": "3.3.0", | ||
"version": "4.0.0", | ||
"description": "Normalize a URL", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -38,7 +38,7 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "*", | ||
"ava": "^0.25.0", | ||
"coveralls": "^3.0.0", | ||
"nyc": "^12.0.2", | ||
"xo": "*" | ||
"nyc": "^13.1.0", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -73,3 +73,3 @@ # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master) | ||
normalizeUrl('https://sindresorhus.com:80/', {normalizeHttps: true}); | ||
normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true}); | ||
//=> 'http://sindresorhus.com' | ||
@@ -89,3 +89,3 @@ ``` | ||
normalizeUrl('http://sindresorhus.com:80/', {normalizeHttp: true}); | ||
normalizeUrl('http://sindresorhus.com:80/', {forceHttps: true}); | ||
//=> 'https://sindresorhus.com' | ||
@@ -96,3 +96,3 @@ ``` | ||
##### stripHash | ||
##### stripAuthentication | ||
@@ -102,2 +102,17 @@ Type: `boolean`<br> | ||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of a URL. | ||
```js | ||
normalizeUrl('user:password@sindresorhus.com'); | ||
//=> 'https://sindresorhus.com' | ||
normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false}); | ||
//=> 'https://user:password@sindresorhus.com' | ||
``` | ||
##### stripHash | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Removes hash from the URL. | ||
@@ -107,6 +122,21 @@ | ||
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' | ||
``` | ||
normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: false}); | ||
//=> 'http://sindresorhus.com/about.html#contact' | ||
##### stripProtocol | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Removes HTTP(S) protocol from an URL `http://sindresorhus.com` → `sindresorhus.com`. | ||
```js | ||
normalizeUrl('https://sindresorhus.com'); | ||
//=> 'https://sindresorhus.com' | ||
normalizeUrl('sindresorhus.com', {stripProtocol: true}); | ||
//=> 'sindresorhus.com' | ||
``` | ||
@@ -122,7 +152,7 @@ | ||
```js | ||
normalizeUrl('http://www.sindresorhus.com/about.html#contact'); | ||
//=> 'http://sindresorhus.com/about.html#contact' | ||
normalizeUrl('http://www.sindresorhus.com'); | ||
//=> 'http://sindresorhus.com' | ||
normalizeUrl('http://www.sindresorhus.com/about.html#contact', {stripWWW: false}); | ||
//=> 'http://www.sindresorhus.com/about.html#contact' | ||
normalizeUrl('http://www.sindresorhus.com', {stripWWW: false}); | ||
//=> 'http://www.sindresorhus.com' | ||
``` | ||
@@ -129,0 +159,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10904
123
225