From d1000b413793bb021e72a4a04c939364484433a3 Mon Sep 17 00:00:00 2001 From: Nathan Woltman Date: Sun, 16 Aug 2015 23:26:46 -0400 Subject: path: make format() consistent and more functional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: James M Snell PR-URL: https://github.com/nodejs/node/pull/2408 --- lib/path.js | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'lib/path.js') 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; }; -- cgit v1.2.3