Skip to content

Commit

Permalink
path: add dot for path format if absent
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Aug 22, 2022
1 parent 937520a commit 719c80a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
return res;
}

function formatExt(ext) {
return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
}

/**
* @param {string} sep
* @param {{
Expand All @@ -142,7 +146,7 @@ function _format(sep, pathObject) {
validateObject(pathObject, 'pathObject');
const dir = pathObject.dir || pathObject.root;
const base = pathObject.base ||
`${pathObject.name || ''}${pathObject.ext || ''}`;
`${pathObject.name || ''}${formatExt(pathObject.ext)}`;
if (!dir) {
return base;
}
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,7 @@ function checkFormat(path, testCases) {
});
});
}

// See https://github.com/nodejs/node/issues/44343
assert.strictEqual(path.format({ name: 'x', ext: 'png' }), 'x.png');
assert.strictEqual(path.format({ name: 'x', ext: '.png' }), 'x.png');

0 comments on commit 719c80a

Please sign in to comment.