Skip to content

Commit 22c39b1

Browse files
authored
path: the dot will be added(path.format) if it is not specified in ext
PR-URL: #44349 Fixes: #44343 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 76229fc commit 22c39b1

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/api/path.md

+12
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ A [`TypeError`][] is thrown if `path` is not a string.
206206

207207
<!-- YAML
208208
added: v0.11.15
209+
changes:
210+
- version: REPLACEME
211+
pr-url: https://github.com/nodejs/node/pull/44349
212+
description: The dot will be added if it is not specified in `ext`.
209213
-->
210214

211215
* `pathObject` {Object} Any JavaScript object having the following properties:
@@ -255,6 +259,14 @@ path.format({
255259
ext: '.txt'
256260
});
257261
// Returns: '/file.txt'
262+
263+
// The dot will be added if it is not specified in `ext`.
264+
path.format({
265+
root: '/',
266+
name: 'file',
267+
ext: 'txt'
268+
});
269+
// Returns: '/file.txt'
258270
```
259271

260272
On Windows:

lib/path.js

+5-1
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

+4
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)