-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
+ 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)
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ function win32SplitPath(filename) { | |
// Separate device+slash from tail | ||
var result = splitDeviceRe.exec(filename), | ||
device = (result[1] || '') + (result[2] || ''), | ||
tail = result[3] || ''; | ||
tail = result[3]; | ||
// Split the tail into dir, basename and extension | ||
var result2 = splitTailRe.exec(tail), | ||
dir = result2[1], | ||
|
@@ -364,42 +364,31 @@ win32.extname = function(path) { | |
win32.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
nwoltman
Author
Owner
|
||
); | ||
} | ||
|
||
var root = pathObject.root || ''; | ||
|
||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
|
||
var dir = pathObject.dir; | ||
var base = pathObject.base || ''; | ||
if (dir) { | ||
if (dir[dir.length - 1] === win32.sep) { | ||
return dir + base; | ||
} | ||
return dir + win32.sep + base; | ||
} | ||
|
||
return base; | ||
if (!dir) { | ||
return base; | ||
} | ||
if (dir[dir.length - 1] === win32.sep) { | ||
return dir + base; | ||
} | ||
return dir + win32.sep + base; | ||
}; | ||
|
||
|
||
win32.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
|
||
var allParts = win32SplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
This comment has been minimized.
Sorry, something went wrong.
misterdjules
|
||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, -1), | ||
|
@@ -571,16 +560,7 @@ posix.extname = function(path) { | |
posix.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
|
||
var root = pathObject.root || ''; | ||
|
||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
|
||
|
@@ -593,17 +573,11 @@ posix.format = function(pathObject) { | |
posix.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
var allParts = posixSplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
allParts[1] = allParts[1] || ''; | ||
allParts[2] = allParts[2] || ''; | ||
allParts[3] = allParts[3] || ''; | ||
This comment has been minimized.
Sorry, something went wrong.
misterdjules
|
||
|
||
var allParts = posixSplitPath(pathString); | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, -1), | ||
|
The fact that
pathObject.root
is not used shows an inconsistency between this API's documentation and its implementation. I'm not sure what's the best way to fix it but here are the possibilities I can think of:Actually make
path.format
takeoptions.root
into account to generate its result and keep the documentation as it is, and actually improve it with more examples of how theroot
option can be used . This could happen in the v0.12 branch if we consider that it's what was intended from the beginning and that this is what the documentation described. We would need to update the tests too, currently they don't test the value ofroot
on Windows or POSIX.Remove this code as is done in this change and update the documentation because then
path.format
would not be strictly "the opposite ofpath.parse
" anymore. This can also be done in v0.12.Personally, I like 1) better because I feel that it would make sense to write
path.format({ root: '/foo/bar', dir: 'other_dir', base: 'somefile.txt'})
and expect to get/foo/bar/other_dir/somefile.txt
as a result (same for different roots on win32, including different drives).This is potentially going to be a long discussion, and one that has nothing to do with the original goal of this PR. So unless the removal of this code has a significant impact on performance, I would suggest tracking that and having this discussion in another PR.
What do you think?
Also including @roryrjb since he's the original author of this API with 2d17193.