Skip to content

Commit

Permalink
path: refactor path.format() repeated code
Browse files Browse the repository at this point in the history
PR-URL: #5673
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
  • Loading branch information
Trott committed Mar 16, 2016
1 parent 85ab4a5 commit 9de9a08
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
},


Expand Down Expand Up @@ -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);
},


Expand Down

0 comments on commit 9de9a08

Please sign in to comment.