GeezerVison is showing github.com — path: the dot will be added(path.format) if it is not specified in `ext` · nodejs/node@22c39b1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
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 number | Diff line number | Diff line change |
|---|
@@ -206,6 +206,10 @@ A [`TypeError`][] is thrown if `path` is not a string. |
206 | 206 |
|
207 | 207 | <!-- YAML |
208 | 208 | 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`. |
209 | 213 | --> |
210 | 214 |
|
211 | 215 | * `pathObject` {Object} Any JavaScript object having the following properties: |
@@ -255,6 +259,14 @@ path.format({ |
255 | 259 | ext: '.txt' |
256 | 260 | }); |
257 | 261 | // 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' |
258 | 270 | ``` |
259 | 271 |
|
260 | 272 | On Windows: |
|
Collapse file
Copy file name to clipboardExpand all lines: lib/path.js +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -127,6 +127,10 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { |
127 | 127 | return res; |
128 | 128 | } |
129 | 129 |
|
| 130 | +function formatExt(ext) { |
| 131 | + return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : ''; |
| 132 | +} |
| 133 | + |
130 | 134 | /** |
131 | 135 | * @param {string} sep |
132 | 136 | * @param {{ |
@@ -142,7 +146,7 @@ function _format(sep, pathObject) { |
142 | 146 | validateObject(pathObject, 'pathObject'); |
143 | 147 | const dir = pathObject.dir || pathObject.root; |
144 | 148 | const base = pathObject.base || |
145 | | - `${pathObject.name || ''}${pathObject.ext || ''}`; |
| 149 | + `${pathObject.name || ''}${formatExt(pathObject.ext)}`; |
146 | 150 | if (!dir) { |
147 | 151 | return base; |
148 | 152 | } |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-path-parse-format.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -224,3 +224,7 @@ function checkFormat(path, testCases) { |
224 | 224 | }); |
225 | 225 | }); |
226 | 226 | } |
| 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'); |