normalize-url
Advanced tools
Comparing version 1.0.2 to 1.1.0
23
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
var sortKeys = require('sort-keys'); | ||
var objectAssign = require('object-assign'); | ||
@@ -15,3 +16,5 @@ var DEFAULT_PORTS = { | ||
module.exports = function (str) { | ||
module.exports = function (str, opts) { | ||
opts = objectAssign({normalizeProtocol: true}, opts); | ||
if (typeof str !== 'string') { | ||
@@ -21,2 +24,4 @@ throw new TypeError('Expected a string'); | ||
var hasRelativeProtocol = str.indexOf('//') === 0; | ||
// prepend protocol | ||
@@ -31,2 +36,5 @@ str = prependHttp(str.trim()).replace(/^\/\//, 'http://'); | ||
// remove fragment | ||
delete urlObj.hash; | ||
// remove default port | ||
@@ -38,2 +46,10 @@ var port = DEFAULT_PORTS[urlObj.protocol]; | ||
// remove duplicate slashes | ||
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/, '/'); | ||
// resolve relative paths | ||
var domain = urlObj.protocol + '//' + urlObj.hostname; | ||
var relative = url.resolve(domain, urlObj.pathname); | ||
urlObj.pathname = relative.replace(domain, ''); | ||
// IDN to Unicode | ||
@@ -62,3 +78,8 @@ urlObj.hostname = punycode.toUnicode(urlObj.hostname).toLowerCase(); | ||
// restore relative protocol, if applicable | ||
if (hasRelativeProtocol && !opts.normalizeProtocol) { | ||
str = str.replace(/^http:\/\//, '//'); | ||
} | ||
return str; | ||
}; |
{ | ||
"name": "normalize-url", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Normalize a URL", | ||
@@ -35,2 +35,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"object-assign": "^2.0.0", | ||
"prepend-http": "^1.0.0", | ||
@@ -37,0 +38,0 @@ "query-string": "^1.0.0", |
@@ -28,4 +28,30 @@ # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) | ||
## API | ||
### normalizeUrl(url, [options]) | ||
#### url | ||
*Required* | ||
Type: `string` | ||
URL to normalize. | ||
#### options | ||
##### normalizeProtocol | ||
Type: `boolean` | ||
Default: `true` | ||
Prepend `http:` to protocol-relative URLs. | ||
```js | ||
normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}); | ||
//=> //sindresorhus.com | ||
``` | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
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
59
57
3703
4
3
+ Addedobject-assign@^2.0.0
+ Addedobject-assign@2.1.1(transitive)