impr: precompute regexes for filters rather than uhhh re-creating them in a loop every single time filter() is called this is CS 101 brethren

This commit is contained in:
Xylight committed 2026-01-29 20:46:04 -08:00
1 parent 9b55e0da58
commit 9e67489df2
6 files changed
+51 -38

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
import { browser } from '$app/environment'
import { env } from '$env/dynamic/public'
import type { CommentSortType, ListingType, SortType } from '$lib/api/types'
import type { Filter } from '$lib/feature/post/filters'
import type { Filter } from '$lib/feature/post/filters.svelte'
import { locale } from './i18n'
// TODO extract settings into submodules
+2 -2
View File
@@ -6,7 +6,7 @@
import type { Snippet } from 'svelte'
import { ArchiveBox, ArrowsPointingOut, Plus } from 'svelte-hero-icons/dist'
import { Post } from '..'
import { filterPost, type FilteredItem } from '../filters'
import { filterPost, type FilteredItem } from '../filters.svelte'
import { t } from '$lib/app/i18n'
interface Props {
@@ -20,7 +20,7 @@
let filteredPosts: FilteredItem[] = $derived(
posts.map((post) => ({
id: post.post.id,
action: filterPost(post, settings.filters),
action: filterPost(post),
})),
)
+2 -2
View File
@@ -23,7 +23,7 @@
import { SvelteSet } from 'svelte/reactivity'
import { fly } from 'svelte/transition'
import { Post } from '..'
import { filterPost, type FilteredItem } from '../filters'
import { filterPost, type FilteredItem } from '../filters.svelte'
import { ReactiveState } from '$lib/app/util.svelte'
interface Props {
@@ -47,7 +47,7 @@
let filteredPosts: FilteredItem[] = $derived(
posts.map((post) => ({
id: post.post.id,
action: filterPost(post, settings.filters),
action: filterPost(post),
})),
)
+42
View File
@@ -0,0 +1,42 @@
import type { PostView } from '$lib/api/types'
import { settings } from '$lib/app/settings.svelte'
type FilterAction = 'none' | 'minimize' | 'hide'
type FilterType = 'keyword'
export interface Filter {
match: string
action: FilterAction
type: FilterType
}
type FilterEx = Filter & { regex: RegExp }
export type FilteredItem = { id: number; action: FilterAction }
// ban me from regex brother
const filtersEx: FilterEx[] = $derived(
settings.filters.map((filter) => ({
...filter,
regex: new RegExp(filter.match, 'i'),
})),
)
export function filterPost(
post: PostView,
filters: FilterEx[] = filtersEx,
): FilterAction {
for (const filter of filters) {
try {
if (
filter.regex.test(post.post.url ?? '') ||
filter.regex.test(post.post.name) ||
filter.regex.test(post.post.body ?? '')
)
return filter.action
} catch {
/* */
}
}
return 'none'
}
-30
View File
@@ -1,30 +0,0 @@
import type { PostView } from '$lib/api/types'
type FilterAction = 'none' | 'minimize' | 'hide'
type FilterType = 'keyword'
// since we can test regexes on URLs, why not unify them?
export interface Filter {
match: string
action: FilterAction
type: FilterType
}
export type FilteredItem = { id: number; action: FilterAction }
export function filterPost(post: PostView, filters: Filter[]): FilterAction {
for (const filter of filters) {
try {
const regex = new RegExp(filter.match, 'i')
if (
regex.test(post.post.url ?? '') ||
regex.test(post.post.name) ||
regex.test(post.post.body ?? '')
)
return filter.action
} catch {
/* */
}
}
return 'none'
}