Skip to content

Commit fc53abf

Browse files
committed
docs: add esm examples
1 parent ac2c8e1 commit fc53abf

File tree

1 file changed

+70
-5
lines changed

1 file changed

+70
-5
lines changed

doc/api/url.md

+70-5
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,22 @@ added: v10.12.0
10121012
This function ensures the correct decodings of percent-encoded characters as
10131013
well as ensuring a cross-platform valid absolute path string.
10141014

1015-
```js
1015+
```mjs
1016+
import url from 'url';
1017+
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
1018+
url.fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
1019+
1020+
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
1021+
url.fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
1022+
1023+
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1024+
url.fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
1025+
1026+
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
1027+
url.fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
1028+
```
1029+
1030+
```cjs
10161031
const url = require('url');
10171032
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
10181033
url.fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
@@ -1053,7 +1068,21 @@ string serializations of the URL. These are not, however, customizable in
10531068
any way. The `url.format(URL[, options])` method allows for basic customization
10541069
of the output.
10551070

1056-
```js
1071+
```mjs
1072+
import url from 'url';
1073+
const myURL = new URL('https://a:b@測試?abc#foo');
1074+
1075+
console.log(myURL.href);
1076+
// Prints https://a:b@xn--g6w251d/?abc#foo
1077+
1078+
console.log(myURL.toString());
1079+
// Prints https://a:b@xn--g6w251d/?abc#foo
1080+
1081+
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
1082+
// Prints 'https://測試/?abc'
1083+
```
1084+
1085+
```cjs
10571086
const url = require('url');
10581087
const myURL = new URL('https://a:b@測試?abc#foo');
10591088

@@ -1078,7 +1107,21 @@ added: v10.12.0
10781107
This function ensures that `path` is resolved absolutely, and that the URL
10791108
control characters are correctly encoded when converting into a File URL.
10801109

1081-
```js
1110+
```mjs
1111+
import url from 'url';
1112+
new URL(__filename); // Incorrect: throws (POSIX)
1113+
new URL(__filename); // Incorrect: C:\... (Windows)
1114+
url.pathToFileURL(__filename); // Correct: file:///... (POSIX)
1115+
url.pathToFileURL(__filename); // Correct: file:///C:/... (Windows)
1116+
1117+
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
1118+
url.pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
1119+
1120+
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
1121+
url.pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
1122+
```
1123+
1124+
```cjs
10821125
const url = require('url');
10831126
new URL(__filename); // Incorrect: throws (POSIX)
10841127
new URL(__filename); // Incorrect: C:\... (Windows)
@@ -1319,7 +1362,22 @@ changes:
13191362
The `url.format()` method returns a formatted URL string derived from
13201363
`urlObject`.
13211364

1322-
```js
1365+
```mjs
1366+
import url from 'url';
1367+
url.format({
1368+
protocol: 'https',
1369+
hostname: 'example.com',
1370+
pathname: '/some/path',
1371+
query: {
1372+
page: 1,
1373+
format: 'json'
1374+
}
1375+
});
1376+
1377+
// => 'https://example.com/some/path?page=1&format=json'
1378+
```
1379+
1380+
```cjs
13231381
const url = require('url');
13241382
url.format({
13251383
protocol: 'https',
@@ -1473,7 +1531,14 @@ changes:
14731531
The `url.resolve()` method resolves a target URL relative to a base URL in a
14741532
manner similar to that of a Web browser resolving an anchor tag HREF.
14751533

1476-
```js
1534+
```mjs
1535+
import url from 'url';
1536+
url.resolve('/one/two/three', 'four'); // '/one/two/four'
1537+
url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
1538+
url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'
1539+
```
1540+
1541+
```cjs
14771542
const url = require('url');
14781543
url.resolve('/one/two/three', 'four'); // '/one/two/four'
14791544
url.resolve('http://example.com/', '/one'); // 'http://example.com/one'

0 commit comments

Comments
 (0)