路径解析 path-parse

302 阅读1分钟

path-parse

返回一个对象,其属性表示的重要元素path,尾部目录分隔符将被忽略,请参见path.sep

on POSIX

var  pathParse  = require' path-parse ') ; 
 
pathParse (' /home/user/dir/file.txt ') ;
// => {
//        root:“ /”,
//        dir:“ / home / user / dir”,
//        base:“ file.txt”,
//        ext:“ .txt”,
//        name:“文件”
//    }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

On Windows:

path.parse('C:\\path\\dir\\file.txt');
// Returns:
// { root: 'C:\\',
//   dir: 'C:\\path\\dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' }
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
" C:\      path\dir   \ file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

path.sep

提供特定于平台的路径段分隔符:

  • \ on Windows
  • / on POSIX

on POSIX

'foo/bar/baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']

On Windows:

'foo\\bar\\baz'.split(path.sep);
// Returns: ['foo', 'bar', 'baz']

来源链接:www.npmjs.com/package/pat…