normalize-url
Advanced tools
Comparing version 1.9.1 to 2.0.0
96
index.js
'use strict'; | ||
var url = require('url'); | ||
var punycode = require('punycode'); | ||
var queryString = require('query-string'); | ||
var prependHttp = require('prepend-http'); | ||
var sortKeys = require('sort-keys'); | ||
var objectAssign = require('object-assign'); | ||
const url = require('url'); | ||
const punycode = require('punycode'); | ||
const queryString = require('query-string'); | ||
const prependHttp = require('prepend-http'); | ||
const sortKeys = require('sort-keys'); | ||
var DEFAULT_PORTS = { | ||
const DEFAULT_PORTS = { | ||
'http:': 80, | ||
@@ -15,9 +14,9 @@ 'https:': 443, | ||
// protocols that always contain a `//`` bit | ||
var slashedProtocol = { | ||
'http': true, | ||
'https': true, | ||
'ftp': true, | ||
'gopher': true, | ||
'file': true, | ||
// Protocols that always contain a `//`` bit | ||
const slashedProtocol = { | ||
http: true, | ||
https: true, | ||
ftp: true, | ||
gopher: true, | ||
file: true, | ||
'http:': true, | ||
@@ -31,9 +30,7 @@ 'https:': true, | ||
function testParameter(name, filters) { | ||
return filters.some(function (filter) { | ||
return filter instanceof RegExp ? filter.test(name) : filter === name; | ||
}); | ||
return filters.some(filter => filter instanceof RegExp ? filter.test(name) : filter === name); | ||
} | ||
module.exports = function (str, opts) { | ||
opts = objectAssign({ | ||
module.exports = (str, opts) => { | ||
opts = Object.assign({ | ||
normalizeProtocol: true, | ||
@@ -45,3 +42,4 @@ normalizeHttps: false, | ||
removeTrailingSlash: true, | ||
removeDirectoryIndex: false | ||
removeDirectoryIndex: false, | ||
sortQueryParameters: true | ||
}, opts); | ||
@@ -53,8 +51,8 @@ | ||
var hasRelativeProtocol = str.indexOf('//') === 0; | ||
const hasRelativeProtocol = str.startsWith('//'); | ||
// prepend protocol | ||
// Prepend protocol | ||
str = prependHttp(str.trim()).replace(/^\/\//, 'http://'); | ||
var urlObj = url.parse(str); | ||
const urlObj = url.parse(str); | ||
@@ -69,7 +67,7 @@ if (opts.normalizeHttps && urlObj.protocol === 'https:') { | ||
// prevent these from being used by `url.format` | ||
// Prevent these from being used by `url.format` | ||
delete urlObj.host; | ||
delete urlObj.query; | ||
// remove fragment | ||
// Remove fragment | ||
if (opts.stripFragment) { | ||
@@ -79,4 +77,4 @@ delete urlObj.hash; | ||
// remove default port | ||
var port = DEFAULT_PORTS[urlObj.protocol]; | ||
// Remove default port | ||
const port = DEFAULT_PORTS[urlObj.protocol]; | ||
if (Number(urlObj.port) === port) { | ||
@@ -86,3 +84,3 @@ delete urlObj.port; | ||
// remove duplicate slashes | ||
// Remove duplicate slashes | ||
if (urlObj.pathname) { | ||
@@ -92,3 +90,3 @@ urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/'); | ||
// decode URI octets | ||
// Decode URI octets | ||
if (urlObj.pathname) { | ||
@@ -98,3 +96,3 @@ urlObj.pathname = decodeURI(urlObj.pathname); | ||
// remove directory index | ||
// Remove directory index | ||
if (opts.removeDirectoryIndex === true) { | ||
@@ -104,5 +102,5 @@ opts.removeDirectoryIndex = [/^index\.[a-z]+$/]; | ||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length) { | ||
var pathComponents = urlObj.pathname.split('/'); | ||
var lastComponent = pathComponents[pathComponents.length - 1]; | ||
if (Array.isArray(opts.removeDirectoryIndex) && opts.removeDirectoryIndex.length > 0) { | ||
let pathComponents = urlObj.pathname.split('/'); | ||
const lastComponent = pathComponents[pathComponents.length - 1]; | ||
@@ -115,6 +113,6 @@ if (testParameter(lastComponent, opts.removeDirectoryIndex)) { | ||
// resolve relative paths, but only for slashed protocols | ||
// Resolve relative paths, but only for slashed protocols | ||
if (slashedProtocol[urlObj.protocol]) { | ||
var domain = urlObj.protocol + '//' + urlObj.hostname; | ||
var relative = url.resolve(domain, urlObj.pathname); | ||
const domain = urlObj.protocol + '//' + urlObj.hostname; | ||
const relative = url.resolve(domain, urlObj.pathname); | ||
urlObj.pathname = relative.replace(domain, ''); | ||
@@ -127,6 +125,6 @@ } | ||
// remove trailing dot | ||
// Remove trailing dot | ||
urlObj.hostname = urlObj.hostname.replace(/\.$/, ''); | ||
// remove `www.` | ||
// Remove `www.` | ||
if (opts.stripWWW) { | ||
@@ -137,3 +135,3 @@ urlObj.hostname = urlObj.hostname.replace(/^www\./, ''); | ||
// remove URL with empty query string | ||
// Remove URL with empty query string | ||
if (urlObj.search === '?') { | ||
@@ -143,7 +141,7 @@ delete urlObj.search; | ||
var queryParameters = queryString.parse(urlObj.search); | ||
const queryParameters = queryString.parse(urlObj.search); | ||
// remove query unwanted parameters | ||
// Remove query unwanted parameters | ||
if (Array.isArray(opts.removeQueryParameters)) { | ||
for (var key in queryParameters) { | ||
for (const key in queryParameters) { | ||
if (testParameter(key, opts.removeQueryParameters)) { | ||
@@ -155,12 +153,14 @@ delete queryParameters[key]; | ||
// sort query parameters | ||
urlObj.search = queryString.stringify(sortKeys(queryParameters)); | ||
// Sort query parameters | ||
if (opts.sortQueryParameters) { | ||
urlObj.search = queryString.stringify(sortKeys(queryParameters)); | ||
} | ||
// decode query parameters | ||
// Decode query parameters | ||
urlObj.search = decodeURIComponent(urlObj.search); | ||
// take advantage of many of the Node `url` normalizations | ||
// Take advantage of many of the Node `url` normalizations | ||
str = url.format(urlObj); | ||
// remove ending `/` | ||
// Remove ending `/` | ||
if (opts.removeTrailingSlash || urlObj.pathname === '/') { | ||
@@ -170,3 +170,3 @@ str = str.replace(/\/$/, ''); | ||
// restore relative protocol, if applicable | ||
// Restore relative protocol, if applicable | ||
if (hasRelativeProtocol && !opts.normalizeProtocol) { | ||
@@ -173,0 +173,0 @@ str = str.replace(/^http:\/\//, '//'); |
{ | ||
"name": "normalize-url", | ||
"version": "1.9.1", | ||
"version": "2.0.0", | ||
"description": "Normalize a URL", | ||
@@ -27,8 +27,5 @@ "license": "MIT", | ||
"string", | ||
"str", | ||
"normalise", | ||
"normalization", | ||
"normalisation", | ||
"query", | ||
"string", | ||
"querystring", | ||
@@ -42,11 +39,10 @@ "unicode", | ||
"dependencies": { | ||
"object-assign": "^4.0.1", | ||
"prepend-http": "^1.0.0", | ||
"query-string": "^4.1.0", | ||
"sort-keys": "^1.0.0" | ||
"prepend-http": "^2.0.0", | ||
"query-string": "^5.0.1", | ||
"sort-keys": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "^0.16.0" | ||
"xo": "*" | ||
} | ||
} |
# normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) | ||
> [Normalize](http://en.wikipedia.org/wiki/URL_normalization) a URL | ||
> [Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL | ||
@@ -11,3 +11,3 @@ Useful when you need to display, store, deduplicate, sort, compare, etc, URLs. | ||
``` | ||
$ npm install --save normalize-url | ||
$ npm install normalize-url | ||
``` | ||
@@ -41,2 +41,4 @@ | ||
Type: `Object` | ||
##### normalizeProtocol | ||
@@ -150,3 +152,17 @@ | ||
##### sortQueryParameters | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Sort the query parameters alphabetically by key. | ||
```js | ||
normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', { | ||
sortQueryParameters: false | ||
}); | ||
//=> 'http://sindresorhus.com/?b=two&a=one&c=three' | ||
``` | ||
## Related | ||
@@ -153,0 +169,0 @@ |
Sorry, the diff of this file is not supported yet
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
9476
3
173
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addedprepend-http@2.0.0(transitive)
+ Addedquery-string@5.1.1(transitive)
+ Addedsort-keys@2.0.0(transitive)
- Removedobject-assign@^4.0.1
- Removedprepend-http@1.0.4(transitive)
- Removedquery-string@4.3.4(transitive)
- Removedsort-keys@1.1.2(transitive)
Updatedprepend-http@^2.0.0
Updatedquery-string@^5.0.1
Updatedsort-keys@^2.0.0