Skip to content

Commit b1058ce

Browse files
committed
path: [make requested changes]
1 parent cd5e7a0 commit b1058ce

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

doc/api/path.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ path.extname('.index.md');
209209
// Returns: '.md'
210210
```
211211

212-
A [`TypeError`][] is thrown if `path` is not a string or URL.
212+
A [`TypeError`][] is thrown if `path` is not a string or an instance of \[`URL`]\[].
213213

214214
## `path.format(pathObject)`
215215

@@ -326,7 +326,7 @@ path.isAbsolute('bar/baz'); // false
326326
path.isAbsolute('.'); // false
327327
```
328328

329-
A [`TypeError`][] is thrown if `path` is not a string or URL.
329+
A [`TypeError`][] is thrown if `path` is not a string or an instance of \[`URL`]\[].
330330

331331
## `path.join([...paths])`
332332

@@ -356,7 +356,7 @@ path.join('foo', {}, 'bar');
356356
// Throws 'TypeError: Path must be a string. Received {}'
357357
```
358358

359-
A [`TypeError`][] is thrown if any of the path segments is not a string or URL.
359+
A [`TypeError`][] is thrown if any of the path segments is not a string or an instance of \[`URL`]\[].
360360

361361
## `path.normalize(path)`
362362

@@ -412,7 +412,7 @@ path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
412412
// Returns: 'C:\\temp\\foo\\bar'
413413
```
414414

415-
A [`TypeError`][] is thrown if `path` is not a string or URL.
415+
A [`TypeError`][] is thrown if `path` is not a string or an instance of \[`URL`]\[].
416416

417417
## `path.parse(path)`
418418

@@ -483,7 +483,7 @@ path.parse('C:\\path\\dir\\file.txt');
483483
(All spaces in the "" line should be ignored. They are purely for formatting.)
484484
```
485485

486-
A [`TypeError`][] is thrown if `path` is not a string or URL.
486+
A [`TypeError`][] is thrown if `path` is not a string or an instance of \[`URL`]\[].
487487

488488
## `path.posix`
489489

@@ -541,7 +541,7 @@ path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
541541
// Returns: '..\\..\\impl\\bbb'
542542
```
543543

544-
A [`TypeError`][] is thrown if either `from` or `to` is not a string or URL.
544+
A [`TypeError`][] is thrown if either `from` or `to` is not a string or an instance of \[`URL`]\[].
545545

546546
## `path.resolve([...paths])`
547547

