Skip to content

Commit 719c80a

Browse files
committed
path: add dot for path format if absent
1 parent 937520a commit 719c80a

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/path.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
127127
return res;
128128
}
129129

130+
function formatExt(ext) {
131+
return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
132+
}
133+
130134
/**
131135
* @param {string} sep
132136
* @param {{
@@ -142,7 +146,7 @@ function _format(sep, pathObject) {
142146
validateObject(pathObject, 'pathObject');
143147
const dir = pathObject.dir || pathObject.root;
144148
const base = pathObject.base ||
145-
`${pathObject.name || ''}${pathObject.ext || ''}`;
149+
`${pathObject.name || ''}${formatExt(pathObject.ext)}`;
146150
if (!dir) {
147151
return base;
148152
}

test/parallel/test-path-parse-format.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,7 @@ function checkFormat(path, testCases) {
224224
});
225225
});
226226
}
227+
228+
// See https://github.com/nodejs/node/issues/44343
229+
assert.strictEqual(path.format({ name: 'x', ext: 'png' }), 'x.png');
230+
assert.strictEqual(path.format({ name: 'x', ext: '.png' }), 'x.png');

0 commit comments

Comments
 (0)