@@ -959,18 +959,37 @@ added: v10.12.0
959959This function ensures the correct decodings of percent-encoded characters as
960960well as ensuring a cross-platform valid absolute path string.
961961
962- ``` js
963- new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
964- fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
962+ ``` mjs
963+ import { fileURLToPath } from ' url' ;
964+
965+ const __filename = fileURLToPath (import .meta.url);
966+
967+ new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
968+ fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
969+
970+ new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
971+ fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
972+
973+ new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
974+ fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
975+
976+ new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
977+ fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
978+ ` ` `
965979
966- new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
967- fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
980+ ` ` ` cjs
981+ const { fileURLToPath } = require (' url' );
982+ new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
983+ fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
968984
969- new URL (' file:///你好 .txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD .txt
970- fileURLToPath (' file:///你好 .txt' ); // Correct: /你好 .txt (POSIX )
985+ new URL (' file://nas/foo .txt' ).pathname ; // Incorrect: /foo .txt
986+ fileURLToPath (' file://nas/foo .txt' ); // Correct: \\nas\foo .txt (Windows )
971987
972- new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
973- fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
988+ new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
989+ fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
990+
991+ new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
992+ fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
974993` ` `
975994
976995### ` url .format (URL [, options])`
@@ -999,7 +1018,22 @@ string serializations of the URL. These are not, however, customizable in
9991018any way. The ` url .format (URL [, options])` method allows for basic customization
10001019of the output.
10011020
1002- ``` js
1021+ ` ` ` mjs
1022+ import url from ' url' ;
1023+ const myURL = new URL (' https://a:b@測試?abc#foo' );
1024+
1025+ console .log (myURL .href );
1026+ // Prints https://a:b@xn--g6w251d/?abc#foo
1027+
1028+ console .log (myURL .toString ());
1029+ // Prints https://a:b@xn--g6w251d/?abc#foo
1030+
1031+ console .log (url .format (myURL, { fragment: false , unicode: true , auth: false }));
1032+ // Prints 'https://測試/?abc'
1033+ ` ` `
1034+
1035+ ` ` ` cjs
1036+ const url = require (' url' );
10031037const myURL = new URL (' https://a:b@測試?abc#foo' );
10041038
10051039console .log (myURL .href );
@@ -1023,17 +1057,28 @@ added: v10.12.0
10231057This function ensures that ` path` is resolved absolutely, and that the URL
10241058control characters are correctly encoded when converting into a File URL.
10251059
1026- ``` js
1027- new URL (__filename ); // Incorrect: throws (POSIX)
1028- new URL (__filename ); // Incorrect: C:\... (Windows)
1029- pathToFileURL (__filename ); // Correct: file:///... (POSIX)
1030- pathToFileURL (__filename ); // Correct: file:///C:/... (Windows)
1060+ ` ` ` mjs
1061+ import { pathToFileURL } from ' url' ;
10311062
1032- new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1033- pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1063+ new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1064+ pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
10341065
1035- new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1036- pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1066+ new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1067+ pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1068+ ` ` `
1069+
1070+ ` ` ` cjs
1071+ const { pathToFileURL } = require (' url' );
1072+ new URL (__filename ); // Incorrect: throws (POSIX)
1073+ new URL (__filename ); // Incorrect: C:\... (Windows)
1074+ pathToFileURL (__filename ); // Correct: file:///... (POSIX)
1075+ pathToFileURL (__filename ); // Correct: file:///C:/... (Windows)
1076+
1077+ new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1078+ pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1079+
1080+ new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1081+ pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
10371082` ` `
10381083
10391084## Legacy URL API
@@ -1190,6 +1235,7 @@ The `url.format()` method returns a formatted URL string derived from
11901235` urlObject` .
11911236
11921237` ` ` js
1238+ const url = require (' url' );
11931239url .format ({
11941240 protocol: ' https' ,
11951241 hostname: ' example.com' ,
0 commit comments