Skip to content

Commit

Permalink
doc: add ESM code examples in url.md
Browse files Browse the repository at this point in the history
PR-URL: #38651
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Masashi Hirano <shisama07@gmail.com>
  • Loading branch information
aduh95 authored and targos committed May 17, 2021
1 parent 5a0b521 commit 6620a31
Showing 1 changed file with 66 additions and 13 deletions.
79 changes: 66 additions & 13 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
The `url` module provides utilities for URL resolution and parsing. It can be
accessed using:

```js
```mjs
import url from 'url';
```

```cjs
const url = require('url');
```

Expand Down Expand Up @@ -61,7 +65,13 @@ const myURL =

Parsing the URL string using the Legacy API:

```js
```mjs
import url from 'url';
const myURL =
url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');
```

```cjs
const url = require('url');
const myURL =
url.parse('https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash');
Expand Down Expand Up @@ -135,7 +145,12 @@ const myURL = new URL('/foo', 'https://example.org/');
The URL constructor is accessible as a property on the global object.
It can also be imported from the built-in url module:

```js
```mjs
import { URL } from 'url';
console.log(URL === globalThis.URL); // Prints 'true'.
```

```cjs
console.log(URL === require('url').URL); // Prints 'true'.
```

Expand Down Expand Up @@ -573,10 +588,6 @@ and [`url.format()`][] methods would produce.
The `toString()` method on the `URL` object returns the serialized URL. The
value returned is equivalent to that of [`url.href`][] and [`url.toJSON()`][].

Because of the need for standard compliance, this method does not allow users
to customize the serialization process of the URL. For more flexibility,
[`require('url').format()`][] method might be of interest.

#### `url.toJSON()`

* Returns: {string}
Expand Down Expand Up @@ -931,8 +942,20 @@ invalid domain, the empty string is returned.

It performs the inverse operation to [`url.domainToUnicode()`][].

```js
```mjs
import url from 'url';

console.log(url.domainToASCII('español.com'));
// Prints xn--espaol-zwa.com
console.log(url.domainToASCII('中文.com'));
// Prints xn--fiq228c.com
console.log(url.domainToASCII('xn--iñvalid.com'));
// Prints an empty string
```

```cjs
const url = require('url');

console.log(url.domainToASCII('español.com'));
// Prints xn--espaol-zwa.com
console.log(url.domainToASCII('中文.com'));
Expand All @@ -956,8 +979,20 @@ domain, the empty string is returned.

It performs the inverse operation to [`url.domainToASCII()`][].

```js
```mjs
import url from 'url';

console.log(url.domainToUnicode('xn--espaol-zwa.com'));
// Prints español.com
console.log(url.domainToUnicode('xn--fiq228c.com'));
// Prints 中文.com
console.log(url.domainToUnicode('xn--iñvalid.com'));
// Prints an empty string
```

```cjs
const url = require('url');

console.log(url.domainToUnicode('xn--espaol-zwa.com'));
// Prints español.com
console.log(url.domainToUnicode('xn--fiq228c.com'));
Expand Down Expand Up @@ -1079,7 +1114,26 @@ added: v15.7.0
This utility function converts a URL object into an ordinary options object as
expected by the [`http.request()`][] and [`https.request()`][] APIs.

```js
```mjs
import { urlToHttpOptions } from 'url';
const myURL = new URL('https://a:b@測試?abc#foo');

console.log(urlToHttpOptions(myUrl));
/**
{
protocol: 'https:',
hostname: 'xn--g6w251d',
hash: '#foo',
search: '?abc',
pathname: '/',
path: '/?abc',
href: 'https://a:b@xn--g6w251d/?abc#foo',
auth: 'a:b'
}
*/
```

```cjs
const { urlToHttpOptions } = require('url');
const myURL = new URL('https://a:b@測試?abc#foo');

Expand Down Expand Up @@ -1124,8 +1178,8 @@ changes:

> Stability: 3 - Legacy: Use the WHATWG URL API instead.
The legacy `urlObject` (`require('url').Url`) is created and returned by the
`url.parse()` function.
The legacy `urlObject` (`require('url').Url` or `import { Url } from 'url'`) is
created and returned by the `url.parse()` function.

#### `urlObject.auth`

Expand Down Expand Up @@ -1499,7 +1553,6 @@ console.log(myURL.origin);
[`https.request()`]: https.md#https_https_request_options_callback
[`new URL()`]: #url_new_url_input_base
[`querystring`]: querystring.md
[`require('url').format()`]: #url_url_format_url_options
[`url.domainToASCII()`]: #url_url_domaintoascii_domain
[`url.domainToUnicode()`]: #url_url_domaintounicode_domain
[`url.format()`]: #url_url_format_urlobject
Expand Down

0 comments on commit 6620a31

Please sign in to comment.