Skip to content

Commit

Permalink
Faster search
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Nov 11, 2023
1 parent ec0f034 commit 6dbb44c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
38 changes: 38 additions & 0 deletions docs-src/src/CodepointTextSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { Codepoint } from "./Data"

interface ICodepointStrings {
[index: number]: string[]
}

const codepointStrings: ICodepointStrings = {}

function buildCodepointUppercaseStrings(codepoint: Codepoint): string[]
{
const result: string[] = [codepoint.name.toUpperCase()];
if (codepoint.unicode1Name !== undefined) {
result.push(codepoint.unicode1Name.toUpperCase());
}
codepoint.formalAliases?.forEach((s) => result.push(s.toUpperCase()));
codepoint.informativeAliases?.forEach((s) => result.push(s.toUpperCase()));
codepoint.correctedNames?.forEach((s) => result.push(s.toUpperCase()));
codepoint.controlNames?.forEach((s) => result.push(s.toUpperCase()));
codepoint.alternateNames?.forEach((s) => result.push(s.toUpperCase()));
codepoint.figments?.forEach((s) => result.push(s.toUpperCase()));
codepoint.abbreviations?.forEach((s) => result.push(s.toUpperCase()));

return result;
}

function getCodepointUppercaseStrings(codepoint: Codepoint): string[]
{
return codepointStrings[codepoint.id] ?? (codepointStrings[codepoint.id] = buildCodepointUppercaseStrings(codepoint));
}

export default function match(codepoint: Codepoint, regexOrUppercaseWords: RegExp|string[]): boolean
{
const strings = getCodepointUppercaseStrings(codepoint);
if (regexOrUppercaseWords instanceof RegExp) {
return strings.some((string) => regexOrUppercaseWords.test(string));
}
return strings.some((string) => regexOrUppercaseWords.every((word) => string.includes(word)));
}
13 changes: 2 additions & 11 deletions docs-src/src/components/DataFilter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { Data, Codepoint } from '@/Data'
import type { PlaneFilterResult, BlockFilterResult } from '@/FilterResult'
import { onMounted, ref, watch } from 'vue'
import match from '@/CodepointTextSearch'
export interface PlaneBlockSelection {
plane: number
Expand All @@ -27,14 +28,6 @@ const emit = defineEmits<{
(e: 'change', filtered: PlaneFilterResult[]): void
}>()
function codepointSatisfyWords(codepoint: Codepoint, ucWords: string[]): boolean {
return !ucWords.some((ucWord) => {
if (!codepoint.name.toUpperCase().includes(ucWord)) {
return true
}
})
}
watch(planeBlockSelection, async () => {
updateSelectedCodepoints()
})
Expand Down Expand Up @@ -68,9 +61,7 @@ function updateSelectedCodepoints() {
if (ucWords.length === 0) {
codepoints = block.codepoints
} else {
codepoints = block.codepoints.filter((codepoint) =>
codepointSatisfyWords(codepoint, ucWords)
)
codepoints = block.codepoints.filter((codepoint) => match(codepoint, ucWords))
}
if (codepoints.length === 0) {
return
Expand Down

0 comments on commit 6dbb44c

Please sign in to comment.