From 9de9a08587e0350b8f8cb07ac6f3635dd5365e8e 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 --- lib/path.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/lib/path.js b/lib/path.js index 64e34ecde66858..b42889ebfc7637 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); }, @@ -1525,20 +1527,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); },