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:
6 files changed
+51
-38
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { env } from '$env/dynamic/public'
|
import { env } from '$env/dynamic/public'
|
||||||
import type { CommentSortType, ListingType, SortType } from '$lib/api/types'
|
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'
|
import { locale } from './i18n'
|
||||||
|
|
||||||
// TODO extract settings into submodules
|
// TODO extract settings into submodules
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
import type { Snippet } from 'svelte'
|
import type { Snippet } from 'svelte'
|
||||||
import { ArchiveBox, ArrowsPointingOut, Plus } from 'svelte-hero-icons/dist'
|
import { ArchiveBox, ArrowsPointingOut, Plus } from 'svelte-hero-icons/dist'
|
||||||
import { Post } from '..'
|
import { Post } from '..'
|
||||||
import { filterPost, type FilteredItem } from '../filters'
|
import { filterPost, type FilteredItem } from '../filters.svelte'
|
||||||
import { t } from '$lib/app/i18n'
|
import { t } from '$lib/app/i18n'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
let filteredPosts: FilteredItem[] = $derived(
|
let filteredPosts: FilteredItem[] = $derived(
|
||||||
posts.map((post) => ({
|
posts.map((post) => ({
|
||||||
id: post.post.id,
|
id: post.post.id,
|
||||||
action: filterPost(post, settings.filters),
|
action: filterPost(post),
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
import { SvelteSet } from 'svelte/reactivity'
|
import { SvelteSet } from 'svelte/reactivity'
|
||||||
import { fly } from 'svelte/transition'
|
import { fly } from 'svelte/transition'
|
||||||
import { Post } from '..'
|
import { Post } from '..'
|
||||||
import { filterPost, type FilteredItem } from '../filters'
|
import { filterPost, type FilteredItem } from '../filters.svelte'
|
||||||
import { ReactiveState } from '$lib/app/util.svelte'
|
import { ReactiveState } from '$lib/app/util.svelte'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@@ -47,7 +47,7 @@
|
|||||||
let filteredPosts: FilteredItem[] = $derived(
|
let filteredPosts: FilteredItem[] = $derived(
|
||||||
posts.map((post) => ({
|
posts.map((post) => ({
|
||||||
id: post.post.id,
|
id: post.post.id,
|
||||||
action: filterPost(post, settings.filters),
|
action: filterPost(post),
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -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'
|
||||||
|
}
|
||||||
@@ -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'
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { t } from '$lib/app/i18n'
|
import { t } from '$lib/app/i18n'
|
||||||
import type { Filter } from '$lib/feature/post/filters'
|
import type { Filter } from '$lib/feature/post/filters.svelte'
|
||||||
import Switch from '$lib/ui/form/Switch.svelte'
|
import Switch from '$lib/ui/form/Switch.svelte'
|
||||||
import { Button, TextInput } from 'mono-svelte'
|
import { Button, TextInput } from 'mono-svelte'
|
||||||
import { Trash } from 'svelte-hero-icons/dist'
|
import { Trash } from 'svelte-hero-icons/dist'
|
||||||
@@ -14,9 +14,10 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<li
|
<li
|
||||||
class="adaptive px-3 py-1 flex flex-col md:flex-row md:items-center xs gap-4"
|
class="adaptive px-3 py-1.5 flex flex-row flex-wrap items-center xs gap-x-2 gap-y-0.5"
|
||||||
>
|
>
|
||||||
<TextInput bind:value={filter.match} label="" class="flex-1"></TextInput>
|
<TextInput bind:value={filter.match} label="" class="flex-1 min-w-96"
|
||||||
|
></TextInput>
|
||||||
<Switch
|
<Switch
|
||||||
options={['hide', 'minimize', 'none']}
|
options={['hide', 'minimize', 'none']}
|
||||||
optionNames={[
|
optionNames={[
|
||||||
|
|||||||
Reference in new issue
Block a user