@ztimson/utils 0.27.13 → 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.cjs CHANGED
@@ -1958,24 +1958,14 @@ ${opts.message || this.desc}`;
1958
1958
  Object.assign(this, PathEvent.pathEventCache.get(e));
1959
1959
  return;
1960
1960
  }
1961
- let [p, method] = e.replaceAll(/\/{2,}/g, "/").split(":");
1961
+ let [p, method] = e.replaceAll(/(^|\/)\*+\/?$/g, "").split(":");
1962
1962
  if (!method) method = "*";
1963
- if (p === "" || p === void 0) {
1964
- this.module = "";
1965
- this.path = "";
1966
- this.fullPath = "";
1967
- this.name = "";
1968
- this.methods = new ASet(["n"]);
1969
- this.hasGlob = false;
1970
- PathEvent.pathEventCache.set(e, this);
1971
- return;
1972
- }
1973
- if (p === "*") {
1963
+ if (p === "" || p === void 0 || p === "*") {
1974
1964
  this.module = "";
1975
1965
  this.path = "";
1976
1966
  this.fullPath = "**";
1977
1967
  this.name = "";
1978
- this.methods = new ASet(["*"]);
1968
+ this.methods = new ASet(p === "*" ? ["*"] : method.split(""));
1979
1969
  this.hasGlob = true;
1980
1970
  PathEvent.pathEventCache.set(e, this);
1981
1971
  return;
@@ -1989,27 +1979,19 @@ ${opts.message || this.desc}`;
1989
1979
  this.methods = new ASet(method.split(""));
1990
1980
  PathEvent.pathEventCache.set(e, this);
1991
1981
  }
1992
- /** Clear the cache of all PathEvents */
1993
- static clearCache() {
1994
- PathEvent.pathEventCache.clear();
1995
- }
1996
- /** Clear the permission cache */
1997
- static clearPermissionCache() {
1998
- PathEvent.permissionCache.clear();
1999
- }
2000
1982
  /**
2001
- * Score a path for specificity ranking (lower = more specific = higher priority)
1983
+ * Check if a filter pattern matches a target path
2002
1984
  * @private
2003
1985
  */
2004
- static scoreSpecificity(path) {
2005
- if (path === "**" || path === "") return Number.MAX_SAFE_INTEGER;
2006
- const segments = path.split("/").filter((p) => !!p);
2007
- let score = -segments.length;
2008
- segments.forEach((seg) => {
2009
- if (seg === "**") score += 0.5;
2010
- else if (seg === "*") score += 0.25;
2011
- });
2012
- return score;
1986
+ static matches(pattern, target) {
1987
+ const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
1988
+ if (!methodsMatch) return false;
1989
+ if (!pattern.hasGlob && !target.hasGlob) {
1990
+ const last = pattern.fullPath[target.fullPath.length];
1991
+ return pattern.fullPath.startsWith(target.fullPath) && (last == null || last == "/");
1992
+ }
1993
+ if (pattern.hasGlob) return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
1994
+ return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
2013
1995
  }
2014
1996
  /**
2015
1997
  * Check if a path matches a glob pattern
@@ -2024,14 +2006,9 @@ ${opts.message || this.desc}`;
2024
2006
  while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
2025
2007
  const patternPart = patternParts[patternIdx];
2026
2008
  if (patternPart === "**") {
2027
- if (patternIdx === patternParts.length - 1) {
2028
- return true;
2029
- }
2030
- patternParts[patternIdx + 1];
2009
+ if (patternIdx === patternParts.length - 1) return true;
2031
2010
  while (pathIdx < pathParts.length) {
2032
- if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) {
2033
- return true;
2034
- }
2011
+ if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) return true;
2035
2012
  pathIdx++;
2036
2013
  }
2037
2014
  return false;
@@ -2039,18 +2016,36 @@ ${opts.message || this.desc}`;
2039
2016
  pathIdx++;
2040
2017
  patternIdx++;
2041
2018
  } else {
2042
- if (patternPart !== pathParts[pathIdx]) {
2043
- return false;
2044
- }
2019
+ if (patternPart !== pathParts[pathIdx]) return false;
2045
2020
  pathIdx++;
2046
2021
  patternIdx++;
2047
2022
  }
2048
2023
  }
2049
- if (patternIdx < patternParts.length) {
2050
- return patternParts.slice(patternIdx).every((p) => p === "**");
2051
- }
2024
+ if (patternIdx < patternParts.length) return patternParts.slice(patternIdx).every((p) => p === "**");
2052
2025
  return pathIdx === pathParts.length;
2053
2026
  }
2027
+ /**
2028
+ * Score a path for specificity ranking (lower = more specific = higher priority)
2029
+ * @private
2030
+ */
2031
+ static scoreSpecificity(path) {
2032
+ if (path === "**" || path === "") return Number.MAX_SAFE_INTEGER;
2033
+ const segments = path.split("/").filter((p) => !!p);
2034
+ let score = -segments.length;
2035
+ segments.forEach((seg) => {
2036
+ if (seg === "**") score += 0.5;
2037
+ else if (seg === "*") score += 0.25;
2038
+ });
2039
+ return score;
2040
+ }
2041
+ /** Clear the cache of all PathEvents */
2042
+ static clearCache() {
2043
+ PathEvent.pathEventCache.clear();
2044
+ }
2045
+ /** Clear the permission cache */
2046
+ static clearPermissionCache() {
2047
+ PathEvent.permissionCache.clear();
2048
+ }
2054
2049
  /**
2055
2050
  * Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
2056
2051
  * combined until a "none" is reached
@@ -2071,9 +2066,7 @@ ${opts.message || this.desc}`;
2071
2066
  result = p;
2072
2067
  } else {
2073
2068
  if (result.fullPath.startsWith(p.fullPath)) {
2074
- if (p.none) {
2075
- break;
2076
- }
2069
+ if (p.none) break;
2077
2070
  result.methods = new ASet([...result.methods, ...p.methods]);
2078
2071
  }
2079
2072
  }
@@ -2092,23 +2085,6 @@ ${opts.message || this.desc}`;
2092
2085
  const parsedFilter = makeArray(filter).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
2093
2086
  return parsedTarget.filter((t) => !!parsedFilter.find((r) => PathEvent.matches(r, t)));
2094
2087
  }
2095
- /**
2096
- * Check if a filter pattern matches a target path
2097
- * @private
2098
- */
2099
- static matches(pattern, target) {
2100
- if (pattern.fullPath === "" || target.fullPath === "") return false;
2101
- if (pattern.fullPath === "*" || target.fullPath === "*") return pattern.methods.has("*") || target.methods.has("*") || pattern.methods.intersection(target.methods).length > 0;
2102
- const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
2103
- if (!methodsMatch) return false;
2104
- if (!pattern.hasGlob && !target.hasGlob) {
2105
- return pattern.fullPath === target.fullPath;
2106
- }
2107
- if (pattern.hasGlob) {
2108
- return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
2109
- }
2110
- return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
2111
- }
2112
2088
  /**
2113
2089
  * Squash 2 sets of paths & return true if any overlap is found
2114
2090
  *