(path: string)
| 30 | * @returns The `ParsedPath` object. |
| 31 | */ |
| 32 | export function parse(path: string): ParsedPath { |
| 33 | assertPath(path); |
| 34 | |
| 35 | const ret: ParsedPath = { root: "", dir: "", base: "", ext: "", name: "" }; |
| 36 | |
| 37 | const len = path.length; |
| 38 | if (len === 0) return ret; |
| 39 | |
| 40 | let rootEnd = 0; |
| 41 | let code = path.charCodeAt(0); |
| 42 | |
| 43 | // Try to match a root |
| 44 | if (len > 1) { |
| 45 | if (isPathSeparator(code)) { |
| 46 | // Possible UNC root |
| 47 | |
| 48 | rootEnd = 1; |
| 49 | if (isPathSeparator(path.charCodeAt(1))) { |
| 50 | // Matched double path separator at beginning |
| 51 | let j = 2; |
| 52 | let last = j; |
| 53 | // Match 1 or more non-path separators |
| 54 | for (; j < len; ++j) { |
| 55 | if (isPathSeparator(path.charCodeAt(j))) break; |
| 56 | } |
| 57 | if (j < len && j !== last) { |
| 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 | |
| 74 | rootEnd = j; |
| 75 | } else if (j !== last) { |
| 76 | // We matched a UNC root with leftovers |
| 77 | |
| 78 | rootEnd = j + 1; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } else if (isWindowsDeviceRoot(code)) { |
| 84 | // Possible device root |
| 85 | |
| 86 | if (path.charCodeAt(1) === CHAR_COLON) { |
| 87 | rootEnd = 2; |
| 88 | if (len > 2) { |
| 89 | if (isPathSeparator(path.charCodeAt(2))) { |
nothing calls this directly
no test coverage detected