utils.mjs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { n as normalizeWindowsPath, j as join } from './shared/pathe.ff20891b.mjs';
  2. const pathSeparators = /* @__PURE__ */ new Set(["/", "\\", void 0]);
  3. const normalizedAliasSymbol = Symbol.for("pathe:normalizedAlias");
  4. function normalizeAliases(_aliases) {
  5. if (_aliases[normalizedAliasSymbol]) {
  6. return _aliases;
  7. }
  8. const aliases = Object.fromEntries(
  9. Object.entries(_aliases).sort(([a], [b]) => _compareAliases(a, b))
  10. );
  11. for (const key in aliases) {
  12. for (const alias in aliases) {
  13. if (alias === key || key.startsWith(alias)) {
  14. continue;
  15. }
  16. if (aliases[key].startsWith(alias) && pathSeparators.has(aliases[key][alias.length])) {
  17. aliases[key] = aliases[alias] + aliases[key].slice(alias.length);
  18. }
  19. }
  20. }
  21. Object.defineProperty(aliases, normalizedAliasSymbol, {
  22. value: true,
  23. enumerable: false
  24. });
  25. return aliases;
  26. }
  27. function resolveAlias(path, aliases) {
  28. const _path = normalizeWindowsPath(path);
  29. aliases = normalizeAliases(aliases);
  30. for (const [alias, to] of Object.entries(aliases)) {
  31. if (!_path.startsWith(alias)) {
  32. continue;
  33. }
  34. const _alias = hasTrailingSlash(alias) ? alias.slice(0, -1) : alias;
  35. if (hasTrailingSlash(_path[_alias.length])) {
  36. return join(to, _path.slice(alias.length));
  37. }
  38. }
  39. return _path;
  40. }
  41. const FILENAME_RE = /(^|[/\\])([^/\\]+?)(?=(\.[^.]+)?$)/;
  42. function filename(path) {
  43. return path.match(FILENAME_RE)?.[2];
  44. }
  45. function _compareAliases(a, b) {
  46. return b.split("/").length - a.split("/").length;
  47. }
  48. function hasTrailingSlash(path = "/") {
  49. const lastChar = path[path.length - 1];
  50. return lastChar === "/" || lastChar === "\\";
  51. }
  52. export { filename, normalizeAliases, resolveAlias };