@@ -959,18 +959,37 @@ added: v10.12.0
959
959
This function ensures the correct decodings of percent-encoded characters as
960
960
well as ensuring a cross-platform valid absolute path string.
961
961
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
+ ` ` `
965
979
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)
968
984
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 )
971
987
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)
974
993
` ` `
975
994
976
995
### ` url .format (URL [, options])`
@@ -999,7 +1018,22 @@ string serializations of the URL. These are not, however, customizable in
999
1018
any way. The ` url .format (URL [, options])` method allows for basic customization
1000
1019
of the output.
1001
1020
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' );
1003
1037
const myURL = new URL (' https://a:b@測試?abc#foo' );
1004
1038
1005
1039
console .log (myURL .href );
@@ -1023,17 +1057,28 @@ added: v10.12.0
1023
1057
This function ensures that ` path` is resolved absolutely, and that the URL
1024
1058
control characters are correctly encoded when converting into a File URL.
1025
1059
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' ;
1031
1062
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)
1034
1065
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)
1037
1082
` ` `
1038
1083
1039
1084
## Legacy URL API
@@ -1190,6 +1235,7 @@ The `url.format()` method returns a formatted URL string derived from
1190
1235
` urlObject` .
1191
1236
1192
1237
` ` ` js
1238
+ const url = require (' url' );
1193
1239
url .format ({
1194
1240
protocol: ' https' ,
1195
1241
hostname: ' example.com' ,
0 commit comments