@@ -1012,7 +1012,22 @@ 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
1015
+ ``` mjs
1016
+ import url from ' url' ;
1017
+ new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
1018
+ url .fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
1019
+
1020
+ new URL (' file://nas/foo.txt' ).pathname ; // Incorrect: /foo.txt
1021
+ url .fileURLToPath (' file://nas/foo.txt' ); // Correct: \\nas\foo.txt (Windows)
1022
+
1023
+ new URL (' file:///你好.txt' ).pathname ; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1024
+ url .fileURLToPath (' file:///你好.txt' ); // Correct: /你好.txt (POSIX)
1025
+
1026
+ new URL (' file:///hello world' ).pathname ; // Incorrect: /hello%20world
1027
+ url .fileURLToPath (' file:///hello world' ); // Correct: /hello world (POSIX)
1028
+ ```
1029
+
1030
+ ``` cjs
1016
1031
const url = require (' url' );
1017
1032
new URL (' file:///C:/path/' ).pathname ; // Incorrect: /C:/path/
1018
1033
url .fileURLToPath (' file:///C:/path/' ); // Correct: C:\path\ (Windows)
@@ -1053,7 +1068,21 @@ string serializations of the URL. These are not, however, customizable in
1053
1068
any way. The ` url.format(URL[, options]) ` method allows for basic customization
1054
1069
of the output.
1055
1070
1056
- ``` js
1071
+ ``` mjs
1072
+ import url from ' url' ;
1073
+ const myURL = new URL (' https://a:b@測試?abc#foo' );
1074
+
1075
+ console .log (myURL .href );
1076
+ // Prints https://a:b@xn--g6w251d/?abc#foo
1077
+
1078
+ console .log (myURL .toString ());
1079
+ // Prints https://a:b@xn--g6w251d/?abc#foo
1080
+
1081
+ console .log (url .format (myURL, { fragment: false , unicode: true , auth: false }));
1082
+ // Prints 'https://測試/?abc'
1083
+ ```
1084
+
1085
+ ``` cjs
1057
1086
const url = require (' url' );
1058
1087
const myURL = new URL (' https://a:b@測試?abc#foo' );
1059
1088
@@ -1078,7 +1107,21 @@ added: v10.12.0
1078
1107
This function ensures that ` path ` is resolved absolutely, and that the URL
1079
1108
control characters are correctly encoded when converting into a File URL.
1080
1109
1081
- ``` js
1110
+ ``` mjs
1111
+ import url from ' url' ;
1112
+ new URL (__filename ); // Incorrect: throws (POSIX)
1113
+ new URL (__filename ); // Incorrect: C:\... (Windows)
1114
+ url .pathToFileURL (__filename ); // Correct: file:///... (POSIX)
1115
+ url .pathToFileURL (__filename ); // Correct: file:///C:/... (Windows)
1116
+
1117
+ new URL (' /foo#1' , ' file:' ); // Incorrect: file:///foo#1
1118
+ url .pathToFileURL (' /foo#1' ); // Correct: file:///foo%231 (POSIX)
1119
+
1120
+ new URL (' /some/path%.c' , ' file:' ); // Incorrect: file:///some/path%.c
1121
+ url .pathToFileURL (' /some/path%.c' ); // Correct: file:///some/path%25.c (POSIX)
1122
+ ```
1123
+
1124
+ ``` cjs
1082
1125
const url = require (' url' );
1083
1126
new URL (__filename ); // Incorrect: throws (POSIX)
1084
1127
new URL (__filename ); // Incorrect: C:\... (Windows)
@@ -1319,7 +1362,22 @@ changes:
1319
1362
The ` url.format() ` method returns a formatted URL string derived from
1320
1363
` urlObject ` .
1321
1364
1322
- ``` js
1365
+ ``` mjs
1366
+ import url from ' url' ;
1367
+ url .format ({
1368
+ protocol: ' https' ,
1369
+ hostname: ' example.com' ,
1370
+ pathname: ' /some/path' ,
1371
+ query: {
1372
+ page: 1 ,
1373
+ format: ' json'
1374
+ }
1375
+ });
1376
+
1377
+ // => 'https://example.com/some/path?page=1&format=json'
1378
+ ```
1379
+
1380
+ ``` cjs
1323
1381
const url = require (' url' );
1324
1382
url .format ({
1325
1383
protocol: ' https' ,
@@ -1473,7 +1531,14 @@ changes:
1473
1531
The ` url.resolve() ` method resolves a target URL relative to a base URL in a
1474
1532
manner similar to that of a Web browser resolving an anchor tag HREF.
1475
1533
1476
- ``` js
1534
+ ``` mjs
1535
+ import url from ' url' ;
1536
+ url .resolve (' /one/two/three' , ' four' ); // '/one/two/four'
1537
+ url .resolve (' http://example.com/' , ' /one' ); // 'http://example.com/one'
1538
+ url .resolve (' http://example.com/one' , ' /two' ); // 'http://example.com/two'
1539
+ ```
1540
+
1541
+ ``` cjs
1477
1542
const url = require (' url' );
1478
1543
url .resolve (' /one/two/three' , ' four' ); // '/one/two/four'
1479
1544
url .resolve (' http://example.com/' , ' /one' ); // 'http://example.com/one'
0 commit comments