Skip to content

Commit aeb52e1

Browse files
committed
Move empty pattern check
1 parent 61d4269 commit aeb52e1

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

web/src/lib/components/diff-filtering/DiffFilterDialog.svelte

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { getFileStatusProps, MultiFileDiffViewerState } from "$lib/diff-viewer.svelte";
33
import { Button, Dialog, ToggleGroup } from "bits-ui";
4-
import { tryCompileRegex, type TryCompileRegexFailure } from "$lib/util";
4+
import { tryCompileRegex } from "$lib/util";
55
import { FILE_STATUSES } from "$lib/github.svelte";
66
import { slide } from "svelte/transition";
77
import { type FilePathFilterMode } from "$lib/components/diff-filtering/index.svelte";
@@ -11,12 +11,7 @@
1111
1212
let newFilePathFilterElement: HTMLInputElement | undefined = $state();
1313
let newFilePathFilterInput = $state("");
14-
let newFilePathFilterInputResult = $derived.by(() => {
15-
if (newFilePathFilterInput === "") {
16-
return { success: false, error: "No input provided", input: "" } satisfies TryCompileRegexFailure;
17-
}
18-
return tryCompileRegex(newFilePathFilterInput);
19-
});
14+
let newFilePathFilterInputResult = $derived(tryCompileRegex(newFilePathFilterInput));
2015
$effect(() => {
2116
if (newFilePathFilterElement && newFilePathFilterInputResult.success) {
2217
newFilePathFilterElement.setCustomValidity("");

web/src/lib/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,11 @@ export type TryCompileRegexSuccess = { success: true; regex: RegExp; input: stri
425425
export type TryCompileRegexFailure = { success: false; error: string; input: string };
426426
export type TryCompileRegexResult = TryCompileRegexSuccess | TryCompileRegexFailure;
427427

428-
export function tryCompileRegex(pattern: string): TryCompileRegexResult {
428+
export function tryCompileRegex(pattern: string, options?: { allowEmpty?: boolean }): TryCompileRegexResult {
429+
const allowEmpty = options?.allowEmpty ?? false;
430+
if (pattern === "" && !allowEmpty) {
431+
return { success: false, error: "Pattern cannot be empty", input: pattern };
432+
}
429433
try {
430434
return { success: true, regex: new RegExp(pattern), input: pattern };
431435
} catch (e) {

0 commit comments

Comments
 (0)