UNPKG

@visulima/path

Version:

Drop-in replacement of the Node.js path module.

512 lines (507 loc) 19.9 kB
let _lazyMatch = () => { var __lib__ = (() => { var C = Object.defineProperty, ee = Object.getOwnPropertyDescriptor, re = Object.getOwnPropertyNames, te = Object.prototype.hasOwnProperty, ne = (e, r) => { for (var t in r) C(e, t, { get: r[t], enumerable: true }); }, ae = (e, r, t, a) => { if (r && typeof r == "object" || typeof r == "function") for (let n of re(r)) !te.call(e, n) && n !== t && C(e, n, { get: () => r[n], enumerable: !(a = ee(r, n)) || a.enumerable }); return e; }, oe = (e) => ae(C({}, "__esModule", { value: true }), e), z = {}; ne(z, { zeptomatch: () => X }); var M = (e) => { const r = /* @__PURE__ */ new Set(), t = [e]; for (let a = 0; a < t.length; a++) { const n = t[a]; if (r.has(n)) continue; r.add(n); const { children: o } = n; if (o?.length) for (let s = 0, u = o.length; s < u; s++) t.push(o[s]); } return Array.from(r); }, se = (e) => { let r = ""; const t = M(e); for (let a = 0, n = t.length; a < n; a++) { const o = t[a]; if (!o.regex) continue; const s = o.regex.flags; if (r || (r = s), r !== s) throw new Error(`Inconsistent RegExp flags used: "${r}" and "${s}"`); } return r; }, O = (e, r, t) => { const a = t.get(e); if (a !== void 0) return a; const n = e.partial ?? r; let o = ""; if (e.regex && (o += n ? "(?:$|" : "", o += e.regex.source), e.children?.length) { const s = ue(e.children.map((u) => O(u, r, t)).filter(Boolean)); if (s?.length) { const u = e.children.some((E) => !E.regex || !(E.partial ?? r)), l = s.length > 1 || n && (!o.length || u); o += l ? n ? "(?:$|" : "(?:" : "", o += s.join("|"), o += l ? ")" : ""; } } return e.regex && (o += n ? ")" : ""), t.set(e, o), o; }, ie = (e, r) => { const t = /* @__PURE__ */ new Map(), a = M(e); for (let n = a.length - 1; n >= 0; n--) { const o = O(a[n], r, t); if (!(n > 0)) return o; } return ""; }, ue = (e) => Array.from(new Set(e)), R = (e, r, t) => R.compile(e, t).test(r); R.compile = (e, r) => { const t = r?.partial ?? false, a = ie(e, t), n = se(e); return new RegExp(`^(?:${a})$`, n); }; var ce = R, le = (e, r) => { const t = ce.compile(e, r), a = `${t.source.slice(0, -1)}[\\\\/]?$`, n = t.flags; return new RegExp(a, n); }, pe = le, ve = (e) => { const r = e.map((a) => a.source).join("|") || "$^", t = e[0]?.flags; return new RegExp(r, t); }, fe = ve, j = (e) => Array.isArray(e), _ = (e) => typeof e == "function", he = (e) => e.length === 0, ge = (() => { const { toString: e } = Function.prototype, r = /(?:^\(\s*(?:[^,.()]|\.(?!\.\.))*\s*\)\s*=>|^\s*[a-zA-Z$_][a-zA-Z0-9$_]*\s*=>)/; return (t) => (t.length === 0 || t.length === 1) && r.test(e.call(t)); })(), de = (e) => typeof e == "number", xe = (e) => typeof e == "object" && e !== null, me = (e) => e instanceof RegExp, Se = /* @__PURE__ */ (() => { const e = /\\\(|\((?!\?(?::|=|!|<=|<!))/; return (r) => e.test(r.source); })(), _e = /* @__PURE__ */ (() => { const e = /^[a-zA-Z0-9_-]+$/; return (r) => e.test(r.source) && !r.flags.includes("i"); })(), A = (e) => typeof e == "string", f = (e) => e === void 0, ye = (e) => { const r = /* @__PURE__ */ new Map(); return (t) => { const a = r.get(t); if (a !== void 0) return a; const n = e(t); return r.set(t, n), n; }; }, I = (e, r, t = {}) => { const a = { cache: {}, input: e, index: 0, indexBacktrackMax: 0, options: t, output: [] }, n = h(r)(a), o = Math.max(a.index, a.indexBacktrackMax); if (n && a.index === e.length) return a.output; throw new Error(`Failed to parse at index ${o}`); }, i = (e, r) => j(e) ? we(e, r) : A(e) ? F(e, r) : $e(e, r), we = (e, r) => { const t = {}; for (const a of e) { 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 o = a.index, s = o; for (; s < n.length && n.charCodeAt(s) in t; ) s += 1; if (s > o) { if (!f(r) && !a.options.silent) { const u = n.slice(o, s), l = _(r) ? r(u, n, `${o}`) : r; f(l) || a.output.push(l); } a.index = s; } return true; }; }, $e = (e, r) => { if (_e(e)) return F(e.source, r); { const t = e.source, a = e.flags.replace(/y|$/, "y"), n = new RegExp(t, a); return Se(e) && _(r) && !ge(r) ? Ee(n, r) : Ce(n, r); } }, Ee = (e, r) => (t) => { const a = t.index, n = t.input; e.lastIndex = a; const o = e.exec(n); if (o) { const s = e.lastIndex; if (!t.options.silent) { const u = r(...o, n, `${a}`); f(u) || t.output.push(u); } return t.index = s, true; } else return false; }, Ce = (e, r) => (t) => { const a = t.index, n = t.input; if (e.lastIndex = a, e.test(n)) { const s = e.lastIndex; if (!f(r) && !t.options.silent) { const u = _(r) ? r(n.slice(a, s), n, `${a}`) : r; f(u) || t.output.push(u); } return t.index = s, true; } else return false; }, F = (e, r) => (t) => { const a = t.index, n = t.input; if (n.startsWith(e, a)) { if (!f(r) && !t.options.silent) { const s = _(r) ? r(e, n, `${a}`) : r; f(s) || t.output.push(s); } return t.index += e.length, true; } else return false; }, k = (e, r, t, a) => { const n = h(e), o = r > 1; return P(B(q((s) => { let u = 0; for (; u < t; ) { const l = s.index; if (!n(s) || (u += 1, s.index === l)) break; } return u >= r; }, o), a)); }, L = (e, r) => k(e, 0, 1, r), $ = (e, r) => k(e, 0, 1 / 0, r), Re = (e, r) => k(e, 1, 1 / 0, r), x = (e, r) => { const t = e.map(h); return P(B(q((a) => { for (let n = 0, o = t.length; n < o; n++) if (!t[n](a)) return false; return true; }), r)); }, p = (e, r) => { const t = e.map(h); return P(B((a) => { for (let n = 0, o = t.length; n < o; n++) if (t[n](a)) return true; return false; }, r)); }, q = (e, r = true, t = false) => { const a = h(e); return r ? (n) => { const o = n.index, s = n.output.length, u = a(n); return !u && !t && (n.indexBacktrackMax = Math.max(n.indexBacktrackMax, n.index)), (!u || t) && (n.index = o, n.output.length !== s && (n.output.length = s)), u; } : a; }, B = (e, r) => { const t = h(e); return r ? (a) => { if (a.options.silent) return t(a); const n = a.output.length; if (t(a)) { const s = a.output.splice(n, 1 / 0), u = r(s); return f(u) || a.output.push(u), true; } else return false; } : t; }, P = /* @__PURE__ */ (() => { let e = 0; return (r) => { const t = h(r), a = e += 1; return (n) => { var o; if (n.options.memoization === false) return t(n); const s = n.index, u = (o = n.cache)[a] || (o[a] = { indexMax: -1, queue: [] }), l = u.queue; if (s <= u.indexMax) { const w = u.store || (u.store = /* @__PURE__ */ new Map()); if (l.length) { for (let S = 0, br = l.length; S < br; S += 2) { const Nr = l[S * 2], zr = l[S * 2 + 1]; w.set(Nr, zr); } l.length = 0; } const v = w.get(s); if (v === false) return false; if (de(v)) return n.index = v, true; if (v) return n.index = v.index, v.output?.length && n.output.push(...v.output), true; } const Y = n.output.length, Pr = t(n); if (u.indexMax = Math.max(u.indexMax, s), Pr) { const w = n.index, v = n.output.length; if (v > Y) { const S = n.output.slice(Y, v); l.push(s, { index: w, output: S }); } else l.push(s, w); return true; } else return l.push(s, false), false; }; }; })(), G = (e) => { let r; return (t) => (r || (r = h(e())), r(t)); }, h = ye((e) => { if (_(e)) return he(e) ? G(e) : e; if (A(e) || me(e)) return i(e); if (j(e)) return x(e); if (xe(e)) return p(Object.values(e)); throw new Error("Invalid rule"); }), d = (e) => e, ke = (e) => typeof e == "string", Be = (e) => { const r = /* @__PURE__ */ new WeakMap(), t = /* @__PURE__ */ new WeakMap(); return (a, n) => { const o = n?.partial ? t : r, s = o.get(a); if (s !== void 0) return s; const u = e(a, n); return o.set(a, u), u; }; }, Pe = (e) => { const r = {}, t = {}; return (a, n) => { const o = n?.partial ? t : r; return o[a] ?? (o[a] = e(a, n)); }; }, be = i(/\\./, d), Ne = i(/./, d), ze = i(/\*\*\*+/, "*"), Me = i(/([^/{[(!])\*\*/, (e, r) => `${r}*`), Oe = i(/(^|.)\*\*(?=[^*/)\]}])/, (e, r) => `${r}*`), je = $(p([be, ze, Me, Oe, Ne])), Ae = je, Ie = (e) => I(e, Ae, { memoization: false }).join(""), Fe = Ie, U = "abcdefghijklmnopqrstuvwxyz", Le = (e) => { let r = ""; for (; e > 0; ) { const t = (e - 1) % 26; r = U[t] + r, e = Math.floor((e - 1) / 26); } return r; }, V = (e) => { let r = 0; for (let t = 0, a = e.length; t < a; t++) r = r * 26 + U.indexOf(e[t]) + 1; return r; }, b = (e, r) => { if (r < e) return b(r, e); const t = []; for (; e <= r; ) t.push(e++); return t; }, qe = (e, r, t) => b(e, r).map((a) => String(a).padStart(t, "0")), W = (e, r) => b(V(e), V(r)).map(Le), c = (e) => ({ partial: false, regex: new RegExp(e, "s"), children: [] }), y = (e) => ({ children: e }), m = /* @__PURE__ */ (() => { const e = (r, t, a) => { if (a.has(r)) return; a.add(r); const { children: n } = r; if (!n.length) n.push(t); else for (let o = 0, s = n.length; o < s; o++) e(n[o], t, a); }; return (r) => { if (!r.length) return y([]); for (let t = r.length - 1; t >= 1; t--) { const a = /* @__PURE__ */ new Set(), n = r[t - 1], o = r[t]; e(n, o, a); } return r[0]; }; })(), g = () => ({ regex: new RegExp("[\\\\/]", "s"), children: [] }), Ge = i(/\\./, c), Ue = i(/[$.*+?^(){}[\]\|]/, (e) => c(`\\${e}`)), Ve = i(/[\\\/]/, g), We = i(/[^$.*+?^(){}[\]\|\\\/]+/, c), Ze = i(/^(?:!!)*!(.*)$/, (e, r) => c(`(?!^${X.compile(r).source}$).*?`)), De = i(/^(!!)+/), Qe = p([Ze, De]), Te = i(/\/(\*\*\/)+/, () => y([m([g(), c(".+?"), g()]), g()])), He = i(/^(\*\*\/)+/, () => y([c("^"), m([c(".*?"), g()])])), Je = i(/\/(\*\*)$/, () => y([m([g(), c(".*?")]), c("$")])), Ke = i(/\*\*/, () => c(".*?")), Z = p([Te, He, Je, Ke]), Xe = i(/\*\/(?!\*\*\/|\*$)/, () => m([c("[^\\\\/]*?"), g()])), Ye = i(/\*/, () => c("[^\\\\/]*")), D = p([Xe, Ye]), Q = i("?", () => c("[^\\\\/]")), er = i("[", d), rr = i("]", d), tr = i(/[!^]/, "^\\\\/"), nr = i(/[a-z]-[a-z]|[0-9]-[0-9]/i, d), ar = i(/\\./, d), or = i(/[$.*+?^(){}[\|]/, (e) => `\\${e}`), sr = i(/[\\\/]/, "\\\\/"), ir = i(/[^$.*+?^(){}[\]\|\\\/]+/, d), ur = p([ar, or, sr, nr, ir]), T = x([er, L(tr), $(ur), rr], (e) => c(e.join(""))), cr = i("{", "(?:"), lr = i("}", ")"), pr = i(/(\d+)\.\.(\d+)/, (e, r, t) => qe(+r, +t, Math.min(r.length, t.length)).join("|")), vr = i(/([a-z]+)\.\.([a-z]+)/, (e, r, t) => W(r, t).join("|")), fr = i(/([A-Z]+)\.\.([A-Z]+)/, (e, r, t) => W(r.toLowerCase(), t.toLowerCase()).join("|").toUpperCase()), hr = p([pr, vr, fr]), H = x([cr, hr, lr], (e) => c(e.join(""))), gr = i("{"), dr = i("}"), xr = i(","), mr = i(/\\./, c), Sr = i(/[$.*+?^(){[\]\|]/, (e) => c(`\\${e}`)), _r = i(/[\\\/]/, g), yr = i(/[^$.*+?^(){}[\]\|\\\/,]+/, c), wr = G(() => K), $r = i("", () => c("(?:)")), Er = Re(p([Z, D, Q, T, H, wr, mr, Sr, _r, yr]), m), J = p([Er, $r]), K = x([gr, L(x([J, $(x([xr, J]))])), dr], y), Cr = $(p([Qe, Z, D, Q, T, H, K, Ge, Ue, Ve, We]), m), Rr = Cr, kr = (e) => I(e, Rr, { memoization: false })[0], Br = kr, N = (e, r, t) => N.compile(e, t).test(r); N.compile = (() => { const e = Pe((t, a) => pe(Br(Fe(t)), a)), r = Be((t, a) => fe(t.map((n) => e(n, a)))); return (t, a) => ke(t) ? e(t, a) : r(t, a); })(); var X = N; return oe(z); })(); return __lib__.default || __lib__; }; let _match; const zeptomatch = (path, pattern) => { if (!_match) { _match = _lazyMatch(); _lazyMatch = null; } return _match(path, pattern); }; const DRIVE_LETTER_START_RE = /^[A-Z]:\//i; const normalizeWindowsPath = (input = "") => { if (!input) { return input; } return input.replaceAll("\\", "/").replace(DRIVE_LETTER_START_RE, (r) => r.toUpperCase()); }; 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 = () => { if (typeof process.cwd === "function") { return process.cwd().replaceAll("\\", "/"); } return "/"; }; const sep = "/"; const delimiter = /^win/i.test(globalThis.process?.platform) ? ";" : ":"; const normalizeString = (path, allowAboveRoot) => { let result = ""; let lastSegmentLength = 0; let lastSlash = -1; let dots = 0; let char; 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; }; const isAbsolute = (path) => IS_ABSOLUTE_RE.test(path); const normalize = 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; }; const join = (...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); }; const resolve = 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 : "."; }; const toNamespacedPath = function(p) { return normalizeWindowsPath(p); }; const extname = function(p) { const match = EXTNAME_RE.exec(normalizeWindowsPath(p)); return match?.[1] ?? ""; }; const relative = 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("/"); }; const dirname = (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) ? "/" : "."); }; const format = function(pathObject) { const segments = [pathObject.root, pathObject.dir, pathObject.base ?? pathObject.name + pathObject.ext].filter(Boolean); return normalizeWindowsPath(pathObject.root ? resolve(...segments) : segments.join("/")); }; const basename = (path, extension) => { const lastSegment = normalizeWindowsPath(path).split("/").pop(); if (extension && lastSegment.endsWith(extension)) { return lastSegment.slice(0, -extension.length); } return lastSegment; }; const parse = function(p) { const root = PATH_ROOT_RE.exec(p)?.[0]?.replaceAll("\\", "/") ?? ""; const base = basename(p); const extension = extname(base); return { base, dir: dirname(p), ext: extension, name: base.slice(0, base.length - extension.length), root }; }; const matchesGlob = (path, pattern) => ( // https://github.com/nodejs/node/blob/main/lib/internal/fs/glob.js#L660 zeptomatch(pattern, normalize(path)) ); 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' }); export { dirname as a, basename as b, normalizeString as c, delimiter as d, extname as e, format as f, parse as g, resolve as h, isAbsolute as i, join as j, normalizeWindowsPath as k, matchesGlob as m, normalize as n, path as p, relative as r, sep as s, toNamespacedPath as t };