path.format(pathObject)


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

¥The path.format() method returns a path string from an object. This is the opposite of path.parse().

当向 pathObject 提供属性时,存在一个属性优先于另一个属性的组合:

¥When providing properties to the pathObject remember that there are combinations where one property has priority over another:

  • 如果提供 pathObject.dir,则忽略 pathObject.root

    ¥pathObject.root is ignored if pathObject.dir is provided

  • 如果 pathObject.base 存在,则忽略 pathObject.extpathObject.name

    ¥pathObject.ext and pathObject.name are ignored if pathObject.base exists

例如,在 POSIX 上:

¥For example, on 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 上:

¥On Windows:

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