Skip to content

Commit

Permalink
path: the dot will be added(path.format) if it is not specified in ext
Browse files Browse the repository at this point in the history
PR-URL: #44349
Fixes: #44343
Reviewed-By: Moshe Atlow <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
  • Loading branch information
theanarkh authored Oct 10, 2022
1 parent 76229fc commit 22c39b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions doc/api/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ A [`TypeError`][] is thrown if `path` is not a string.

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

* `pathObject` {Object} Any JavaScript object having the following properties:
Expand Down Expand Up @@ -255,6 +259,14 @@ path.format({
ext: '.txt'
});
// Returns: '/file.txt'

// The dot will be added if it is not specified in `ext`.
path.format({
root: '/',
name: 'file',
ext: 'txt'
});
// Returns: '/file.txt'
```

On Windows:
Expand Down
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 22c39b1

Please sign in to comment.