normalize-url
Advanced tools
Comparing version 1.4.1 to 1.5.0
22
index.js
@@ -29,2 +29,8 @@ 'use strict'; | ||
function testQueryParameter(name, filters) { | ||
return filters.some(function (filter) { | ||
return filter instanceof RegExp ? filter.test(name) : filter === name; | ||
}); | ||
} | ||
module.exports = function (str, opts) { | ||
@@ -34,3 +40,4 @@ opts = objectAssign({ | ||
stripFragment: true, | ||
stripWWW: true | ||
stripWWW: true, | ||
removeQueryParameters: [/^utm_\w+/i] | ||
}, opts); | ||
@@ -91,4 +98,15 @@ | ||
var queryParameters = queryString.parse(urlObj.search); | ||
// remove query unwanted parameters | ||
if (Array.isArray(opts.removeQueryParameters)) { | ||
for (var key in queryParameters) { | ||
if (testQueryParameter(key, opts.removeQueryParameters)) { | ||
delete queryParameters[key]; | ||
} | ||
} | ||
} | ||
// sort query parameters | ||
urlObj.search = queryString.stringify(sortKeys(queryString.parse(urlObj.search))); | ||
urlObj.search = queryString.stringify(sortKeys(queryParameters)); | ||
@@ -95,0 +113,0 @@ // decode query parameters |
{ | ||
"name": "normalize-url", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "Normalize a URL", | ||
@@ -22,2 +22,3 @@ "license": "MIT", | ||
"keywords": [ | ||
"normalize", | ||
"url", | ||
@@ -28,3 +29,2 @@ "uri", | ||
"str", | ||
"normalize", | ||
"normalise", | ||
@@ -45,3 +45,3 @@ "normalization", | ||
"prepend-http": "^1.0.0", | ||
"query-string": "^3.0.0", | ||
"query-string": "^4.1.0", | ||
"sort-keys": "^1.0.0" | ||
@@ -48,0 +48,0 @@ }, |
@@ -34,3 +34,3 @@ # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) | ||
Type: `string` | ||
Type: `String` | ||
@@ -43,3 +43,3 @@ URL to normalize. | ||
Type: `boolean` | ||
Type: `Boolean`<br> | ||
Default: `true` | ||
@@ -59,3 +59,3 @@ | ||
Type: `boolean` | ||
Type: `Boolean`<br> | ||
Default: `true` | ||
@@ -75,3 +75,3 @@ | ||
Type: `boolean` | ||
Type: `Boolean`<br> | ||
Default: `true` | ||
@@ -89,3 +89,17 @@ | ||
##### removeQueryParameters | ||
Type: `Array<RegExp|String>`<br> | ||
Default: `[/^utm_\w+/i]` | ||
Remove query parameters that matches any of the provided strings or regexes. | ||
```js | ||
normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', { | ||
ignoredQueryParameters: ['ref'] | ||
}); | ||
//=> 'http://sindresorhus.com/?foo=bar' | ||
``` | ||
## Related | ||
@@ -98,2 +112,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
7063
101
108
+ Addedquery-string@4.3.4(transitive)
- Removedquery-string@3.0.3(transitive)
Updatedquery-string@^4.1.0