(path: string, isWindows: boolean = false)
| 237 | * @returns 格式化后的路径 |
| 238 | */ |
| 239 | export function formatPath(path: string, isWindows: boolean = false): string { |
| 240 | if (!path || typeof path !== 'string') { |
| 241 | return '' |
| 242 | } |
| 243 | |
| 244 | // 统一替换反斜杠为正斜杠,并合并多个连续的斜杠 |
| 245 | let formattedPath = path.replace(/\\/g, '/').replace(/\/+/g, '/') |
| 246 | |
| 247 | if (isWindows) { |
| 248 | // Windows 路径处理 |
| 249 | if (/^[A-Za-z]/.test(formattedPath)) { |
| 250 | // 以字母开头,需要添加驱动器冒号 |
| 251 | if (formattedPath.substring(1, 2) !== ':') { |
| 252 | formattedPath = |
| 253 | formattedPath.substring(0, 1).toUpperCase() + ':' + formattedPath.substring(1) |
| 254 | } |
| 255 | } else if (formattedPath.startsWith('/')) { |
| 256 | // 以斜杠开头,移除开头的斜杠 |
| 257 | formattedPath = formattedPath.substring(1) |
| 258 | // 递归处理,确保正确处理所有情况 |
| 259 | return formatPath(formattedPath, isWindows) |
| 260 | } |
| 261 | } else { |
| 262 | // Unix/Linux 路径处理:确保以斜杠开头 |
| 263 | if (!formattedPath.startsWith('/')) { |
| 264 | formattedPath = '/' + formattedPath |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return formattedPath |
| 269 | } |
| 270 | |
| 271 | export function mergeObjects<T>(target: T, source: Partial<T>): T { |
| 272 | for (const key in source) { |
no outgoing calls
no test coverage detected