Skip to content

Commit 9de9a08

Browse files
committed
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>
1 parent 85ab4a5 commit 9de9a08

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

@@ -1525,20 +1527,10 @@ const posix = {
15251527
format: function format(pathObject) {
15261528
if (pathObject === null || typeof pathObject !== 'object') {
15271529
throw new TypeError(
1528-
'Parameter "pathObject" must be an object, not ' + typeof pathObject
1530+
`Parameter "pathObject" must be an object, not ${typeof pathObject}`
15291531
);
15301532
}
1531-
1532-
var dir = pathObject.dir || pathObject.root;
1533-
var base = pathObject.base ||
1534-
((pathObject.name || '') + (pathObject.ext || ''));
1535-
if (!dir) {
1536-
return base;
1537-
}
1538-
if (dir === pathObject.root) {
1539-
return dir + base;
1540-
}
1541-
return dir + posix.sep + base;
1533+
return _format('/', pathObject);
15421534
},
15431535

15441536

0 commit comments

Comments
 (0)