(
path: string,
{
removeLeading,
}: {
removeLeading: boolean;
} = { removeLeading: false },
)
| 2 | * Replace windows style backslashes with unix forwards slashes |
| 3 | */ |
| 4 | export const normalizePath = ( |
| 5 | path: string, |
| 6 | { |
| 7 | removeLeading, |
| 8 | }: { |
| 9 | removeLeading: boolean; |
| 10 | } = { removeLeading: false }, |
| 11 | ): string => { |
| 12 | if (path === '\\' || path === '/') return '/'; |
| 13 | |
| 14 | if (path.length <= 1) { |
| 15 | return path; |
| 16 | } |
| 17 | |
| 18 | let prefix = ''; |
| 19 | |
| 20 | if (path.length > 4 && path[3] === '\\') { |
| 21 | if (['?', '.'].includes(path[2]!) && path.slice(0, 2) === '\\\\') { |
| 22 | path = path.slice(2); |
| 23 | prefix = '//'; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | const segments = path.split(/[/\\]+/); |
| 28 | |
| 29 | if (segments.at(-1) === '') { |
| 30 | segments.pop(); |
| 31 | } |
| 32 | |
| 33 | const normalizedPath = prefix + segments.join('/'); |
| 34 | |
| 35 | if (removeLeading && path.startsWith('/')) { |
| 36 | return normalizedPath.substring(1); |
| 37 | } |
| 38 | |
| 39 | return normalizedPath; |
| 40 | }; |
no test coverage detected