(path: string | URL)
| 25 | * @returns The normalized path |
| 26 | */ |
| 27 | export function normalize(path: string | URL): string { |
| 28 | if (path instanceof URL) { |
| 29 | path = fromFileUrl(path); |
| 30 | } |
| 31 | assertArg(path); |
| 32 | |
| 33 | const len = path.length; |
| 34 | let rootEnd = 0; |
| 35 | let device: string | undefined; |
| 36 | let isAbsolute = false; |
| 37 | const code = path.charCodeAt(0); |
| 38 | |
| 39 | // Try to match a root |
| 40 | if (len > 1) { |
| 41 | if (isPathSeparator(code)) { |
| 42 | // Possible UNC root |
| 43 | |
| 44 | // If we started with a separator, we know we at least have an absolute |
| 45 | // path of some kind (UNC or otherwise) |
| 46 | isAbsolute = true; |
| 47 | |
| 48 | if (isPathSeparator(path.charCodeAt(1))) { |
| 49 | // Matched double path separator at beginning |
| 50 | let j = 2; |
| 51 | let last = j; |
| 52 | // Match 1 or more non-path separators |
| 53 | for (; j < len; ++j) { |
| 54 | if (isPathSeparator(path.charCodeAt(j))) break; |
| 55 | } |
| 56 | if (j < len && j !== last) { |
| 57 | const firstPart = path.slice(last, j); |
| 58 | // Matched! |
| 59 | last = j; |
| 60 | // Match 1 or more path separators |
| 61 | for (; j < len; ++j) { |
| 62 | if (!isPathSeparator(path.charCodeAt(j))) break; |
| 63 | } |
| 64 | if (j < len && j !== last) { |
| 65 | // Matched! |
| 66 | last = j; |
| 67 | // Match 1 or more non-path separators |
| 68 | for (; j < len; ++j) { |
| 69 | if (isPathSeparator(path.charCodeAt(j))) break; |
| 70 | } |
| 71 | if (j === len) { |
| 72 | // We matched a UNC root only |
| 73 | // Return the normalized version of the UNC root since there |
| 74 | // is nothing left to process |
| 75 | |
| 76 | return `\\\\${firstPart}\\${path.slice(last)}\\`; |
| 77 | } else if (j !== last) { |
| 78 | // We matched a UNC root with leftovers |
| 79 | |
| 80 | device = `\\\\${firstPart}\\${path.slice(last, j)}`; |
| 81 | rootEnd = j; |
| 82 | } |
| 83 | } |
| 84 | } |
no test coverage detected