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 <[email protected]>
Reviewed-By: James Snell <[email protected]>
Reviewed-By: Brian White <[email protected]>

Conflicts:
	lib/path.js
  • Loading branch information
Trott authored and Fishrock123 committed Mar 22, 2016
1 parent c5d8369 commit 6e5835b
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 @@ -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);
},


Expand Down

0 comments on commit 6e5835b

Please sign in to comment.