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: #44349Fixes: #44343 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 76229fc commit 22c39b1

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎doc/api/path.md‎

Lines changed: 12 additions & 0 deletions
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‎

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
returnres;
128128
}
129129

130+
functionformatExt(ext){
131+
returnext ? `${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
constdir=pathObject.dir||pathObject.root;
144148
constbase=pathObject.base||
145-
`${pathObject.name||''}${pathObject.ext||''}`;
149+
`${pathObject.name||''}${formatExt(pathObject.ext)}`;
146150
if(!dir){
147151
returnbase;
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)