当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js path.format(pathObject)用法及代码示例

path.format(pathObject)

添加于:v0.11.15

参数

path.format() 方法从对象返回路径字符串。这与 path.parse() 相反。

pathObject 提供属性时,请记住在某些组合中一个属性优先于另一个属性:

  • 如果提供了pathObject.dir,则忽略pathObject.root
  • 如果存在pathObject.base,则忽略pathObject.extpathObject.name

例如,在 POSIX 上:

// If `dir`, `root` and `base` are provided,
// `${dir}${path.sep}${base}`
// will be returned. `root` is ignored.
path.format({
  root: '/ignored',
  dir: '/home/user/dir',
  base: 'file.txt'
});
// Returns: '/home/user/dir/file.txt'

// `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the
// platform separator will not be included. `ext` will be ignored.
path.format({
  root: '/',
  base: 'file.txt',
  ext: 'ignored'
});
// Returns: '/file.txt'

// `name` + `ext` will be used if `base` is not specified.
path.format({
  root: '/',
  name: 'file',
  ext: '.txt'
});
// Returns: '/file.txt'

在 Windows 上:

path.format({
  dir: 'C:\\path\\dir',
  base: 'file.txt'
});
// Returns: 'C:\\path\\dir\\file.txt'

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 path.format(pathObject)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。