mirror of
https://github.com/nodejs/node.git
synced 2025-08-16 06:08:50 +02:00
path: the dot will be added(path.format) if it is not specified in ext
PR-URL: https://github.com/nodejs/node/pull/44349 Fixes: https://github.com/nodejs/node/issues/44343 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
parent
76229fc216
commit
22c39b1ddd
3 changed files with 21 additions and 1 deletions
|
@ -206,6 +206,10 @@ A [`TypeError`][] is thrown if `path` is not a string.
|
||||||
|
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.11.15
|
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:
|
* `pathObject` {Object} Any JavaScript object having the following properties:
|
||||||
|
@ -255,6 +259,14 @@ path.format({
|
||||||
ext: '.txt'
|
ext: '.txt'
|
||||||
});
|
});
|
||||||
// Returns: '/file.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:
|
On Windows:
|
||||||
|
|
|
@ -127,6 +127,10 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatExt(ext) {
|
||||||
|
return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} sep
|
* @param {string} sep
|
||||||
* @param {{
|
* @param {{
|
||||||
|
@ -142,7 +146,7 @@ function _format(sep, pathObject) {
|
||||||
validateObject(pathObject, 'pathObject');
|
validateObject(pathObject, 'pathObject');
|
||||||
const dir = pathObject.dir || pathObject.root;
|
const dir = pathObject.dir || pathObject.root;
|
||||||
const base = pathObject.base ||
|
const base = pathObject.base ||
|
||||||
`${pathObject.name || ''}${pathObject.ext || ''}`;
|
`${pathObject.name || ''}${formatExt(pathObject.ext)}`;
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue