Socket
Socket
Sign inDemoInstall

normalize-url

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-url - npm Package Compare versions

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;
};

3

package.json
{
"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)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc