@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 +40 -64
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +40 -64
- package/dist/index.mjs.map +1 -1
- package/dist/path-events.d.ts +11 -11
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1954,24 +1954,14 @@ class PathEvent {
|
|
|
1954
1954
|
Object.assign(this, PathEvent.pathEventCache.get(e));
|
|
1955
1955
|
return;
|
|
1956
1956
|
}
|
|
1957
|
-
let [p, method] = e.replaceAll(
|
|
1957
|
+
let [p, method] = e.replaceAll(/(^|\/)\*+\/?$/g, "").split(":");
|
|
1958
1958
|
if (!method) method = "*";
|
|
1959
|
-
if (p === "" || p === void 0) {
|
|
1960
|
-
this.module = "";
|
|
1961
|
-
this.path = "";
|
|
1962
|
-
this.fullPath = "";
|
|
1963
|
-
this.name = "";
|
|
1964
|
-
this.methods = new ASet(["n"]);
|
|
1965
|
-
this.hasGlob = false;
|
|
1966
|
-
PathEvent.pathEventCache.set(e, this);
|
|
1967
|
-
return;
|
|
1968
|
-
}
|
|
1969
|
-
if (p === "*") {
|
|
1959
|
+
if (p === "" || p === void 0 || p === "*") {
|
|
1970
1960
|
this.module = "";
|
|
1971
1961
|
this.path = "";
|
|
1972
1962
|
this.fullPath = "**";
|
|
1973
1963
|
this.name = "";
|
|
1974
|
-
this.methods = new ASet(["*"]);
|
|
1964
|
+
this.methods = new ASet(p === "*" ? ["*"] : method.split(""));
|
|
1975
1965
|
this.hasGlob = true;
|
|
1976
1966
|
PathEvent.pathEventCache.set(e, this);
|
|
1977
1967
|
return;
|
|
@@ -1985,27 +1975,19 @@ class PathEvent {
|
|
|
1985
1975
|
this.methods = new ASet(method.split(""));
|
|
1986
1976
|
PathEvent.pathEventCache.set(e, this);
|
|
1987
1977
|
}
|
|
1988
|
-
/** Clear the cache of all PathEvents */
|
|
1989
|
-
static clearCache() {
|
|
1990
|
-
PathEvent.pathEventCache.clear();
|
|
1991
|
-
}
|
|
1992
|
-
/** Clear the permission cache */
|
|
1993
|
-
static clearPermissionCache() {
|
|
1994
|
-
PathEvent.permissionCache.clear();
|
|
1995
|
-
}
|
|
1996
1978
|
/**
|
|
1997
|
-
*
|
|
1979
|
+
* Check if a filter pattern matches a target path
|
|
1998
1980
|
* @private
|
|
1999
1981
|
*/
|
|
2000
|
-
static
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
return
|
|
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);
|
|
2009
1991
|
}
|
|
2010
1992
|
/**
|
|
2011
1993
|
* Check if a path matches a glob pattern
|
|
@@ -2020,14 +2002,9 @@ class PathEvent {
|
|
|
2020
2002
|
while (patternIdx < patternParts.length && pathIdx < pathParts.length) {
|
|
2021
2003
|
const patternPart = patternParts[patternIdx];
|
|
2022
2004
|
if (patternPart === "**") {
|
|
2023
|
-
if (patternIdx === patternParts.length - 1)
|
|
2024
|
-
return true;
|
|
2025
|
-
}
|
|
2026
|
-
patternParts[patternIdx + 1];
|
|
2005
|
+
if (patternIdx === patternParts.length - 1) return true;
|
|
2027
2006
|
while (pathIdx < pathParts.length) {
|
|
2028
|
-
if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/")))
|
|
2029
|
-
return true;
|
|
2030
|
-
}
|
|
2007
|
+
if (PathEvent.pathMatchesGlob(pathParts.slice(pathIdx).join("/"), patternParts.slice(patternIdx + 1).join("/"))) return true;
|
|
2031
2008
|
pathIdx++;
|
|
2032
2009
|
}
|
|
2033
2010
|
return false;
|
|
@@ -2035,18 +2012,36 @@ class PathEvent {
|
|
|
2035
2012
|
pathIdx++;
|
|
2036
2013
|
patternIdx++;
|
|
2037
2014
|
} else {
|
|
2038
|
-
if (patternPart !== pathParts[pathIdx])
|
|
2039
|
-
return false;
|
|
2040
|
-
}
|
|
2015
|
+
if (patternPart !== pathParts[pathIdx]) return false;
|
|
2041
2016
|
pathIdx++;
|
|
2042
2017
|
patternIdx++;
|
|
2043
2018
|
}
|
|
2044
2019
|
}
|
|
2045
|
-
if (patternIdx < patternParts.length)
|
|
2046
|
-
return patternParts.slice(patternIdx).every((p) => p === "**");
|
|
2047
|
-
}
|
|
2020
|
+
if (patternIdx < patternParts.length) return patternParts.slice(patternIdx).every((p) => p === "**");
|
|
2048
2021
|
return pathIdx === pathParts.length;
|
|
2049
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
|
+
}
|
|
2050
2045
|
/**
|
|
2051
2046
|
* Combine multiple events into one parsed object. Longest path takes precedent, but all subsequent methods are
|
|
2052
2047
|
* combined until a "none" is reached
|
|
@@ -2067,9 +2062,7 @@ class PathEvent {
|
|
|
2067
2062
|
result = p;
|
|
2068
2063
|
} else {
|
|
2069
2064
|
if (result.fullPath.startsWith(p.fullPath)) {
|
|
2070
|
-
if (p.none)
|
|
2071
|
-
break;
|
|
2072
|
-
}
|
|
2065
|
+
if (p.none) break;
|
|
2073
2066
|
result.methods = new ASet([...result.methods, ...p.methods]);
|
|
2074
2067
|
}
|
|
2075
2068
|
}
|
|
@@ -2088,23 +2081,6 @@ class PathEvent {
|
|
|
2088
2081
|
const parsedFilter = makeArray(filter).map((pe) => pe instanceof PathEvent ? pe : new PathEvent(pe));
|
|
2089
2082
|
return parsedTarget.filter((t) => !!parsedFilter.find((r) => PathEvent.matches(r, t)));
|
|
2090
2083
|
}
|
|
2091
|
-
/**
|
|
2092
|
-
* Check if a filter pattern matches a target path
|
|
2093
|
-
* @private
|
|
2094
|
-
*/
|
|
2095
|
-
static matches(pattern, target) {
|
|
2096
|
-
if (pattern.fullPath === "" || target.fullPath === "") return false;
|
|
2097
|
-
if (pattern.fullPath === "*" || target.fullPath === "*") return pattern.methods.has("*") || target.methods.has("*") || pattern.methods.intersection(target.methods).length > 0;
|
|
2098
|
-
const methodsMatch = pattern.all || target.all || pattern.methods.intersection(target.methods).length > 0;
|
|
2099
|
-
if (!methodsMatch) return false;
|
|
2100
|
-
if (!pattern.hasGlob && !target.hasGlob) {
|
|
2101
|
-
return pattern.fullPath === target.fullPath;
|
|
2102
|
-
}
|
|
2103
|
-
if (pattern.hasGlob) {
|
|
2104
|
-
return this.pathMatchesGlob(target.fullPath, pattern.fullPath);
|
|
2105
|
-
}
|
|
2106
|
-
return this.pathMatchesGlob(pattern.fullPath, target.fullPath);
|
|
2107
|
-
}
|
|
2108
2084
|
/**
|
|
2109
2085
|
* Squash 2 sets of paths & return true if any overlap is found
|
|
2110
2086
|
*
|