Skip to content

Commit 6e5835b

Browse files
TrottFishrock123
authored andcommitted
path: refactor path.format() repeated code
PR-URL: #5673 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Conflicts: lib/path.js
1 parent c5d8369 commit 6e5835b

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

lib/path.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ function normalizeStringPosix(path, allowAboveRoot) {
140140
return res;
141141
}
142142

143+
function _format(sep, pathObject) {
144+
const dir = pathObject.dir || pathObject.root;
145+
const base = pathObject.base ||
146+
((pathObject.name || '') + (pathObject.ext || ''));
147+
if (!dir) {
148+
return base;
149+
}
150+
if (dir === pathObject.root) {
151+
return dir + base;
152+
}
153+
return dir + sep + base;
154+
}
143155

144156
const win32 = {
145157
// path.resolve([from ...], to)
@@ -970,20 +982,10 @@ const win32 = {
970982
format: function format(pathObject) {
971983
if (pathObject === null || typeof pathObject !== 'object') {
972984
throw new TypeError(
973-
'Parameter \'pathObject\' must be an object, not ' + typeof pathObject
985+
`Parameter \'pathObject\' must be an object, not ${typeof pathObject}`
974986
);
975987
}
976-
977-
var dir = pathObject.dir || pathObject.root;
978-
var base = pathObject.base ||
979-
((pathObject.name || '') + (pathObject.ext || ''));
980-
if (!dir) {
981-
return base;
982-
}
983-
if (dir === pathObject.root) {
984-
return dir + base;
985-
}
986-
return dir + win32.sep + base;
988+
return _format('\\', pathObject);
987989
},
988990

989991

@@ -1523,20 +1525,10 @@ const posix = {
15231525
format: function format(pathObject) {
15241526
if (pathObject === null || typeof pathObject !== 'object') {
15251527
throw new TypeError(
1526-
'Parameter \'pathObject\' must be an object, not ' + typeof pathObject
1528+
`Parameter \'pathObject\' must be an object, not ${typeof pathObject}`
15271529
);
15281530
}
1529-
1530-
var dir = pathObject.dir || pathObject.root;
1531-
var base = pathObject.base ||
1532-
((pathObject.name || '') + (pathObject.ext || ''));
1533-
if (!dir) {
1534-
return base;
1535-
}
1536-
if (dir === pathObject.root) {
1537-
return dir + base;
1538-
}
1539-
return dir + posix.sep + base;
1531+
return _format('/', pathObject);
15401532
},
15411533

15421534

0 commit comments

Comments
 (0)