path: make format() consistent and more functional by nwoltman · Pull Request #2408 · nodejs/node (original) (raw)
This PR makes the win32
and posix
versions of path.format()
consistent in when they add a directory separator between the dir and base parts of the path (always add it unless the dir
part is the same as the root
part). This fixes the following inconsistencies:
path. | Before | After |
---|---|---|
win32.format({dir: 'folder\\', base: 'file.txt'}) | 'folder\\file.txt' | 'folder\\\\file.txt' |
posix.format({dir: 'folder/', base: 'file.txt'}) | 'folder//file.txt' | (no change) |
win32.format({root: 'C:\\', dir: 'C:\\'}) | 'C:\\' | (no change) |
win32.format({root: '\\\\unc\\path\\', dir: '\\\\unc\\path\\'}) | '\\\\unc\\path\\' | (no change) |
posix.format({root: '/', dir: '/'}) | '//' | '/' |
This fixes bugs in path.format()
and path.parse()
being mirrors of each other (now they truly are mirrors for both win32
and posix
).
Also, path.format()
is now more functional in that it uses the name
and ext
parts of the path if the base
part is left out, and it uses the root
part if the dir
part is left out. I added an example to the docs to show the new functionality.
I also removed the check for pathObject.root
to be a string, partially because before this commit, pathObject.root
wasn't being used for anything in the path.format()
function, and also because if that part gets checked then all of the parts should get checked.
Alternatively, the code could check that all of the path parts in the pathObject
are strings, in which case it could just go back to using only the dir
and base
parts (essentially expecting the input to only come from path.parse()
).
The discussion for this originated here.