Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 22c39b1

Browse filesBrowse 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 <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 76229fc commit 22c39b1
Copy full SHA for 22c39b1

File tree

Expand file treeCollapse file tree

3 files changed

+21
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+21
-1
lines changed
Open diff view settings
Collapse file

‎doc/api/path.md‎

Copy file name to clipboardExpand all lines: doc/api/path.md
+12Lines changed: 12 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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:
Collapse file

‎lib/path.js‎

Copy file name to clipboardExpand all lines: lib/path.js
+5-1Lines 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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-path-parse-format.js
+4Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.