diff --git a/doc/api/url.md b/doc/api/url.md index 3c879390469a54..27b3b69e4447d2 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -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) @@ -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); @@ -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); @@ -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' ``` @@ -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) @@ -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) @@ -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',