1
0
Fork 0
Browse Source

vault backup: 2022-08-03 12:05:45

master
YuJian 2 years ago
parent
commit
643d1b7d7f
  1. 21
      随时随地/Nodejs 文件系统(path)学习笔记.md

21
随时随地/Nodejs 文件系统(path)学习笔记.md

@ -302,4 +302,23 @@ path.posix.resolve(dataDirUnix, ...universalRelativePath), @@ -302,4 +302,23 @@ path.posix.resolve(dataDirUnix, ...universalRelativePath),
// '/home/john/data-dir/static/img/logo.jpg'
```
以下代码用于将路径转换为数组
以下代码用于将路径转换为数组
```javascript
const splitRelativePathIntoSegments = (relPath) => {
if (path.isAbsolute(relPath)) {
throw new Error("Path isn’t relative: " + relPath);
}
relPath = path.normalize(relPath);
const result = [];
while (true) {
const base = path.basename(relPath);
if (base.length === 0) break;
result.unshift(base);
const dir = path.dirname(relPath);
if (dir === ".") break;
relPath = dir;
}
return result;
};
```
Loading…
Cancel
Save