@@ -1012,18 +1012,37 @@ added: v10.12.0
1012
1012
This function ensures the correct decodings of percent-encoded characters as
1013
1013
well as ensuring a cross-platform valid absolute path string.
1014
1014
1015
- ``` js
1016
- new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
1017
- fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
1015
+ ``` mjs
1016
+ import { fileURLToPath } from ' url' ;
1017
+
1018
+ const __filename = fileURLToPath (import .meta.url);
1019
+
1020
+ new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
1021
+ fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
1022
+
1023
+ new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
1024
+ fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
1025
+
1026
+ new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1027
+ fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
1028
+
1029
+ new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
1030
+ fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
1031
+ ` ` `
1032
+
1033
+ ` ` ` cjs
1034
+ const { fileURLToPath } = require (' url' );
1035
+ new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
1036
+ fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
1018
1037
1019
- new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
1020
- fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
1038
+ new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
1039
+ fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
1021
1040
1022
- new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1023
- fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
1041
+ new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1042
+ fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
1024
1043
1025
- new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
1026
- fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
1044
+ new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
1045
+ fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
1027
1046
` ` `
1028
1047
1029
1048
### ` url .format (URL [, options])`
@@ -1052,7 +1071,22 @@ string serializations of the URL. These are not, however, customizable in
1052
1071
any way. The ` url .format (URL [, options])` method allows for basic customization
1053
1072
of the output.
1054
1073
1055
- ``` js
1074
+ ` ` ` mjs
1075
+ import url from ' url' ;
1076
+ const myURL = new URL (' https://a:b@測試?abc#foo' );
1077
+
1078
+ console .log (myURL .href );
1079
+ // Prints https://a:b@xn--g6w251d/?abc#foo
1080
+
1081
+ console .log (myURL .toString ());
1082
+ // Prints https://a:b@xn--g6w251d/?abc#foo
1083
+
1084
+ console .log (url .format (myURL, { fragment: false , unicode: true , auth: false }));
1085
+ // Prints 'https://測試/?abc'
1086
+ ` ` `
1087
+
1088
+ ` ` ` cjs
1089
+ const url = require (' url' );
1056
1090
const myURL = new URL (' https://a:b@測試?abc#foo' );
1057
1091
1058
1092
console .log (myURL .href );
@@ -1076,17 +1110,28 @@ added: v10.12.0
1076
1110
This function ensures that ` path` is resolved absolutely, and that the URL
1077
1111
control characters are correctly encoded when converting into a File URL.
1078
1112
1079
- ``` js
1080
- new URL (__filename ); // Incorrect: throws (POSIX)
1081
- new URL (__filename ); // Incorrect: C:\... (Windows)
1082
- pathToFileURL (__filename ); // Correct: file:///... (POSIX)
1083
- pathToFileURL (__filename ); // Correct: file:///C:/... (Windows)
1113
+ ` ` ` mjs
1114
+ import { pathToFileURL } from ' url' ;
1115
+
1116
+ new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1117
+ pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1118
+
1119
+ new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1120
+ pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1121
+ ` ` `
1122
+
1123
+ ` ` ` cjs
1124
+ const { pathToFileURL } = require (' url' );
1125
+ new URL (__filename ); // Incorrect: throws (POSIX)
1126
+ new URL (__filename ); // Incorrect: C:\... (Windows)
1127
+ pathToFileURL (__filename ); // Correct: file:///... (POSIX)
1128
+ pathToFileURL (__filename ); // Correct: file:///C:/... (Windows)
1084
1129
1085
- new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1086
- pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1130
+ new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1131
+ pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1087
1132
1088
- new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1089
- pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1133
+ new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1134
+ pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1090
1135
` ` `
1091
1136
1092
1137
### ` url .urlToHttpOptions (url)`
@@ -1306,6 +1351,7 @@ The `url.format()` method returns a formatted URL string derived from
1306
1351
` urlObject` .
1307
1352
1308
1353
` ` ` js
1354
+ const url = require (' url' );
1309
1355
url .format ({
1310
1356
protocol: ' https' ,
1311
1357
hostname: ' example.com' ,
0 commit comments