@ztimson/utils 0.27.14 → 0.27.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1954,7 +1954,7 @@ class PathEvent {
1954
1954
  Object.assign(this, PathEvent.pathEventCache.get(e));
1955
1955
  return;
1956
1956
  }
1957
- let [p, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
1957
+ let [p, method] = e.replaceAll(/(^|\/)\*+\/?$/g, "").split(":");
1958
1958
  if (!method) method = "*";
1959
1959
  if (p === "" || p === void 0 || p === "*") {
1960
1960
  this.module = "";
@@ -1975,27 +1975,19 @@ class PathEvent {
1975
1975
  this.methods = new ASet(method.split(""));
1976
1976
  PathEvent.pathEventCache.set(e, this);
1977
1977
  }
1978
- /** Clear the cache of all PathEvents */
1979
- static clearCache() {
1980
- PathEvent.pathEventCache.clear();
1981
- }
1982
- /** Clear the permission cache */
1983
- static clearPermissionCache() {
1984
- PathEvent.permissionCache.clear();
1985
- }
1986
1978
  /**
1987
- * Score a path for specificity ranking (lower = more specific = higher priority)
1979
+ * Check if a filter pattern matches a target path
1988
1980
  * @private
1989
1981
  */
1990
- static scoreSpecificity(path) {
1991
- if (path === "**" || path === "") return Number.MAX_SAFE_INTEGER;
1992
- const segments = path.split("/").filter((p) => !!p);
1993
- let score = -segments.length;
1994
- segments.forEach((seg) => {
1995
- if (seg === "**") score += 0.5;
1996
- else if (seg === "*") score += 0.25;
1997
- });
1998
- return score;
1982
+ static matches(pattern, target) {
1983
+ const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
1984
+ if (!methodsMatch) return false;
1985
+ if (!pattern.hasGlob && !target.hasGlob) {
1986
+ const last = pattern.fullPath[target.fullPath.length];
1987
+ return pattern.fullPath.startsWith(target.fullPath) && (last == null || last == "/");
1988
+ }
1989
+ if (pattern.hasGlob) return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
1990
+ return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
1999
1991
  }
2000
1992
  /**
2001
1993
  * Check if a path matches a glob pattern
@@ -2010,14 +2002,9 @@ class PathEvent {
2010
2002
  while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
2011
2003
  const patternPart = patternParts[patternIdx];
2012
2004
  if (patternPart === "**") {
2013
- if (patternIdx === patternParts.length - 1) {
2014
- return true;
2015
- }
2016
- patternParts[patternIdx + 1];
2005
+ if (patternIdx === patternParts.length - 1) return true;
2017
2006
  while (pathIdx < pathParts.length) {
2018
- if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) {
2019
- return true;
2020
- }
2007
+ if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) return true;
2021
2008
  pathIdx++;
2022
2009
  }
2023
2010
  return false;
@@ -2025,18 +2012,36 @@ class PathEvent {
2025
2012
  pathIdx++;
2026
2013
  patternIdx++;
2027
2014
  } else {
2028
- if (patternPart !== pathParts[pathIdx]) {
2029
- return false;
2030
- }
2015
+ if (patternPart !== pathParts[pathIdx]) return false;
2031
2016
  pathIdx++;
2032
2017
  patternIdx++;
2033
2018
  }
2034
2019
  }
2035
- if (patternIdx < patternParts.length) {
2036
- return patternParts.slice(patternIdx).every((p) => p === "**");
2037
- }
2020
+ if (patternIdx < patternParts.length) return patternParts.slice(patternIdx).every((p) => p === "**");
2038
2021
  return pathIdx === pathParts.length;
2039
2022
  }
2023
+ /**
2024
+ * Score a path for specificity ranking (lower = more specific = higher priority)
2025
+ * @private
2026
+ */
2027
+ static scoreSpecificity(path) {
2028
+ if (path === "**" || path === "") return Number.MAX_SAFE_INTEGER;
2029
+ const segments = path.split("/").filter((p) => !!p);
2030
+ let score = -segments.length;
2031
+ segments.forEach((seg) => {
2032
+ if (seg === "**") score += 0.5;
2033
+ else if (seg === "*") score += 0.25;
2034
+ });
2035
+ return score;
2036
+ }
2037
+ /** Clear the cache of all PathEvents */
2038
+ static clearCache() {
2039
+ PathEvent.pathEventCache.clear();
2040
+ }
2041
+ /** Clear the permission cache */
2042
+ static clearPermissionCache() {
2043
+ PathEvent.permissionCache.clear();
2044
+ }
2040
2045
  /**
2041
2046
  * Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
2042
2047
  * combined until a "none" is reached
@@ -2057,9 +2062,7 @@ class PathEvent {
2057
2062
  result = p;
2058
2063
  } else {
2059
2064
  if (result.fullPath.startsWith(p.fullPath)) {
2060
- if (p.none) {
2061
- break;
2062
- }
2065
+ if (p.none) break;
2063
2066
  result.methods = new ASet([...result.methods, ...p.methods]);
2064
2067
  }
2065
2068
  }
@@ -2078,24 +2081,6 @@ class PathEvent {
2078
2081
  const parsedFilter = makeArray(filter).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
2079
2082
  return parsedTarget.filter((t) => !!parsedFilter.find((r) => PathEvent.matches(r, t)));
2080
2083
  }
2081
- /**
2082
- * Check if a filter pattern matches a target path
2083
- * @private
2084
- */
2085
- static matches(pattern, target) {
2086
- if (pattern.fullPath === "" || target.fullPath === "") return false;
2087
- if (pattern.fullPath === "**") return pattern.methods.has("*") || pattern.methods.intersection(target.methods).length > 0;
2088
- if (target.fullPath === "**") return pattern.methods.has("*") || target.methods.has("*") || pattern.methods.intersection(target.methods).length > 0;
2089
- const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
2090
- if (!methodsMatch) return false;
2091
- if (!pattern.hasGlob && !target.hasGlob) {
2092
- return pattern.fullPath === target.fullPath;
2093
- }
2094
- if (pattern.hasGlob) {
2095
- return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
2096
- }
2097
- return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
2098
- }
2099
2084
  /**
2100
2085
  * Squash 2 sets of paths & return true if any overlap is found
2101
2086
  *