1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

实现最基础的 Windows <-> Unix 风格互转逻辑;添加基础测试用例并通过

This commit is contained in:
2024-12-14 22:00:12 +08:00
parent 6248f3d0cd
commit 6fce88f88a
4 changed files with 163 additions and 6 deletions

View File

@@ -1,6 +1,60 @@
import { SupportPathFormat } from "./types/SupportPathFormatType";
/** / */
const LEFT_SLASH = '/';
/** \ */
const RIGHT_SLASH = '\\';
/** \\ */
const DOUBLE_RIGHT_SLASH = '\\\\';
export function pathConversion(SupportPathFormat: SupportPathFormat, input: string) {
// TODO
export function pathConversion(targetPathType: SupportPathFormat, input: string): string {
let resultPath;
let isSeperator = false;
switch (targetPathType) {
case SupportPathFormat.Windows:
// 将其中的 / 替换为 \
resultPath = Array.from(input).map((char: string) => {
if (char !== LEFT_SLASH) {
// 当前字符不是 /
isSeperator = false;
return char;
} else {
// 当前字符是 /
if (!isSeperator) {
// 上一字符不是 /
isSeperator = true;
return RIGHT_SLASH; // 替换成 \
}
// 上一字符是 /
return '';
}
}).join('');
break;
case SupportPathFormat.Unix:
// 将其中的 \\ 和 \ 替换为 /
resultPath = Array.from(input).map((char: string) => {
if (char !== RIGHT_SLASH) {
// 当前字符不是 \
isSeperator = false;
return char;
} else {
// 当前字符是 \
if (!isSeperator) {
// 上一字符不是 \
isSeperator = true;
return LEFT_SLASH; // 替换成 /
}
// 上一字符是 \
return '';
}
}).join('');
break;
// case SupportPathFormat.WindowsGitBash:
// break;
default:
return input;
}
return resultPath;
}