UNPKG

@flex-development/pathe

Version:

Universal drop-in replacement for node:path

66 lines (65 loc) 2.13 kB
import { DRIVE_PATH_REGEX, sepWindows } from "#internal/constants"; import normalizeString from "#internal/normalize-string"; import validateString from "#internal/validate-string"; import dot from "#lib/dot"; import isAbsolute from "#lib/is-absolute"; import isSep from "#lib/is-sep"; import sep from "#lib/sep"; import toPosix from "#lib/to-posix"; function normalize(path) { validateString(path, "path"); let normalized = ""; if (path.length <= 1) { normalized = path || dot; } else { let absolute = false; let device = ""; let rootEnd = 0; if (isSep(path[0])) { absolute = true; if (!isSep(path[1])) { rootEnd = 1; } else { let j = 2; let last = j; while (j < path.length && !isSep(path[j])) j++; if (j < path.length && j !== last) { const comp = path.slice(last, j); last = j; while (j < path.length && isSep(path[j])) j++; if (j < path.length && j !== last) { last = j; while (j < path.length && !isSep(path[j])) j++; if (j === path.length || j !== last) { device = sepWindows.repeat(2) + comp; if (comp === dot || comp === "?") { rootEnd = 4; } else if (j === path.length) { device += sepWindows + path.slice(last) + sepWindows; return toPosix(device); } else { device += sepWindows + path.slice(last, j); rootEnd = j; } } } } } } else if (DRIVE_PATH_REGEX.test(path)) { device = path.slice(0, rootEnd = 2); if (absolute = isAbsolute(path)) rootEnd++; } let tail = ""; if (rootEnd < path.length) { tail = normalizeString(path.slice(rootEnd), !absolute); } if (!tail.length && !absolute) tail = dot; if (tail.length && isSep(path[path.length - 1])) tail += sep; normalized = device + (absolute ? sep : "") + tail; } return toPosix(normalized); } var normalize_default = normalize; export { normalize_default as default };