Skip to content

Commit

Permalink
path: refactor path.format() repeated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Trott committed Mar 13, 2016
1 parent 4c4d9ae commit bc36ea2
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 bc36ea2

Please sign in to comment.