normalize-url
Advanced tools
Comparing version 4.5.0 to 5.0.0
@@ -191,27 +191,20 @@ declare namespace normalizeUrl { | ||
declare const normalizeUrl: { | ||
/** | ||
[Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL. | ||
/** | ||
[Normalize](https://en.wikipedia.org/wiki/URL_normalization) a URL. | ||
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
@example | ||
``` | ||
import normalizeUrl = require('normalize-url'); | ||
@example | ||
``` | ||
import normalizeUrl = require('normalize-url'); | ||
normalizeUrl('sindresorhus.com'); | ||
//=> 'http://sindresorhus.com' | ||
normalizeUrl('sindresorhus.com'); | ||
//=> 'http://sindresorhus.com' | ||
normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'); | ||
//=> 'http://êxample.com/?a=foo&b=bar' | ||
``` | ||
*/ | ||
(url: string, options?: normalizeUrl.Options): string; | ||
normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'); | ||
//=> 'http://êxample.com/?a=foo&b=bar' | ||
``` | ||
*/ | ||
declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string; | ||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function normalizeUrl(url: string, options?: normalizeUrl.Options): string; | ||
// export = normalizeUrl; | ||
default: typeof normalizeUrl; | ||
}; | ||
export = normalizeUrl; |
54
index.js
'use strict'; | ||
// TODO: Use the `URL` global when targeting Node.js 10 | ||
const URLParser = typeof URL === 'undefined' ? require('url').URL : URL; | ||
@@ -14,17 +12,16 @@ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | ||
const normalizeDataURL = (urlString, {stripHash}) => { | ||
const parts = urlString.match(/^data:(.*?),(.*?)(?:#(.*))?$/); | ||
const match = /^data:(?<type>.*?),(?<data>.*?)(?:#(?<hash>.*))?$/.exec(urlString); | ||
if (!parts) { | ||
if (!match) { | ||
throw new Error(`Invalid URL: ${urlString}`); | ||
} | ||
const mediaType = parts[1].split(';'); | ||
const body = parts[2]; | ||
const hash = stripHash ? '' : parts[3]; | ||
let {type, data, hash} = match.groups; | ||
const mediaType = type.split(';'); | ||
hash = stripHash ? '' : hash; | ||
let base64 = false; | ||
let isBase64 = false; | ||
if (mediaType[mediaType.length - 1] === 'base64') { | ||
mediaType.pop(); | ||
base64 = true; | ||
isBase64 = true; | ||
} | ||
@@ -55,3 +52,3 @@ | ||
if (base64) { | ||
if (isBase64) { | ||
normalizedMediaType.push('base64'); | ||
@@ -64,3 +61,3 @@ } | ||
return `data:${normalizedMediaType.join(';')},${base64 ? body.trim() : body}${hash ? `#${hash}` : ''}`; | ||
return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`; | ||
}; | ||
@@ -84,15 +81,2 @@ | ||
// TODO: Remove this at some point in the future | ||
if (Reflect.has(options, 'normalizeHttps')) { | ||
throw new Error('options.normalizeHttps is renamed to options.forceHttp'); | ||
} | ||
if (Reflect.has(options, 'normalizeHttp')) { | ||
throw new Error('options.normalizeHttp is renamed to options.forceHttps'); | ||
} | ||
if (Reflect.has(options, 'stripFragment')) { | ||
throw new Error('options.stripFragment is renamed to options.stripHash'); | ||
} | ||
urlString = urlString.trim(); | ||
@@ -113,3 +97,3 @@ | ||
const urlObj = new URLParser(urlString); | ||
const urlObj = new URL(urlString); | ||
@@ -141,11 +125,3 @@ if (options.forceHttp && options.forceHttps) { | ||
if (urlObj.pathname) { | ||
// TODO: Use the following instead when targeting Node.js 10 | ||
// `urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/');` | ||
urlObj.pathname = urlObj.pathname.replace(/((?!:).|^)\/{2,}/g, (_, p1) => { | ||
if (/^(?!\/)/g.test(p1)) { | ||
return `${p1}/`; | ||
} | ||
return '/'; | ||
}); | ||
urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/'); | ||
} | ||
@@ -155,3 +131,5 @@ | ||
if (urlObj.pathname) { | ||
urlObj.pathname = decodeURI(urlObj.pathname); | ||
try { | ||
urlObj.pathname = decodeURI(urlObj.pathname); | ||
} catch (_) {} | ||
} | ||
@@ -179,3 +157,3 @@ | ||
// Remove `www.` | ||
if (options.stripWWW && /^www\.([a-z\-\d]{2,63})\.([a-z.]{2,5})$/.test(urlObj.hostname)) { | ||
if (options.stripWWW && /^www\.(?:[a-z\-\d]{2,63})\.(?:[a-z.]{2,5})$/.test(urlObj.hostname)) { | ||
// Each label should be max 63 at length (min: 2). | ||
@@ -228,3 +206,1 @@ // The extension should be max 5 at length (min: 2). | ||
module.exports = normalizeUrl; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = normalizeUrl; |
{ | ||
"name": "normalize-url", | ||
"version": "4.5.0", | ||
"version": "5.0.0", | ||
"description": "Normalize a URL", | ||
"license": "MIT", | ||
"repository": "sindresorhus/normalize-url", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
@@ -13,3 +14,3 @@ "name": "Sindre Sorhus", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -41,6 +42,6 @@ "scripts": { | ||
"coveralls": "^3.0.6", | ||
"nyc": "^14.1.1", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
"nyc": "^15.0.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -7,3 +7,2 @@ # normalize-url [![Build Status](https://travis-ci.org/sindresorhus/normalize-url.svg?branch=master)](https://travis-ci.org/sindresorhus/normalize-url) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/normalize-url/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/normalize-url?branch=master) | ||
## Install | ||
@@ -15,3 +14,2 @@ | ||
## Usage | ||
@@ -29,3 +27,2 @@ | ||
## API | ||
@@ -47,3 +44,3 @@ | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `http:` | ||
@@ -53,3 +50,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -69,3 +66,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -85,3 +82,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -103,3 +100,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -119,3 +116,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -135,3 +132,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -145,3 +142,3 @@ | ||
normalizeUrl('sindresorhus.com', {stripProtocol: true}); | ||
normalizeUrl('https://sindresorhus.com', {stripProtocol: true}); | ||
//=> 'sindresorhus.com' | ||
@@ -152,3 +149,3 @@ ``` | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -168,3 +165,3 @@ | ||
Type: `Array<RegExp | string>`<br> | ||
Type: `Array<RegExp | string>`\ | ||
Default: `[/^utm_\w+/i]` | ||
@@ -183,3 +180,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -204,3 +201,3 @@ | ||
Type: `boolean | Array<RegExp | string>`<br> | ||
Type: `boolean | Array<RegExp | string>`\ | ||
Default: `false` | ||
@@ -219,3 +216,3 @@ | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
@@ -232,3 +229,2 @@ | ||
## Related | ||
@@ -235,0 +231,0 @@ |
17059
315
229