(filePath: string)
| 1 | export function splitFilePath(filePath: string) { |
| 2 | // Extract the base name (the last part of the path) |
| 3 | const baseName = filePath.split("/").pop(); |
| 4 | |
| 5 | if (!baseName) throw new Error("Invalid file path"); |
| 6 | |
| 7 | // Handling cases where there is no extension or the file is hidden |
| 8 | if (baseName === "" || baseName.startsWith(".") || !baseName.includes(".")) { |
| 9 | return { |
| 10 | pathWithoutExtension: filePath, |
| 11 | extension: "", |
| 12 | fileName: baseName, |
| 13 | }; |
| 14 | } |
| 15 | |
| 16 | // Finding the last dot to separate the extension |
| 17 | const lastDotIndex = baseName.lastIndexOf("."); |
| 18 | |
| 19 | // Extracting the path without extension and the extension |
| 20 | const pathWithoutExtension = |
| 21 | filePath.substring(0, filePath.lastIndexOf(".")) || |
| 22 | baseName.substring(0, lastDotIndex); |
| 23 | const extension = baseName.substring(lastDotIndex); |
| 24 | |
| 25 | // Extracting just the file name without the extension |
| 26 | const fileName = baseName.substring(0, lastDotIndex); |
| 27 | |
| 28 | return { pathWithoutExtension, extension, fileName }; |
| 29 | } |
no outgoing calls
no test coverage detected