(path: string)
| 494 | |
| 495 | // Go's `path.Clean` |
| 496 | const clean = (path: string): string => { |
| 497 | if (path === "") { |
| 498 | return "." |
| 499 | } |
| 500 | const rooted = path.startsWith("/") |
| 501 | const out: Array<string> = [] |
| 502 | for (const segment of path.split("/")) { |
| 503 | if (segment === "" || segment === ".") { |
| 504 | continue |
| 505 | } |
| 506 | if (segment === "..") { |
| 507 | if (out.length > 0 && out[out.length - 1] !== "..") { |
| 508 | out.pop() |
| 509 | } else if (!rooted) { |
| 510 | out.push("..") |
| 511 | } |
| 512 | continue |
| 513 | } |
| 514 | out.push(segment) |
| 515 | } |
| 516 | const joined = out.join("/") |
| 517 | return rooted ? `/${joined}` : joined === "" ? "." : joined |
| 518 | } |
| 519 | |
| 520 | // Port of Go's `filepath.Match(pattern, ".")`, which `PatternMatcher` uses to |
| 521 | // reject bad syntax. Only chunks reached before a mismatch are checked. |
no test coverage detected