Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 31, 2020
1 parent 7b4ad64 commit 76172da
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 84 deletions.
41 changes: 17 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,22 @@ declare namespace normalizeUrl {
}
}

declare const normalizeUrl: {
/**
[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).
@example
```
import normalizeUrl = require('normalize-url');
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;

// 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;
};
/**
[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).
@example
```
import normalizeUrl = require('normalize-url');
normalizeUrl('sindresorhus.com');
//=> 'http://sindresorhus.com'
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;

export = normalizeUrl;
46 changes: 11 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand All @@ -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
Expand Down Expand Up @@ -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.

Copy link
@nghiepdev

nghiepdev Feb 2, 2020

Hi,
SyntaxError: Invalid regular expression: invalid group specifier name
negative lookbehind does not support in safari.

This comment has been minimized.

Copy link
@TrySound

TrySound Feb 2, 2020

Contributor

@nghiepit Open an issue please

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Feb 2, 2020

Author Owner

Sorry, but don't open an issue. This is not a browser package. It's up to you to transpile/polyfill if you need to use it in the browser. I would recommend just staying at the previous version.

This comment has been minimized.

Copy link
@TrySound

TrySound Feb 2, 2020

Contributor

I would be good to transpile but I do not see babel-preset-env does anything here. Do you know any babel plugin to transpile this feature?

This comment has been minimized.

Copy link
@TrySound

TrySound Feb 2, 2020

Contributor

@sindresorhus Looks like it's not possible to use normalize-url in browsers anymore.

Lookbehind assertions cannot be transpiled

babel/babel#11086 (comment)

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Feb 2, 2020

Author Owner

This comment has been minimized.

Copy link
@TrySound

TrySound Feb 2, 2020

Contributor

😩

}

// Decode URI octets
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -217,5 +195,3 @@ const normalizeUrl = (urlString, options) => {
};

module.exports = normalizeUrl;
// TODO: Remove this for the next major release
module.exports.default = normalizeUrl;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "sindresorhus@gmail.com",
Expand Down Expand Up @@ -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"
}
}
28 changes: 12 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
Useful when you need to display, store, deduplicate, sort, compare, etc, URLs.


## Install

```
$ npm install normalize-url
```


## Usage

```js
Expand All @@ -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?)
Expand All @@ -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.
Expand All @@ -61,7 +58,7 @@ normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false});

##### forceHttp

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Normalize `https:` to `http:`.
Expand All @@ -76,7 +73,7 @@ normalizeUrl('https://sindresorhus.com:80/', {forceHttp: true});

##### forceHttps

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Normalize `http:` to `https:`.
Expand All @@ -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.
Expand All @@ -108,7 +105,7 @@ normalizeUrl('user:password@sindresorhus.com', {stripAuthentication: false});

##### stripHash

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Strip the hash part of the URL.
Expand All @@ -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`.
Expand All @@ -138,7 +135,7 @@ normalizeUrl('https://sindresorhus.com', {stripProtocol: true});

##### stripWWW

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Remove `www.` from the URL.
Expand All @@ -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.
Expand All @@ -167,7 +164,7 @@ normalizeUrl('www.sindresorhus.com?foo=bar&ref=test_ref', {

##### removeTrailingSlash

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Remove trailing slash.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,6 @@ test('remove duplicate pathname slashes', t => {
t.is(normalizeUrl('http://sindresorhus.com//foo'), 'http://sindresorhus.com/foo');
});

test('deprecated options', t => {
t.throws(() => normalizeUrl('', {normalizeHttps: true}), 'options.normalizeHttps is renamed to options.forceHttp');
t.throws(() => normalizeUrl('', {normalizeHttp: true}), 'options.normalizeHttp is renamed to options.forceHttps');
t.throws(() => normalizeUrl('', {stripFragment: true}), 'options.stripFragment is renamed to options.stripHash');
});

test('data URL', t => {
// Invalid URL.
t.throws(() => normalizeUrl('data:'), 'Invalid URL: data:');
Expand Down

0 comments on commit 76172da

Please sign in to comment.