Skip to content

Commit

Permalink
feature: add matched rule hint
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamscached committed Apr 9, 2024
1 parent 61f75c7 commit ec7dae0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/lib/curses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ function getListStringContents(tokens: SimpleToken[], varName: string): string[]
);
}

export function checkName(name: string, regExps: Record<string, RegExp[]>): string | null {
const found = Object.entries(regExps).find(([_, re]) => re.find((it) => it.test(name)));
if (found === undefined) return null;
return found[0];
export function checkName(name: string, regExps: Record<string, RegExp[]>): [string, RegExp] | null {
// @prettier-ignore
const found = Object.entries(regExps)
.map(([key, re]) => [key, re.find((it) => it.test(name))])
.filter((it) => it[1]);
if (found.length === 0) return null;
return found[0] as [string, RegExp];
}
8 changes: 7 additions & 1 deletion src/routes/curses/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let name: string;
let matchKey: string;
let matchRule: RegExp;
onMount(async () => {
libCurses = await import("$lib/curses");
Expand All @@ -19,7 +20,9 @@
});
function onNameInput(e: Event) {
matchKey = libCurses.checkName((e.target as HTMLInputElement).value, regexps);
name = (e.target as HTMLInputElement).value;
const match = libCurses.checkName(name, regexps);
[matchKey, matchRule] = match ?? [null, null];
}
</script>

Expand Down Expand Up @@ -63,6 +66,9 @@
<div class="mb-3">
<NameInput on:input={onNameInput}/>
</div>
{#if matchRule}
<p class="text-center">Matched rule: <span class="font-mono bg-neutral-200 rounded-md py-0.5 px-1">{matchRule.toString()}</span></p>
{/if}
</div>
{/await}
</div>
Expand Down

0 comments on commit ec7dae0

Please sign in to comment.