diff options
author | Nathan Woltman <nwoltman@outlook.com> | 2015-08-16 23:26:46 -0400 |
---|---|---|
committer | João Reis <reis@janeasystems.com> | 2015-11-27 11:53:33 +0000 |
commit | d1000b413793bb021e72a4a04c939364484433a3 (patch) | |
tree | 436b4d9a86c5d3e747d70ea58fc3ca3cbf9e42be /lib | |
parent | e25f8683f1735f55a27c00d41691be286f50e13f (diff) | |
download | node-new-d1000b413793bb021e72a4a04c939364484433a3.tar.gz |
path: make format() consistent and more functional
Make 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).
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.
Reviewed-By: João Reis <reis@janeasystems.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/2408
Diffstat (limited to 'lib')
-rw-r--r-- | lib/path.js | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/lib/path.js b/lib/path.js index 694cd38425..0a2c0d6bce 100644 --- a/lib/path.js +++ b/lib/path.js @@ -361,21 +361,13 @@ win32.format = function(pathObject) { ); } - var root = pathObject.root || ''; - - if (typeof root !== 'string') { - throw new TypeError( - '"pathObject.root" must be a string or undefined, not ' + - typeof pathObject.root - ); - } - - var dir = pathObject.dir; - var base = pathObject.base || ''; + var dir = pathObject.dir || pathObject.root; + var base = pathObject.base || + ((pathObject.name || '') + (pathObject.ext || '')); if (!dir) { return base; } - if (dir[dir.length - 1] === win32.sep) { + if (dir === pathObject.root) { return dir + base; } return dir + win32.sep + base; @@ -570,18 +562,16 @@ posix.format = function(pathObject) { ); } - var root = pathObject.root || ''; - - if (typeof root !== 'string') { - throw new TypeError( - '"pathObject.root" must be a string or undefined, not ' + - typeof pathObject.root - ); + var dir = pathObject.dir || pathObject.root; + var base = pathObject.base || + ((pathObject.name || '') + (pathObject.ext || '')); + if (!dir) { + return base; } - - var dir = pathObject.dir ? pathObject.dir + posix.sep : ''; - var base = pathObject.base || ''; - return dir + base; + if (dir === pathObject.root) { + return dir + base; + } + return dir + posix.sep + base; }; |