Skip to content

Commit

Permalink
ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 11, 2017
1 parent 7e32757 commit 48b1864
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 53 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ language: node_js
node_js:
- '6'
- '4'
- '0.12'
- '0.10'
91 changes: 44 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
'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');

var DEFAULT_PORTS = {
const url = require('url');
const punycode = require('punycode');
const queryString = require('query-string');
const prependHttp = require('prepend-http');
const sortKeys = require('sort-keys');

const DEFAULT_PORTS = {
'http:': 80,
'https:': 443,
'ftp:': 21
};

// 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,
'https:': true,
'ftp:': true,
Expand All @@ -27,13 +26,11 @@ var slashedProtocol = {
};

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,
normalizeHttps: false,
stripFragment: true,
Expand All @@ -47,12 +44,12 @@ module.exports = function (str, opts) {
throw new TypeError('Expected a string');
}

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

if (opts.normalizeHttps && urlObj.protocol === 'https:') {
urlObj.protocol = 'http:';
Expand All @@ -62,97 +59,97 @@ module.exports = function (str, opts) {
throw new Error('Invalid URL');
}

// 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) {
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) {
delete urlObj.port;
}

// remove duplicate slashes
// Remove duplicate slashes
if (urlObj.pathname) {
urlObj.pathname = urlObj.pathname.replace(/\/{2,}/g, '/');
}

// decode URI octets
// Decode URI octets
if (urlObj.pathname) {
urlObj.pathname = decodeURI(urlObj.pathname);
}

// remove directory index
// Remove directory index
if (opts.removeDirectoryIndex === true) {
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];

if (testParameter(lastComponent, opts.removeDirectoryIndex)) {
pathComponents = pathComponents.slice(0, pathComponents.length - 1);
urlObj.pathname = pathComponents.slice(1).join('/') + '/';
}
}

// 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, '');
}

if (urlObj.hostname) {
// IDN to Unicode
urlObj.hostname = punycode.toUnicode(urlObj.hostname).toLowerCase();

// remove trailing dot
// Remove trailing dot
urlObj.hostname = urlObj.hostname.replace(/\.$/, '');

// remove `www.`
// Remove `www.`
if (opts.stripWWW) {
urlObj.hostname = urlObj.hostname.replace(/^www\./, '');
}
}

// remove URL with empty query string
// Remove URL with empty query string
if (urlObj.search === '?') {
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)) {
delete queryParameters[key];
}
}
}

// sort query parameters
// Sort query parameters
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 === '/') {
str = str.replace(/\/$/, '');
}

// restore relative protocol, if applicable
// Restore relative protocol, if applicable
if (hasRelativeProtocol && !opts.normalizeProtocol) {
str = str.replace(/^http:\/\//, '//');
}
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"normalization",
"normalisation",
"query",
"string",
"querystring",
"unicode",
"simplify",
Expand All @@ -38,13 +37,12 @@
"canonical"
],
"dependencies": {
"object-assign": "^4.0.1",
"prepend-http": "^1.0.0",
"query-string": "^4.1.0",
"sort-keys": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "^0.16.0"
"xo": "*"
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ URL to normalize.

#### options

Type: `Object`

##### normalizeProtocol

Type: `boolean`<br>
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import m from './';
import m from '.';

test('main', t => {
t.is(m('sindresorhus.com'), 'http://sindresorhus.com');
Expand Down

0 comments on commit 48b1864

Please sign in to comment.