-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: add ESM code examples in url.md
PR-URL: #38651 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Masashi Hirano <[email protected]>
- Loading branch information
Showing
1 changed file
with
66 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
``` | ||
|
||
|
@@ -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:[email protected]:8080/p/a/t/h?query=string#hash'); | ||
``` | ||
|
||
```cjs | ||
const url = require('url'); | ||
const myURL = | ||
url.parse('https://user:[email protected]:8080/p/a/t/h?query=string#hash'); | ||
|
@@ -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'. | ||
``` | ||
|
||
|
@@ -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} | ||
|
@@ -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')); | ||
|
@@ -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')); | ||
|
@@ -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'); | ||
|
||
|
@@ -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` | ||
|
||
|
@@ -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 | ||
|