Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
fisker and aduh95 committed May 19, 2021
1 parent 1f8c002 commit 3f76186
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions doc/api/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,9 @@ well as ensuring a cross-platform valid absolute path string.

```mjs
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);

new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)

Expand Down Expand Up @@ -1069,7 +1072,7 @@ any way. The `url.format(URL[, options])` method allows for basic customization
of the output.
```mjs
import {format} from 'url';
import url from 'url';
const myURL = new URL('https://a:b@測試?abc#foo');

console.log(myURL.href);
Expand All @@ -1078,12 +1081,12 @@ console.log(myURL.href);
console.log(myURL.toString());
// Prints https://a:b@xn--g6w251d/?abc#foo

console.log(format(myURL, { fragment: false, unicode: true, auth: false }));
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
// Prints 'https://測試/?abc'
```
```cjs
const {format} = require('url');
const url = require('url');
const myURL = new URL('https://a:b@測試?abc#foo');

console.log(myURL.href);
Expand All @@ -1092,7 +1095,7 @@ console.log(myURL.href);
console.log(myURL.toString());
// Prints https://a:b@xn--g6w251d/?abc#foo

console.log(format(myURL, { fragment: false, unicode: true, auth: false }));
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
// Prints 'https://測試/?abc'
```
Expand All @@ -1108,12 +1111,7 @@ This function ensures that `path` is resolved absolutely, and that the URL
control characters are correctly encoded when converting into a File URL.
```mjs
import {pathToFileURL, fileURLToPath} from 'url';
const filename = fileURLToPath(import.meta.url);
new URL(filename); // Incorrect: throws (POSIX)
new URL(filename); // Incorrect: C:\... (Windows)
pathToFileURL(filename); // Correct: file:///... (POSIX)
pathToFileURL(filename); // Correct: file:///C:/... (Windows)
import { pathToFileURL } from 'url';

new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
Expand All @@ -1123,7 +1121,7 @@ pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSI
```
```cjs
const url = require('url');
const { pathToFileURL } = require('url');
new URL(__filename); // Incorrect: throws (POSIX)
new URL(__filename); // Incorrect: C:\... (Windows)
pathToFileURL(__filename); // Correct: file:///... (POSIX)
Expand Down Expand Up @@ -1364,8 +1362,8 @@ The `url.format()` method returns a formatted URL string derived from
`urlObject`.
```js
const {format} = require('url');
format({
const url = require('url');
url.format({
protocol: 'https',
hostname: 'example.com',
pathname: '/some/path',
Expand Down

0 comments on commit 3f76186

Please sign in to comment.