(posixPath: string)
| 39 | export const posixResolve: (...paths: string[]) => string = pathe.resolve |
| 40 | |
| 41 | export function posixPathToFileHref(posixPath: string): string { |
| 42 | let resolved = posixResolve(posixPath) |
| 43 | // path.resolve strips trailing slashes so we must add them back |
| 44 | const filePathLast = posixPath.charCodeAt(posixPath.length - 1) |
| 45 | if ( |
| 46 | (filePathLast === CHAR_FORWARD_SLASH || |
| 47 | (isWindows && filePathLast === CHAR_BACKWARD_SLASH)) && |
| 48 | resolved[resolved.length - 1] !== '/' |
| 49 | ) |
| 50 | resolved += '/' |
| 51 | |
| 52 | // Call encodePathChars first to avoid encoding % again for ? and #. |
| 53 | resolved = encodePathChars(resolved) |
| 54 | |
| 55 | // Question and hash character should be included in pathname. |
| 56 | // Therefore, encoding is required to eliminate parsing them in different states. |
| 57 | // This is done as an optimization to not creating a URL instance and |
| 58 | // later triggering pathname setter, which impacts performance |
| 59 | if (resolved.includes('?')) resolved = resolved.replace(questionRegex, '%3F') |
| 60 | if (resolved.includes('#')) resolved = resolved.replace(hashRegex, '%23') |
| 61 | return new URL(`file://${resolved}`).href |
| 62 | } |
| 63 | |
| 64 | export function toWindowsPath(path: string): string { |
| 65 | return path.replace(/\//g, '\\') |
no test coverage detected