42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
export type ChannelEntryMatch<T> = {
|
|
entry?: T;
|
|
key?: string;
|
|
wildcardEntry?: T;
|
|
wildcardKey?: string;
|
|
};
|
|
|
|
export function buildChannelKeyCandidates(
|
|
...keys: Array<string | undefined | null>
|
|
): string[] {
|
|
const seen = new Set<string>();
|
|
const candidates: string[] = [];
|
|
for (const key of keys) {
|
|
if (typeof key !== "string") continue;
|
|
const trimmed = key.trim();
|
|
if (!trimmed || seen.has(trimmed)) continue;
|
|
seen.add(trimmed);
|
|
candidates.push(trimmed);
|
|
}
|
|
return candidates;
|
|
}
|
|
|
|
export function resolveChannelEntryMatch<T>(params: {
|
|
entries?: Record<string, T>;
|
|
keys: string[];
|
|
wildcardKey?: string;
|
|
}): ChannelEntryMatch<T> {
|
|
const entries = params.entries ?? {};
|
|
const match: ChannelEntryMatch<T> = {};
|
|
for (const key of params.keys) {
|
|
if (!Object.prototype.hasOwnProperty.call(entries, key)) continue;
|
|
match.entry = entries[key];
|
|
match.key = key;
|
|
break;
|
|
}
|
|
if (params.wildcardKey && Object.prototype.hasOwnProperty.call(entries, params.wildcardKey)) {
|
|
match.wildcardEntry = entries[params.wildcardKey];
|
|
match.wildcardKey = params.wildcardKey;
|
|
}
|
|
return match;
|
|
}
|