-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,20 @@ const testParameter = (name, filters) => { | |
}; | ||
|
||
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 base64 = false; | ||
let {type, data, hash} = match.groups; | ||
const mediaType = type.split(';'); | ||
hash = stripHash ? '' : hash; | ||
|
||
let isBase64 = false; | ||
if (mediaType[mediaType.length - 1] === 'base64') { | ||
mediaType.pop(); | ||
base64 = true; | ||
isBase64 = true; | ||
} | ||
|
||
// Lowercase MIME type | ||
|
@@ -49,15 +48,15 @@ const normalizeDataURL = (urlString, {stripHash}) => { | |
...attributes | ||
]; | ||
|
||
if (base64) { | ||
if (isBase64) { | ||
normalizedMediaType.push('base64'); | ||
} | ||
|
||
if (normalizedMediaType.length !== 0 || (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE)) { | ||
normalizedMediaType.unshift(mimeType); | ||
} | ||
|
||
return `data:${normalizedMediaType.join(';')},${base64 ? body.trim() : body}${hash ? `#${hash}` : ''}`; | ||
return `data:${normalizedMediaType.join(';')},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ''}`; | ||
}; | ||
|
||
const normalizeUrl = (urlString, options) => { | ||
|
@@ -76,19 +75,6 @@ const normalizeUrl = (urlString, options) => { | |
...options | ||
}; | ||
|
||
// 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(); | ||
|
||
// Data URL | ||
|
@@ -131,15 +117,7 @@ const normalizeUrl = (urlString, options) => { | |
|
||
// Remove duplicate slashes if not preceded by a protocol | ||
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, '/'); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sindresorhus
Author
Owner
|
||
} | ||
|
||
// Decode URI octets | ||
|
@@ -169,7 +147,7 @@ const normalizeUrl = (urlString, options) => { | |
urlObj.hostname = urlObj.hostname.replace(/\.$/, ''); | ||
|
||
// 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). | ||
// The extension should be max 5 at length (min: 2). | ||
// Source: https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names | ||
|
@@ -217,5 +195,3 @@ const normalizeUrl = (urlString, options) => { | |
}; | ||
|
||
module.exports = normalizeUrl; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = normalizeUrl; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"description": "Normalize a URL", | ||
"license": "MIT", | ||
"repository": "sindresorhus/normalize-url", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "[email protected]", | ||
|
@@ -37,8 +38,8 @@ | |
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"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" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,12 @@ | |
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs. | ||
|
||
|
||
## Install | ||
|
||
``` | ||
$ npm install normalize-url | ||
``` | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
|
@@ -24,7 +22,6 @@ normalizeUrl('HTTP://xn--xample-hva.com:80/?b=bar&a=foo'); | |
//=> 'http://êxample.com/?a=foo&b=bar' | ||
``` | ||
|
||
|
||
## API | ||
|
||
### normalizeUrl(url, options?) | ||
|
@@ -41,12 +38,12 @@ Type: `object` | |
|
||
##### defaultProtocol | ||
|
||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `http:` | ||
|
||
##### normalizeProtocol | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Prepend `defaultProtocol` to the URL if it's protocol-relative. | ||
|
@@ -61,7 +58,7 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}); | |
|
||
##### forceHttp | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Normalize `https:` to `http:`. | ||
|
@@ -76,7 +73,7 @@ normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true}); | |
|
||
##### forceHttps | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Normalize `http:` to `https:`. | ||
|
@@ -93,7 +90,7 @@ This option can't be used with the `forceHttp` option at the same time. | |
|
||
##### stripAuthentication | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Strip the [authentication](https://en.wikipedia.org/wiki/Basic_access_authentication) part of the URL. | ||
|
@@ -108,7 +105,7 @@ normalizeUrl('user:[email protected]', {stripAuthentication: false}); | |
|
||
##### stripHash | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Strip the hash part of the URL. | ||
|
@@ -123,7 +120,7 @@ normalizeUrl('sindresorhus.com/about.html#contact', {stripHash: true}); | |
|
||
##### stripProtocol | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
|
||
Remove HTTP(S) protocol from the URL: `http://sindresorhus.com` → `sindresorhus.com`. | ||
|
@@ -138,7 +135,7 @@ normalizeUrl('https://sindresorhus.com', {stripProtocol: true}); | |
|
||
##### stripWWW | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Remove `www.` from the URL. | ||
|
@@ -153,7 +150,7 @@ normalizeUrl('http://www.sindresorhus.com', {stripWWW: false}); | |
|
||
##### removeQueryParameters | ||
|
||
Type: `Array<RegExp | string>`<br> | ||
Type: `Array<RegExp | string>`\ | ||
Default: `[/^utm_\w+/i]` | ||
|
||
Remove query parameters that matches any of the provided strings or regexes. | ||
|
@@ -167,7 +164,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', { | |
|
||
##### removeTrailingSlash | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Remove trailing slash. | ||
|
@@ -187,7 +184,7 @@ normalizeUrl('http://sindresorhus.com/', {removeTrailingSlash: false}); | |
|
||
##### removeDirectoryIndex | ||
|
||
Type: `boolean | Array<RegExp | string>`<br> | ||
Type: `boolean | Array<RegExp | string>`\ | ||
Default: `false` | ||
|
||
Removes the default directory index file from path that matches any of the provided strings or regexes. When `true`, the regex `/^index\.[a-z]+$/` is used. | ||
|
@@ -201,7 +198,7 @@ normalizeUrl('www.sindresorhus.com/foo/default.php', { | |
|
||
##### sortQueryParameters | ||
|
||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `true` | ||
|
||
Sorts the query parameters alphabetically by key. | ||
|
@@ -213,7 +210,6 @@ normalizeUrl('www.sindresorhus.com?b=two&a=one&c=three', { | |
//=> 'http://sindresorhus.com/?b=two&a=one&c=three' | ||
``` | ||
|
||
|
||
## Related | ||
|
||
- [compare-urls](https://github.com/sindresorhus/compare-urls) - Compare URLs by first normalizing them | ||
|
Hi,
SyntaxError: Invalid regular expression: invalid group specifier name
negative lookbehind
does not support in safari.