@visulima/path
Version:
Drop-in replacement of the Node.js path module.
455 lines (449 loc) • 18.8 kB
JavaScript
;
var __defProp$2 = Object.defineProperty;
var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true });
let _lazyMatch = /* @__PURE__ */ __name$2(() => {
var __lib__ = (() => {
var y = Object.defineProperty, G = Object.getOwnPropertyDescriptor, Q = Object.getOwnPropertyNames, H = Object.prototype.hasOwnProperty, J = /* @__PURE__ */ __name$2((r, e) => {
for (var t in e) y(r, t, { get: e[t], enumerable: true });
}, "J"), T = /* @__PURE__ */ __name$2((r, e, t, a) => {
if (e && typeof e == "object" || typeof e == "function") for (let n of Q(e)) !H.call(r, n) && n !== t && y(r, n, { get: /* @__PURE__ */ __name$2(() => e[n], "get"), enumerable: !(a = G(e, n)) || a.enumerable });
return r;
}, "T"), W = /* @__PURE__ */ __name$2((r) => T(y({}, "__esModule", { value: true }), r), "W"), b = {};
J(b, { zeptomatch: /* @__PURE__ */ __name$2(() => ne, "zeptomatch") });
var k = /* @__PURE__ */ __name$2((r) => Array.isArray(r), "k"), x = /* @__PURE__ */ __name$2((r) => typeof r == "function", "x"), K = /* @__PURE__ */ __name$2((r) => r.length === 0, "K"), X = (() => {
const { toString: r } = Function.prototype, e = /(?:^\(\s*(?:[^,.()]|\.(?!\.\.))*\s*\)\s*=>|^\s*[a-zA-Z$_][a-zA-Z0-9$_]*\s*=>)/;
return (t) => (t.length === 0 || t.length === 1) && e.test(r.call(t));
})(), Y = /* @__PURE__ */ __name$2((r) => typeof r == "number", "Y"), rr = /* @__PURE__ */ __name$2((r) => typeof r == "object" && r !== null, "rr"), er = /* @__PURE__ */ __name$2((r) => r instanceof RegExp, "er"), tr = /* @__PURE__ */ (() => {
const r = /\\\(|\((?!\?(?::|=|!|<=|<!))/;
return (e) => r.test(e.source);
})(), nr = /* @__PURE__ */ (() => {
const r = /^[a-zA-Z0-9_-]+$/;
return (e) => r.test(e.source) && !e.flags.includes("i");
})(), M = /* @__PURE__ */ __name$2((r) => typeof r == "string", "M"), f = /* @__PURE__ */ __name$2((r) => r === void 0, "f"), ar = /* @__PURE__ */ __name$2((r) => {
const e = /* @__PURE__ */ new Map();
return (t) => {
const a = e.get(t);
if (a !== void 0) return a;
const n = r(t);
return e.set(t, n), n;
};
}, "ar"), or = /* @__PURE__ */ __name$2((r, e, t = {}) => {
const a = { cache: {}, input: r, index: 0, indexBacktrackMax: 0, options: t, output: [] }, n = d(e)(a), s = Math.max(a.index, a.indexBacktrackMax);
if (n && a.index === r.length) return a.output;
throw new Error(`Failed to parse at index ${s}`);
}, "or"), o = /* @__PURE__ */ __name$2((r, e) => k(r) ? ir(r, e) : M(r) ? R(r, e) : ur(r, e), "o"), ir = /* @__PURE__ */ __name$2((r, e) => {
const t = {};
for (const a of r) {
if (a.length !== 1) throw new Error(`Invalid character: "${a}"`);
const n = a.charCodeAt(0);
t[n] = true;
}
return (a) => {
const n = a.input;
let s = a.index, i = s;
for (; i < n.length && n.charCodeAt(i) in t; ) i += 1;
if (i > s) {
if (!f(e) && !a.options.silent) {
const u = n.slice(s, i), c = x(e) ? e(u, n, `${s}`) : e;
f(c) || a.output.push(c);
}
a.index = i;
}
return true;
};
}, "ir"), ur = /* @__PURE__ */ __name$2((r, e) => {
if (nr(r)) return R(r.source, e);
{
const t = r.source, a = r.flags.replace(/y|$/, "y"), n = new RegExp(t, a);
return tr(r) && x(e) && !X(e) ? sr(n, e) : cr(n, e);
}
}, "ur"), sr = /* @__PURE__ */ __name$2((r, e) => (t) => {
const a = t.index, n = t.input;
r.lastIndex = a;
const s = r.exec(n);
if (s) {
const i = r.lastIndex;
if (!t.options.silent) {
const u = e(...s, n, `${a}`);
f(u) || t.output.push(u);
}
return t.index = i, true;
} else return false;
}, "sr"), cr = /* @__PURE__ */ __name$2((r, e) => (t) => {
const a = t.index, n = t.input;
if (r.lastIndex = a, r.test(n)) {
const i = r.lastIndex;
if (!f(e) && !t.options.silent) {
const u = x(e) ? e(n.slice(a, i), n, `${a}`) : e;
f(u) || t.output.push(u);
}
return t.index = i, true;
} else return false;
}, "cr"), R = /* @__PURE__ */ __name$2((r, e) => (t) => {
const a = t.index, n = t.input;
if (n.startsWith(r, a)) {
if (!f(e) && !t.options.silent) {
const i = x(e) ? e(r, n, `${a}`) : e;
f(i) || t.output.push(i);
}
return t.index += r.length, true;
} else return false;
}, "R"), O = /* @__PURE__ */ __name$2((r, e, t, a) => {
const n = d(r), s = e > 1;
return w(C(P((i) => {
let u = 0;
for (; u < t; ) {
const c = i.index;
if (!n(i) || (u += 1, i.index === c)) break;
}
return u >= e;
}, s), a));
}, "O"), lr = /* @__PURE__ */ __name$2((r, e) => O(r, 0, 1, e), "lr"), m = /* @__PURE__ */ __name$2((r, e) => O(r, 0, 1 / 0, e), "m"), _ = /* @__PURE__ */ __name$2((r, e) => {
const t = r.map(d);
return w(C(P((a) => {
for (let n = 0, s = t.length; n < s; n++) if (!t[n](a)) return false;
return true;
}), e));
}, "_"), p = /* @__PURE__ */ __name$2((r, e) => {
const t = r.map(d);
return w(C((a) => {
for (let n = 0, s = t.length; n < s; n++) if (t[n](a)) return true;
return false;
}, e));
}, "p"), P = /* @__PURE__ */ __name$2((r, e = true, t = false) => {
const a = d(r);
return e ? (n) => {
const s = n.index, i = n.output.length, u = a(n);
return !u && !t && (n.indexBacktrackMax = Math.max(n.indexBacktrackMax, n.index)), (!u || t) && (n.index = s, n.output.length !== i && (n.output.length = i)), u;
} : a;
}, "P"), C = /* @__PURE__ */ __name$2((r, e) => {
const t = d(r);
return e ? (a) => {
if (a.options.silent) return t(a);
const n = a.output.length;
if (t(a)) {
const i = a.output.splice(n, 1 / 0), u = e(i);
return f(u) || a.output.push(u), true;
} else return false;
} : t;
}, "C"), w = /* @__PURE__ */ (() => {
let r = 0;
return (e) => {
const t = d(e), a = r += 1;
return (n) => {
var s;
if (n.options.memoization === false) return t(n);
const i = n.index, u = (s = n.cache)[a] || (s[a] = { indexMax: -1, queue: [] }), c = u.queue;
if (i <= u.indexMax) {
const g = u.store || (u.store = /* @__PURE__ */ new Map());
if (c.length) {
for (let h = 0, ie = c.length; h < ie; h += 2) {
const ue = c[h * 2], se = c[h * 2 + 1];
g.set(ue, se);
}
c.length = 0;
}
const l = g.get(i);
if (l === false) return false;
if (Y(l)) return n.index = l, true;
if (l) return n.index = l.index, l.output?.length && n.output.push(...l.output), true;
}
const V = n.output.length, oe = t(n);
if (u.indexMax = Math.max(u.indexMax, i), oe) {
const g = n.index, l = n.output.length;
if (l > V) {
const h = n.output.slice(V, l);
c.push(i, { index: g, output: h });
} else c.push(i, g);
return true;
} else return c.push(i, false), false;
};
};
})(), A = /* @__PURE__ */ __name$2((r) => {
let e;
return (t) => (e || (e = d(r())), e(t));
}, "A"), d = ar((r) => {
if (x(r)) return K(r) ? A(r) : r;
if (M(r) || er(r)) return o(r);
if (k(r)) return _(r);
if (rr(r)) return p(Object.values(r));
throw new Error("Invalid rule");
}), v = /* @__PURE__ */ __name$2((r) => r, "v"), z = /* @__PURE__ */ __name$2((r) => (e) => or(e, r, { memoization: false }).join(""), "z"), pr = /* @__PURE__ */ __name$2((r) => {
const e = {};
return (t) => e[t] ?? (e[t] = r(t));
}, "pr"), B = "abcdefghijklmnopqrstuvwxyz", vr = /* @__PURE__ */ __name$2((r) => {
let e = "";
for (; r > 0; ) {
const t = (r - 1) % 26;
e = B[t] + e, r = Math.floor((r - 1) / 26);
}
return e;
}, "vr"), N = /* @__PURE__ */ __name$2((r) => {
let e = 0;
for (let t = 0, a = r.length; t < a; t++) e = e * 26 + B.indexOf(r[t]) + 1;
return e;
}, "N"), $ = /* @__PURE__ */ __name$2((r, e) => {
if (e < r) return $(e, r);
const t = [];
for (; r <= e; ) t.push(r++);
return t;
}, "$"), fr = /* @__PURE__ */ __name$2((r, e, t) => $(r, e).map((a) => String(a).padStart(t, "0")), "fr"), j = /* @__PURE__ */ __name$2((r, e) => $(N(r), N(e)).map(vr), "j"), E = o(/\\./, v), dr = o(/[$.*+?^(){}[\]\|]/, (r) => `\\${r}`), hr = o(/./, v), xr = o(/^(?:!!)*!(.*)$/, (r, e) => `(?!^${q(e)}$).*?`), gr = o(/^(!!)+/, ""), mr = p([xr, gr]), _r = o(/\/(\*\*\/)+/, "(?:[\\\\/].+[\\\\/]|[\\\\/])"), Sr = o(/^(\*\*\/)+/, "(?:^|.*[\\\\/])"), yr = o(/\/(\*\*)$/, "(?:[\\\\/].*|$)"), Cr = o(/\*\*/, ".*"), I = p([_r, Sr, yr, Cr]), wr = o(/\*\/(?!\*\*\/|\*$)/, "[^\\\\/]*[\\\\/]"), $r = o(/\*/, "[^\\\\/]*"), L = p([wr, $r]), F = o("?", "[^\\\\/]"), Er = o("[", v), br = o("]", v), kr = o(/[!^]/, "^\\\\/"), Mr = o(/[a-z]-[a-z]|[0-9]-[0-9]/i, v), Rr = o(/[$.*+?^(){}[\|]/, (r) => `\\${r}`), Or = o(/[^\]]/, v), Pr = p([E, Rr, Mr, Or]), U = _([Er, lr(kr), m(Pr), br]), Ar = o("{", "(?:"), zr = o("}", ")"), Br = o(/(\d+)\.\.(\d+)/, (r, e, t) => fr(+e, +t, Math.min(e.length, t.length)).join("|")), Nr = o(/([a-z]+)\.\.([a-z]+)/, (r, e, t) => j(e, t).join("|")), jr = o(/([A-Z]+)\.\.([A-Z]+)/, (r, e, t) => j(e.toLowerCase(), t.toLowerCase()).join("|").toUpperCase()), Ir = p([Br, Nr, jr]), Z = _([Ar, Ir, zr]), Lr = o("{", "(?:"), Fr = o("}", ")"), Ur = o(",", "|"), Zr = o(/[$.*+?^(){[\]\|]/, (r) => `\\${r}`), Dr = o(/[^}]/, v), qr = A(() => D), Vr = p([I, L, F, U, Z, qr, E, Zr, Ur, Dr]), D = _([Lr, m(Vr), Fr]), Gr = m(p([mr, I, L, F, U, Z, D, E, dr, hr])), Qr = Gr, Hr = z(Qr), q = Hr, Jr = o(/\\./, v), Tr = o(/./, v), Wr = o(/\*\*\*+/, "*"), Kr = o(/([^/{[(!])\*\*/, (r, e) => `${e}*`), Xr = o(/(^|.)\*\*(?=[^*/)\]}])/, (r, e) => `${e}*`), Yr = m(p([Jr, Wr, Kr, Xr, Tr])), re = Yr, ee = z(re), te = ee, S = /* @__PURE__ */ __name$2((r, e) => Array.isArray(r) ? r.map(S.compile).some((n) => n.test(e)) : S.compile(r).test(e), "S");
S.compile = pr((r) => new RegExp(`^${q(te(r))}[\\\\/]?$`, "s"));
var ne = S;
return W(b);
})();
return __lib__.default || __lib__;
}, "_lazyMatch");
let _match;
const zeptomatch = /* @__PURE__ */ __name$2((path, pattern) => {
if (!_match) {
_match = _lazyMatch();
_lazyMatch = null;
}
return _match(path, pattern);
}, "default");
var __defProp$1 = Object.defineProperty;
var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true });
const DRIVE_LETTER_START_RE = /^[A-Z]:\//i;
const normalizeWindowsPath = /* @__PURE__ */ __name$1((input = "") => {
if (!input) {
return input;
}
return input.replaceAll("\\", "/").replace(DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
}, "normalizeWindowsPath");
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const UNC_REGEX = /^[/\\]{2}/;
const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Z]:[/\\]/i;
const DRIVE_LETTER_RE = /^[A-Z]:$/i;
const ROOT_FOLDER_RE = /^\/([A-Z]:)?$/i;
const EXTNAME_RE = /.(\.[^./]+)$/;
const PATH_ROOT_RE = /^[/\\]|^[a-z]:[/\\]/i;
const cwd = /* @__PURE__ */ __name(() => {
if (typeof process.cwd === "function") {
return process.cwd().replaceAll("\\", "/");
}
return "/";
}, "cwd");
const sep = "/";
const delimiter = /^win/i.test(globalThis.process?.platform) ? ";" : ":";
const normalizeString = /* @__PURE__ */ __name((path, allowAboveRoot) => {
let result = "";
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let char = null;
for (let index = 0; index <= path.length; ++index) {
if (index < path.length) {
char = path[index];
} else if (char === "/") {
break;
} else {
char = "/";
}
if (char === "/") {
if (lastSlash === index - 1 || dots === 1) ; else if (dots === 2) {
if (result.length < 2 || lastSegmentLength !== 2 || !result.endsWith(".") || result.at(-2) !== ".") {
if (result.length > 2) {
const lastSlashIndex = result.lastIndexOf("/");
if (lastSlashIndex === -1) {
result = "";
lastSegmentLength = 0;
} else {
result = result.slice(0, lastSlashIndex);
lastSegmentLength = result.length - 1 - result.lastIndexOf("/");
}
lastSlash = index;
dots = 0;
continue;
} else if (result.length > 0) {
result = "";
lastSegmentLength = 0;
lastSlash = index;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
result += result.length > 0 ? "/.." : "..";
lastSegmentLength = 2;
}
} else {
if (result.length > 0) {
result += `/${path.slice(lastSlash + 1, index)}`;
} else {
result = path.slice(lastSlash + 1, index);
}
lastSegmentLength = index - lastSlash - 1;
}
lastSlash = index;
dots = 0;
} else if (char === "." && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return result;
}, "normalizeString");
const isAbsolute = /* @__PURE__ */ __name((path) => IS_ABSOLUTE_RE.test(path), "isAbsolute");
const normalize = /* @__PURE__ */ __name(function(path) {
if (path.length === 0) {
return ".";
}
path = normalizeWindowsPath(path);
const isUNCPath = UNC_REGEX.exec(path);
const isPathAbsolute = isAbsolute(path);
const trailingSeparator = path.at(-1) === "/";
path = normalizeString(path, !isPathAbsolute);
if (path.length === 0) {
if (isPathAbsolute) {
return "/";
}
return trailingSeparator ? "./" : ".";
}
if (trailingSeparator) {
path += "/";
}
if (DRIVE_LETTER_RE.test(path)) {
path += "/";
}
if (isUNCPath) {
if (!isPathAbsolute) {
return `//./${path}`;
}
return `//${path}`;
}
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
}, "normalize");
const join = /* @__PURE__ */ __name((...segments) => {
let path = "";
for (const seg of segments) {
if (!seg) {
continue;
}
if (path.length > 0) {
const pathTrailing = path[path.length - 1] === "/";
const segLeading = seg[0] === "/";
const both = pathTrailing && segLeading;
if (both) {
path += seg.slice(1);
} else {
path += pathTrailing || segLeading ? seg : `/${seg}`;
}
} else {
path += seg;
}
}
return normalize(path);
}, "join");
const resolve = /* @__PURE__ */ __name(function(...arguments_) {
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
let resolvedPath = "";
let resolvedAbsolute = false;
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
const path = index >= 0 ? arguments_[index] : cwd();
if (!path || path.length === 0) {
continue;
}
resolvedPath = `${path}/${resolvedPath}`;
resolvedAbsolute = isAbsolute(path);
}
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
if (resolvedAbsolute && !isAbsolute(resolvedPath)) {
return `/${resolvedPath}`;
}
return resolvedPath.length > 0 ? resolvedPath : ".";
}, "resolve");
const toNamespacedPath = /* @__PURE__ */ __name(function(p) {
return normalizeWindowsPath(p);
}, "toNamespacedPath");
const extname = /* @__PURE__ */ __name(function(p) {
const match = EXTNAME_RE.exec(normalizeWindowsPath(p));
return match?.[1] ?? "";
}, "extname");
const relative = /* @__PURE__ */ __name(function(from, to) {
const splitFrom = resolve(from).replace(ROOT_FOLDER_RE, "$1").split("/");
const splitTo = resolve(to).replace(ROOT_FOLDER_RE, "$1").split("/");
if (splitTo[0][1] === ":" && splitFrom[0][1] === ":" && splitFrom[0] !== splitTo[0]) {
return splitTo.join("/");
}
const fromCopy = [...splitFrom];
for (const segment of fromCopy) {
if (splitTo[0] !== segment) {
break;
}
splitFrom.shift();
splitTo.shift();
}
return [...splitFrom.map(() => ".."), ...splitTo].join("/");
}, "relative");
const dirname = /* @__PURE__ */ __name((path) => {
const segments = normalizeWindowsPath(path).replace(/\/$/, "").split("/").slice(0, -1);
if (segments.length === 1 && DRIVE_LETTER_RE.test(segments[0])) {
segments[0] += "/";
}
return segments.join("/") || (isAbsolute(path) ? "/" : ".");
}, "dirname");
const format = /* @__PURE__ */ __name(function(pathObject) {
const segments = [pathObject.root, pathObject.dir, pathObject.base ?? pathObject.name + pathObject.ext].filter(Boolean);
return normalizeWindowsPath(pathObject.root ? resolve(...segments) : segments.join("/"));
}, "format");
const basename = /* @__PURE__ */ __name((path, extension) => {
const lastSegment = normalizeWindowsPath(path).split("/").pop();
if (extension && lastSegment.endsWith(extension)) {
return lastSegment.slice(0, -extension.length);
}
return lastSegment;
}, "basename");
const parse = /* @__PURE__ */ __name(function(p) {
const root = PATH_ROOT_RE.exec(p)?.[0]?.replace(/\\/g, "/") ?? "";
const base = basename(p);
const extension = extname(base);
return {
base,
dir: dirname(p),
ext: extension,
name: base.slice(0, base.length - extension.length),
root
};
}, "parse");
const matchesGlob = /* @__PURE__ */ __name((path, pattern) => (
// https://github.com/nodejs/node/blob/main/lib/internal/fs/glob.js#L660
zeptomatch(pattern, normalize(path))
), "matchesGlob");
const path = /*#__PURE__*/Object.defineProperty({
__proto__: null,
basename,
delimiter,
dirname,
extname,
format,
isAbsolute,
join,
matchesGlob,
normalize,
normalizeString,
parse,
relative,
resolve,
sep,
toNamespacedPath
}, Symbol.toStringTag, { value: 'Module' });
exports.basename = basename;
exports.delimiter = delimiter;
exports.dirname = dirname;
exports.extname = extname;
exports.format = format;
exports.isAbsolute = isAbsolute;
exports.join = join;
exports.matchesGlob = matchesGlob;
exports.normalize = normalize;
exports.normalizeString = normalizeString;
exports.normalizeWindowsPath = normalizeWindowsPath;
exports.parse = parse;
exports.path = path;
exports.relative = relative;
exports.resolve = resolve;
exports.sep = sep;
exports.toNamespacedPath = toNamespacedPath;