normalize-url
Advanced tools
Comparing version 4.3.0 to 4.4.0
@@ -135,3 +135,3 @@ declare namespace normalizeUrl { | ||
*/ | ||
readonly removeQueryParameters?: (RegExp | string)[]; | ||
readonly removeQueryParameters?: ReadonlyArray<RegExp | string>; | ||
@@ -173,3 +173,3 @@ /** | ||
*/ | ||
readonly removeDirectoryIndex?: (RegExp | string)[]; | ||
readonly removeDirectoryIndex?: ReadonlyArray<RegExp | string>; | ||
@@ -197,3 +197,3 @@ /** | ||
@param url - URL to normalize. | ||
@param url - URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
@@ -200,0 +200,0 @@ @example |
55
index.js
@@ -9,2 +9,49 @@ 'use strict'; | ||
const normalizeDataURL = urlString => { | ||
const parts = urlString.trim().match(/^data:(.*?),(.*)$/); | ||
if (!parts) { | ||
throw new Error(`Invalid URL: ${urlString}`); | ||
} | ||
const mediaType = parts[1].split(';'); | ||
const body = parts[2]; | ||
let base64 = false; | ||
if (mediaType[mediaType.length - 1] === 'base64') { | ||
mediaType.pop(); | ||
base64 = true; | ||
} | ||
// Lowercase MIME type | ||
const mimeType = (mediaType.shift() || '').toLowerCase(); | ||
const attributes = mediaType | ||
.filter(Boolean) | ||
.map(attribute => { | ||
let [key, value = ''] = attribute.split('=').map(string => string.trim()); | ||
// Lowercase `charset` | ||
if (key === 'charset') { | ||
value = value.toLowerCase(); | ||
} | ||
return `${key}=${value}`; | ||
}); | ||
const normalizedMediaType = [ | ||
...attributes | ||
]; | ||
if (base64) { | ||
normalizedMediaType.push('base64'); | ||
} | ||
if (normalizedMediaType.length !== 0 || mimeType) { | ||
normalizedMediaType.unshift(mimeType); | ||
} | ||
return `data:${normalizedMediaType.join(';')},${base64 ? body.trim() : body}`; | ||
}; | ||
const normalizeUrl = (urlString, options) => { | ||
@@ -45,3 +92,3 @@ options = { | ||
// Prepend protocol | ||
if (!isRelativeUrl) { | ||
if (!isRelativeUrl && !/^data:/i.test(urlString)) { | ||
urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, options.defaultProtocol); | ||
@@ -135,2 +182,8 @@ } | ||
// Data URL | ||
if (urlObj.protocol === 'data:') { | ||
const url = normalizeDataURL(`${urlObj.protocol}${urlObj.pathname}`); | ||
return `${url}${urlObj.search}${urlObj.hash}`; | ||
} | ||
if (options.removeTrailingSlash) { | ||
@@ -137,0 +190,0 @@ urlObj.pathname = urlObj.pathname.replace(/\/$/, ''); |
{ | ||
"name": "normalize-url", | ||
"version": "4.3.0", | ||
"version": "4.4.0", | ||
"description": "Normalize a URL", | ||
@@ -38,8 +38,8 @@ "license": "MIT", | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"coveralls": "^3.0.3", | ||
"nyc": "^13.3.0", | ||
"tsd": "^0.7.2", | ||
"ava": "^2.4.0", | ||
"coveralls": "^3.0.6", | ||
"nyc": "^14.1.1", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -30,3 +30,3 @@ # 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) | ||
### normalizeUrl(url, [options]) | ||
### normalizeUrl(url, options?) | ||
@@ -37,7 +37,7 @@ #### url | ||
URL to normalize. | ||
URL to normalize, including [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
#### options | ||
Type: `Object` | ||
Type: `object` | ||
@@ -54,3 +54,3 @@ ##### defaultProtocol | ||
Prepends `defaultProtocol` to the URL if it's protocol-relative. | ||
Prepend `defaultProtocol` to the URL if it's protocol-relative. | ||
@@ -70,3 +70,3 @@ ```js | ||
Normalizes `https:` URLs to `http:`. | ||
Normalize `https:` to `http:`. | ||
@@ -86,3 +86,3 @@ ```js | ||
Normalizes `http:` URLs to `https:`. | ||
Normalize `http:` to `https:`. | ||
@@ -104,3 +104,3 @@ ```js | ||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of a URL. | ||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL. | ||
@@ -120,3 +120,3 @@ ```js | ||
Removes hash from the URL. | ||
Strip the hash part of the URL. | ||
@@ -136,3 +136,3 @@ ```js | ||
Removes HTTP(S) protocol from an URL `http://sindresorhus.com` → `sindresorhus.com`. | ||
Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`. | ||
@@ -152,3 +152,3 @@ ```js | ||
Removes `www.` from the URL. | ||
Remove `www.` from the URL. | ||
@@ -165,6 +165,6 @@ ```js | ||
Type: `Array<RegExp|string>`<br> | ||
Type: `Array<RegExp | string>`<br> | ||
Default: `[/^utm_\w+/i]` | ||
Removes query parameters that matches any of the provided strings or regexes. | ||
Remove query parameters that matches any of the provided strings or regexes. | ||
@@ -183,3 +183,3 @@ ```js | ||
Removes trailing slash. | ||
Remove trailing slash. | ||
@@ -201,3 +201,3 @@ **Note:** Trailing slash is always removed if the URL doesn't have a pathname. | ||
Type: `boolean` `Array<RegExp|string>`<br> | ||
Type: `boolean | Array<RegExp | string>`<br> | ||
Default: `false` | ||
@@ -234,4 +234,12 @@ | ||
## License | ||
--- | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-normalize-url?utm_source=npm-normalize-url&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
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
17815
334
233