Skip to content

Commit 6048b25

Browse files
committed
path: Clean up path.parse/format and add tests
+ Remove unused variable and error triggered if that variable isn't the right type + Remove unreachable code (checking return value of *SplitPath) + Remove unnecessary `|| ''` (because what's on the left of that is guaranteed to be a string)
1 parent a71e518 commit 6048b25

File tree

2 files changed

+36
-47
lines changed

2 files changed

+36
-47
lines changed

lib/path.js

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function win32SplitPath(filename) {
9090
// Separate device+slash from tail
9191
var result = splitDeviceRe.exec(filename),
9292
device = (result[1] || '') + (result[2] || ''),
93-
tail = result[3] || '';
93+
tail = result[3];
9494
// Split the tail into dir, basename and extension
9595
var result2 = splitTailRe.exec(tail),
9696
dir = result2[1],
@@ -364,42 +364,31 @@ win32.extname = function(path) {
364364
win32.format = function(pathObject) {
365365
if (!util.isObject(pathObject)) {
366366
throw new TypeError(
367-
"Parameter 'pathObject' must be an object, not " + typeof pathObject
368-
);
369-
}
370-
371-
var root = pathObject.root || '';
372-
373-
if (!util.isString(root)) {
374-
throw new TypeError(
375-
"'pathObject.root' must be a string or undefined, not " +
376-
typeof pathObject.root
367+
"Parameter 'pathObject' must be an object, not " + typeof pathObject
377368
);
378369
}
379370

380371
var dir = pathObject.dir;
381372
var base = pathObject.base || '';
382-
if (dir) {
383-
if (dir[dir.length - 1] === win32.sep) {
384-
return dir + base;
385-
}
386-
return dir + win32.sep + base;
387-
}
388373

389-
return base;
374+
if (!dir) {
375+
return base;
376+
}
377+
if (dir[dir.length - 1] === win32.sep) {
378+
return dir + base;
379+
}
380+
return dir + win32.sep + base;
390381
};
391382

392383

393384
win32.parse = function(pathString) {
394385
if (!util.isString(pathString)) {
395386
throw new TypeError(
396-
"Parameter 'pathString' must be a string, not " + typeof pathString
387+
"Parameter 'pathString' must be a string, not " + typeof pathString
397388
);
398389
}
390+
399391
var allParts = win32SplitPath(pathString);
400-
if (!allParts || allParts.length !== 4) {
401-
throw new TypeError("Invalid path '" + pathString + "'");
402-
}
403392
return {
404393
root: allParts[0],
405394
dir: allParts[0] + allParts[1].slice(0, -1),
@@ -571,16 +560,7 @@ posix.extname = function(path) {
571560
posix.format = function(pathObject) {
572561
if (!util.isObject(pathObject)) {
573562
throw new TypeError(
574-
"Parameter 'pathObject' must be an object, not " + typeof pathObject
575-
);
576-
}
577-
578-
var root = pathObject.root || '';
579-
580-
if (!util.isString(root)) {
581-
throw new TypeError(
582-
"'pathObject.root' must be a string or undefined, not " +
583-
typeof pathObject.root
563+
"Parameter 'pathObject' must be an object, not " + typeof pathObject
584564
);
585565
}
586566

@@ -593,17 +573,11 @@ posix.format = function(pathObject) {
593573
posix.parse = function(pathString) {
594574
if (!util.isString(pathString)) {
595575
throw new TypeError(
596-
"Parameter 'pathString' must be a string, not " + typeof pathString
576+
"Parameter 'pathString' must be a string, not " + typeof pathString
597577
);
598578
}
599-
var allParts = posixSplitPath(pathString);
600-
if (!allParts || allParts.length !== 4) {
601-
throw new TypeError("Invalid path '" + pathString + "'");
602-
}
603-
allParts[1] = allParts[1] || '';
604-
allParts[2] = allParts[2] || '';
605-
allParts[3] = allParts[3] || '';
606579

580+
var allParts = posixSplitPath(pathString);
607581
return {
608582
root: allParts[0],
609583
dir: allParts[0] + allParts[1].slice(0, -1),

test/simple/test-path-parse-format.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ var winPaths = [
3535
'\\\\server two\\shared folder\\file path.zip',
3636
'\\\\teela\\admin$\\system32',
3737
'\\\\?\\UNC\\server\\share'
38+
];
3839

40+
var winSpecialCaseFormatTests = [
41+
[{dir: 'some\\dir'}, 'some\\dir\\'],
42+
[{base: 'index.html'}, 'index.html'],
43+
[{}, '']
3944
];
4045

4146
var unixPaths = [
@@ -50,25 +55,30 @@ var unixPaths = [
5055
'C:\\foo'
5156
];
5257

58+
var unixSpecialCaseFormatTests = [
59+
[{dir: 'some/dir'}, 'some/dir/'],
60+
[{base: 'index.html'}, 'index.html'],
61+
[{}, '']
62+
];
63+
5364
var errors = [
5465
{method: 'parse', input: [null], message: /Parameter 'pathString' must be a string, not/},
5566
{method: 'parse', input: [{}], message: /Parameter 'pathString' must be a string, not object/},
5667
{method: 'parse', input: [true], message: /Parameter 'pathString' must be a string, not boolean/},
5768
{method: 'parse', input: [1], message: /Parameter 'pathString' must be a string, not number/},
5869
{method: 'parse', input: [], message: /Parameter 'pathString' must be a string, not undefined/},
59-
// {method: 'parse', input: [''], message: /Invalid path/}, // omitted because it's hard to trigger!
6070
{method: 'format', input: [null], message: /Parameter 'pathObject' must be an object, not/},
6171
{method: 'format', input: [''], message: /Parameter 'pathObject' must be an object, not string/},
6272
{method: 'format', input: [true], message: /Parameter 'pathObject' must be an object, not boolean/},
6373
{method: 'format', input: [1], message: /Parameter 'pathObject' must be an object, not number/},
64-
{method: 'format', input: [{root: true}], message: /'pathObject.root' must be a string or undefined, not boolean/},
65-
{method: 'format', input: [{root: 12}], message: /'pathObject.root' must be a string or undefined, not number/},
6674
];
6775

68-
check(path.win32, winPaths);
69-
check(path.posix, unixPaths);
76+
checkParseFormat(path.win32, winPaths);
77+
checkParseFormat(path.posix, unixPaths);
7078
checkErrors(path.win32);
7179
checkErrors(path.posix);
80+
checkFormat(path.win32, winSpecialCaseFormatTests);
81+
checkFormat(path.posix, unixSpecialCaseFormatTests);
7282

7383
function checkErrors(path) {
7484
errors.forEach(function(errorCase) {
@@ -87,8 +97,7 @@ function checkErrors(path) {
8797
});
8898
}
8999

90-
91-
function check(path, paths) {
100+
function checkParseFormat(path, paths) {
92101
paths.forEach(function(element, index, array) {
93102
var output = path.parse(element);
94103
assert.strictEqual(path.format(output), element);
@@ -97,3 +106,9 @@ function check(path, paths) {
97106
assert.strictEqual(output.ext, path.extname(element));
98107
});
99108
}
109+
110+
function checkFormat(path, testCases) {
111+
testCases.forEach(function(testCase) {
112+
assert.strictEqual(path.format(testCase[0]), testCase[1]);
113+
});
114+
}

0 commit comments

Comments
 (0)