Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: refactor path.format() repeated code #5673

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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