normalize-url
Advanced tools
Comparing version 7.0.3 to 7.1.0
export interface Options { | ||
/** | ||
@default 'http:' | ||
Values: `'https:' | 'http:'` | ||
*/ | ||
readonly defaultProtocol?: string; | ||
readonly defaultProtocol?: string; // TODO: Make this `'https:' | 'http:'` in the next major version. | ||
@@ -90,4 +92,6 @@ /** | ||
/** | ||
Removes HTTP(S) protocol from an URL `http://sindresorhus.com` → `sindresorhus.com`. | ||
Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`. | ||
It will only remove `https://` and `http://` protocols. | ||
@default false | ||
@@ -180,2 +184,19 @@ | ||
/** | ||
Keeps only query parameters that matches any of the provided strings or regexes. | ||
__Note__: It overrides the `removeQueryParameters` option. | ||
@default undefined | ||
@example | ||
``` | ||
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', { | ||
keepQueryParameters: ['ref'] | ||
}); | ||
//=> 'https://sindresorhus.com/?ref=unicorn' | ||
``` | ||
*/ | ||
readonly keepQueryParameters?: ReadonlyArray<RegExp | string>; | ||
/** | ||
Removes trailing slash. | ||
@@ -202,3 +223,3 @@ | ||
/** | ||
Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`. | ||
Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`. | ||
@@ -205,0 +226,0 @@ @default true |
12
index.js
@@ -203,6 +203,16 @@ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | ||
if (options.removeQueryParameters === true) { | ||
if (!Array.isArray(options.keepQueryParameters) && options.removeQueryParameters === true) { | ||
urlObject.search = ''; | ||
} | ||
// Keep wanted query parameters | ||
if (Array.isArray(options.keepQueryParameters) && options.keepQueryParameters.length > 0) { | ||
// eslint-disable-next-line unicorn/no-useless-spread -- We are intentionally spreading to get a copy. | ||
for (const key of [...urlObject.searchParams.keys()]) { | ||
if (!testParameter(key, options.keepQueryParameters)) { | ||
urlObject.searchParams.delete(key); | ||
} | ||
} | ||
} | ||
// Sort query parameters | ||
@@ -209,0 +219,0 @@ if (options.sortQueryParameters) { |
{ | ||
"name": "normalize-url", | ||
"version": "7.0.3", | ||
"version": "7.1.0", | ||
"description": "Normalize a URL", | ||
@@ -19,3 +19,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "ava && tsd" | ||
"test": "xo && c8 ava && tsd" | ||
}, | ||
@@ -22,0 +22,0 @@ "files": [ |
@@ -15,3 +15,3 @@ # normalize-url [![Coverage Status](https://codecov.io/gh/sindresorhus/normalize-url/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/normalize-url) | ||
*If you need to use this in the browser, use version 4: `npm i normalize-url@4`* | ||
*If you need Safari support, use version 4: `npm i normalize-url@4`* | ||
@@ -47,3 +47,4 @@ ## Usage | ||
Type: `string`\ | ||
Default: `http:` | ||
Default: `http:`\ | ||
Values: `'https:' | 'http:'` | ||
@@ -132,4 +133,6 @@ ##### normalizeProtocol | ||
Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`. | ||
Remove the protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`. | ||
It will only remove `https://` and `http://` protocols. | ||
```js | ||
@@ -213,2 +216,18 @@ normalizeUrl('https://sindresorhus.com'); | ||
##### keepQueryParameters | ||
Type: `Array<RegExp | string>`\ | ||
Default: `undefined` | ||
Keeps only query parameters that matches any of the provided strings or regexes. | ||
**Note:** It overrides the `removeQueryParameters` option. | ||
```js | ||
normalizeUrl('https://sindresorhus.com?foo=bar&ref=unicorn', { | ||
keepQueryParameters: ['ref'] | ||
}); | ||
//=> 'https://sindresorhus.com/?ref=unicorn' | ||
``` | ||
##### removeTrailingSlash | ||
@@ -239,3 +258,3 @@ | ||
Remove a sole `/` pathname in the output. This option is independant of `removeTrailingSlash`. | ||
Remove a sole `/` pathname in the output. This option is independent of `removeTrailingSlash`. | ||
@@ -242,0 +261,0 @@ ```js |
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
24219
419
307