@@ -588,7 +588,7 @@ path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
588588
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'
589589
```
590590

591-
A [`TypeError`][] is thrown if any of the arguments is not a string or URL.
591+
A [`TypeError`][] is thrown if any of the arguments is not a string or an instance of \[`URL`]\[].
592592

593593
## `path.sep`
594594

@@ -665,3 +665,4 @@ The API is accessible via `require('node:path').win32` or `require('node:path/wi
665665
[`path.sep`]: #pathsep
666666
[`path.win32`]: #pathwin32
667667
[namespace-prefixed path]: https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces
668+
[`URL`]: url.md

lib/path.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,14 @@ const {
4747
validateString,
4848
} = require('internal/validators');
4949
const {
50-
toPathIfFileURL,
50+
toPathIfFileURL: URLToPathIfFileURL,
5151
} = require('internal/url');
5252

53+
function toPathIfFileURL(url, options) {
54+
if (typeof url === 'string') return url;
55+
return URLToPathIfFileURL(url, options);
56+
}
57+
5358
const platformIsWin32 = (process.platform === 'win32');
5459

5560
function isPathSeparator(code) {
@@ -65,6 +70,9 @@ function isWindowsDeviceRoot(code) {
6570
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z);
6671
}
6772

73+
const kWindowsURLOptions = { windows: true };
74+
const kNixURLOptions = { windows: false };
75+
6876
// Resolves . and .. elements in a path with directory names
6977
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
7078
let res = '';
@@ -175,7 +183,7 @@ const win32 = {
175183
for (let i = args.length - 1; i >= -1; i--) {
176184
let path;
177185
if (i >= 0) {
178-
path = toPathIfFileURL(args[i], { windows: true });
186+
path = toPathIfFileURL(args[i], kWindowsURLOptions);
179187
validateString(path, `paths[${i}]`);
180188

181189
// Skip empty entries
@@ -314,7 +322,7 @@ const win32 = {
314322
* @returns {string}
315323
*/
316324
normalize(path) {
317-
path = toPathIfFileURL(path, { windows: true });
325+
path = toPathIfFileURL(path, kWindowsURLOptions);
318326
validateString(path, 'path');
319327
const len = path.length;
320328
if (len === 0)
@@ -413,7 +421,7 @@ const win32 = {
413421
* @returns {boolean}
414422
*/
415423
isAbsolute(path) {
416-
path = toPathIfFileURL(path, { windows: true });
424+
path = toPathIfFileURL(path, kWindowsURLOptions);
417425
validateString(path, 'path');
418426
const len = path.length;
419427
if (len === 0)
@@ -439,7 +447,7 @@ const win32 = {
439447
let joined;
440448
let firstPart;
441449
for (let i = 0; i < args.length; ++i) {
442-
const arg = toPathIfFileURL(args[i], { windows: true });
450+
const arg = toPathIfFileURL(args[i], kWindowsURLOptions);
443451
validateString(arg, 'path');
444452
if (arg.length > 0) {
445453
if (joined === undefined)
@@ -508,8 +516,8 @@ const win32 = {
508516
* @returns {string}
509517
*/
510518
relative(from, to) {
511-
from = toPathIfFileURL(from, { windows: true });
512-
to = toPathIfFileURL(to, { windows: true });
519+
from = toPathIfFileURL(from, kWindowsURLOptions);
520+
to = toPathIfFileURL(to, kWindowsURLOptions);
513521
validateString(from, 'from');
514522
validateString(to, 'to');
515523

@@ -632,7 +640,7 @@ const win32 = {
632640
* @returns {string}
633641
*/
634642
toNamespacedPath(path) {
635-
path = toPathIfFileURL(path, { windows: true });
643+
path = toPathIfFileURL(path, kWindowsURLOptions);
636644
// Note: this will *probably* throw somewhere.
637645
if (typeof path !== 'string' || path.length === 0)
638646
return path;
@@ -668,7 +676,7 @@ const win32 = {
668676
* @returns {string}
669677
*/
670678
dirname(path) {
671-
path = toPathIfFileURL(path, { windows: true });
679+
path = toPathIfFileURL(path, kWindowsURLOptions);
672680
validateString(path, 'path');
673681
const len = path.length;
674682
if (len === 0)
@@ -767,7 +775,7 @@ const win32 = {
767775
basename(path, suffix) {
768776
if (suffix !== undefined)
769777
validateString(suffix, 'ext');
770-
path = toPathIfFileURL(path, { windows: true });
778+
path = toPathIfFileURL(path, kWindowsURLOptions);
771779
validateString(path, 'path');
772780
let start = 0;
773781
let end = -1;
@@ -853,7 +861,7 @@ const win32 = {
853861
* @returns {string}
854862
*/
855863
extname(path) {
856-
path = toPathIfFileURL(path, { windows: true });
864+
path = toPathIfFileURL(path, kWindowsURLOptions);
857865
validateString(path, 'path');
858866
let start = 0;
859867
let startDot = -1;
@@ -930,7 +938,7 @@ const win32 = {
930938
* }}
931939
*/
932940
parse(path) {
933-
path = toPathIfFileURL(path, { windows: true });
941+
path = toPathIfFileURL(path, kWindowsURLOptions);
934942
validateString(path, 'path');
935943

936944
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
@@ -1114,7 +1122,7 @@ const posix = {
11141122
let resolvedAbsolute = false;
11151123

11161124
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
1117-
const path = i >= 0 ? toPathIfFileURL(args[i], { windows: false }) : posixCwd();
1125+
const path = i >= 0 ? toPathIfFileURL(args[i], kNixURLOptions) : posixCwd();
11181126
validateString(path, `paths[${i}]`);
11191127

11201128
// Skip empty entries
@@ -1145,7 +1153,7 @@ const posix = {
11451153
* @returns {string}
11461154
*/
11471155
normalize(path) {
1148-
path = toPathIfFileURL(path, { windows: false });
1156+
path = toPathIfFileURL(path, kNixURLOptions);
11491157
validateString(path, 'path');
11501158

11511159
if (path.length === 0)
@@ -1175,7 +1183,7 @@ const posix = {
11751183
* @returns {boolean}
11761184
*/
11771185
isAbsolute(path) {
1178-
path = toPathIfFileURL(path, { windows: false });
1186+
path = toPathIfFileURL(path, kNixURLOptions);
11791187
validateString(path, 'path');
11801188
return path.length > 0 &&
11811189
StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
@@ -1190,7 +1198,7 @@ const posix = {
11901198
return '.';
11911199
let joined;
11921200
for (let i = 0; i < args.length; ++i) {
1193-
const arg = toPathIfFileURL(args[i], { windows: false });
1201+
const arg = toPathIfFileURL(args[i], kNixURLOptions);
11941202
validateString(arg, 'path');
11951203
if (arg.length > 0) {
11961204
if (joined === undefined)
@@ -1210,8 +1218,8 @@ const posix = {
12101218
* @returns {string}
12111219
*/
12121220
relative(from, to) {
1213-
from = toPathIfFileURL(from, { windows: false });
1214-
to = toPathIfFileURL(to, { windows: false });
1221+
from = toPathIfFileURL(from, kNixURLOptions);
1222+
to = toPathIfFileURL(to, kNixURLOptions);
12151223
validateString(from, 'from');
12161224
validateString(to, 'to');
12171225

@@ -1289,15 +1297,15 @@ const posix = {
12891297
*/
12901298
toNamespacedPath(path) {
12911299
// Non-op on posix systems
1292-
return toPathIfFileURL(path, { windows: false });
1300+
return toPathIfFileURL(path, kNixURLOptions);
12931301
},
12941302

12951303
/**
12961304
* @param {PathLike} path
12971305
* @returns {string}
12981306
*/
12991307
dirname(path) {
1300-
path = toPathIfFileURL(path, { windows: false });
1308+
path = toPathIfFileURL(path, kNixURLOptions);
13011309
validateString(path, 'path');
13021310
if (path.length === 0)
13031311
return '.';
@@ -1331,7 +1339,7 @@ const posix = {
13311339
basename(path, suffix) {
13321340
if (suffix !== undefined)
13331341
validateString(suffix, 'ext');
1334-
path = toPathIfFileURL(path, { windows: false });
1342+
path = toPathIfFileURL(path, kNixURLOptions);
13351343
validateString(path, 'path');
13361344

13371345
let start = 0;
@@ -1409,7 +1417,7 @@ const posix = {
14091417
* @returns {string}
14101418
*/
14111419
extname(path) {
1412-
path = toPathIfFileURL(path, { windows: false });
1420+
path = toPathIfFileURL(path, kNixURLOptions);
14131421
validateString(path, 'path');
14141422
let startDot = -1;
14151423
let startPart = 0;
@@ -1474,7 +1482,7 @@ const posix = {
14741482
* }}
14751483
*/
14761484
parse(path) {
1477-
path = toPathIfFileURL(path, { windows: false });
1485+
path = toPathIfFileURL(path, kNixURLOptions);
14781486
validateString(path, 'path');
14791487

14801488
const ret = { root: '', dir: '', base: '', ext: '', name: '' };

0 commit comments

Comments
 (0)