From 6e5835b8cdfa2f7503ae38c47cd4c0600b9f105b Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Fri, 11 Mar 2016 20:28:39 -0800 Subject: [PATCH] path: refactor path.format() repeated code PR-URL: https://github.com/nodejs/node/pull/5673 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James Snell Reviewed-By: Brian White Conflicts: lib/path.js --- lib/path.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/path.js b/lib/path.js index 7c80f867b1578c..7571918b906fdd 100644 --- a/lib/path.js +++ b/lib/path.js @@ -140,6 +140,18 @@ function normalizeStringPosix(path, allowAboveRoot) { return res; } +function _format(sep, pathObject) { + const dir = pathObject.dir || pathObject.root; + const base = pathObject.base || + ((pathObject.name || '') + (pathObject.ext || '')); + if (!dir) { + return base; + } + if (dir === pathObject.root) { + return dir + base; + } + return dir + sep + base; +} const win32 = { // path.resolve([from ...], to) @@ -970,20 +982,10 @@ const win32 = { format: function format(pathObject) { if (pathObject === null || typeof pathObject !== 'object') { throw new TypeError( - 'Parameter \'pathObject\' must be an object, not ' + typeof pathObject + `Parameter \'pathObject\' must be an object, not ${typeof pathObject}` ); } - - var dir = pathObject.dir || pathObject.root; - var base = pathObject.base || - ((pathObject.name || '') + (pathObject.ext || '')); - if (!dir) { - return base; - } - if (dir === pathObject.root) { - return dir + base; - } - return dir + win32.sep + base; + return _format('\\', pathObject); }, @@ -1523,20 +1525,10 @@ const posix = { format: function format(pathObject) { if (pathObject === null || typeof pathObject !== 'object') { throw new TypeError( - 'Parameter \'pathObject\' must be an object, not ' + typeof pathObject + `Parameter \'pathObject\' must be an object, not ${typeof pathObject}` ); } - - var dir = pathObject.dir || pathObject.root; - var base = pathObject.base || - ((pathObject.name || '') + (pathObject.ext || '')); - if (!dir) { - return base; - } - if (dir === pathObject.root) { - return dir + base; - } - return dir + posix.sep + base; + return _format('/', pathObject); },