refactor: share teams allowlist matching helpers

Co-authored-by: thewilloftheshadow <thewilloftheshadow@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-18 01:37:18 +00:00
parent 0674f1fa3c
commit c1da78a271
5 changed files with 174 additions and 45 deletions

View File

@@ -1,8 +1,5 @@
export type ChannelMatchSource = "direct" | "parent" | "wildcard";
export function buildChannelKeyCandidates(
...keys: Array<string | undefined | null>
): string[] {
export type ChannelEntryMatch<T> = {
entry?: T;
key?: string;
@@ -14,6 +11,15 @@ export type ChannelEntryMatch<T> = {
matchSource?: ChannelMatchSource;
};
export function normalizeChannelSlug(value: string): string {
return value
.trim()
.toLowerCase()
.replace(/^#/, "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
export function buildChannelKeyCandidates(
...keys: Array<string | undefined | null>
): string[] {
@@ -54,6 +60,7 @@ export function resolveChannelEntryMatchWithFallback<T>(params: {
keys: string[];
parentKeys?: string[];
wildcardKey?: string;
normalizeKey?: (value: string) => string;
}): ChannelEntryMatch<T> {
const direct = resolveChannelEntryMatch({
entries: params.entries,
@@ -65,6 +72,25 @@ export function resolveChannelEntryMatchWithFallback<T>(params: {
return { ...direct, matchKey: direct.key, matchSource: "direct" };
}
const normalizeKey = params.normalizeKey;
if (normalizeKey) {
const normalizedKeys = params.keys.map((key) => normalizeKey(key)).filter(Boolean);
if (normalizedKeys.length > 0) {
for (const [entryKey, entry] of Object.entries(params.entries ?? {})) {
const normalizedEntry = normalizeKey(entryKey);
if (normalizedEntry && normalizedKeys.includes(normalizedEntry)) {
return {
...direct,
entry,
key: entryKey,
matchKey: entryKey,
matchSource: "direct",
};
}
}
}
}
const parentKeys = params.parentKeys ?? [];
if (parentKeys.length > 0) {
const parent = resolveChannelEntryMatch({ entries: params.entries, keys: parentKeys });
@@ -79,6 +105,25 @@ export function resolveChannelEntryMatchWithFallback<T>(params: {
matchSource: "parent",
};
}
if (normalizeKey) {
const normalizedParentKeys = parentKeys.map((key) => normalizeKey(key)).filter(Boolean);
if (normalizedParentKeys.length > 0) {
for (const [entryKey, entry] of Object.entries(params.entries ?? {})) {
const normalizedEntry = normalizeKey(entryKey);
if (normalizedEntry && normalizedParentKeys.includes(normalizedEntry)) {
return {
...direct,
entry,
key: entryKey,
parentEntry: entry,
parentKey: entryKey,
matchKey: entryKey,
matchSource: "parent",
};
}
}
}
}
}
if (direct.wildcardEntry && direct.wildcardKey) {
@@ -93,3 +138,15 @@ export function resolveChannelEntryMatchWithFallback<T>(params: {
return direct;
}
export function resolveNestedAllowlistDecision(params: {
outerConfigured: boolean;
outerMatched: boolean;
innerConfigured: boolean;
innerMatched: boolean;
}): boolean {
if (!params.outerConfigured) return true;
if (!params.outerMatched) return false;
if (!params.innerConfigured) return true;
return params.innerMatched;
}