mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +03:00
Improved iso 3166 searcher page performance (#79)
* Improved performance on the Unicode Search page: - Created new flexSearch component (Faster than fuzzySearch). For more information, see https://github.com/nextapps-de/flexsearch - Replaced fuzzySearch with the flexSearch component on the unicode search page - Fixed a bug where the search limit counter didn't have any effect - Reduced the default search limit from 100 to 40 to reduce lag from loading all the ui elements * Added clarification to comment * components.d.ts update * - Replaced FuzzySearch with FlexSearch on the ISO 3166 Searcher page - Reduced the debounce from 500ms to 250ms - Reduced the default search limit from 100 to 20, and fixed a bug where the search limit wasn't applied when the value changed - Added the `readonly` prop to the `input-copyable` field - Added the `readonly` flag to all input copyable fields on the unicode search and iso 3166 searcher pages * Fixed incorrect case usage for functions and variables
This commit is contained in:
1 parent
8decf33ec8
commit
226081db3a
4 files changed
+213
-103
No files matched your search
@@ -7,6 +7,7 @@ const props = defineProps<{
|
||||
multiline?: boolean
|
||||
rows?: number | string
|
||||
autosize?: boolean
|
||||
readonly?: boolean
|
||||
}>();
|
||||
const emit = defineEmits(['update:value']);
|
||||
|
||||
@@ -21,6 +22,7 @@ const tooltipText = computed(() => isJustCopied.value ? 'Copied!' : 'Copy to cli
|
||||
:multiline="multiline"
|
||||
:rows="rows"
|
||||
:autosize="autosize"
|
||||
:readonly="readonly"
|
||||
>
|
||||
<template #suffix>
|
||||
<c-tooltip :tooltip="tooltipText">
|
||||
|
||||
@@ -2,6 +2,9 @@ import { type MaybeRef, get } from '@vueuse/core';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import FlexSearch from 'flexsearch';
|
||||
|
||||
// Define key types to match Fuse.js format
|
||||
type SearchKey = string | { name: string; weight?: number };
|
||||
|
||||
export function useFlexSearch<Data extends Record<string, any>>({
|
||||
search,
|
||||
data,
|
||||
@@ -13,7 +16,7 @@ export function useFlexSearch<Data extends Record<string, any>>({
|
||||
search: MaybeRef<string>
|
||||
data: Data[]
|
||||
options?: {
|
||||
keys: string[]
|
||||
keys: SearchKey[]
|
||||
filterEmpty?: boolean
|
||||
tokenize?: 'strict' | 'forward' | 'reverse' | 'full'
|
||||
resolution?: number
|
||||
@@ -23,96 +26,115 @@ export function useFlexSearch<Data extends Record<string, any>>({
|
||||
limit?: MaybeRef<number>
|
||||
}) {
|
||||
// Extract options
|
||||
const { keys, filterEmpty = true, ...indexOptions } = options;
|
||||
const { keys, filterEmpty = true, tokenize = 'forward', ...indexOptions } = options;
|
||||
|
||||
// Create a map to store original data items
|
||||
const data_map = ref(new Map<number, Data>());
|
||||
|
||||
// Create separate indices for each key
|
||||
const indices = keys.map((key) => {
|
||||
return {
|
||||
key,
|
||||
index: new FlexSearch.Index({
|
||||
tokenize: 'forward',
|
||||
preset: 'performance',
|
||||
resolution: 9,
|
||||
optimize: true,
|
||||
cache: 100,
|
||||
fastupdate: true,
|
||||
...indexOptions,
|
||||
}),
|
||||
};
|
||||
// Normalize keys to include weights
|
||||
const normalizedKeys = keys.map((key) => {
|
||||
if (typeof key === 'string') {
|
||||
return { name: key, weight: 1 };
|
||||
}
|
||||
return { name: key.name, weight: key.weight ?? 1 };
|
||||
});
|
||||
|
||||
// Helper to get unique key for each item (prefer 'id', fallback to index)
|
||||
const getItemKey = (item: Data, idx: number) => (item.id !== undefined ? item.id : idx);
|
||||
|
||||
// Map to store original data items by unique key
|
||||
const dataMap = ref(new Map<any, Data>());
|
||||
|
||||
// Create separate indices for each key with weight info
|
||||
const indices = normalizedKeys.map(({ name, weight }) => ({
|
||||
key: name,
|
||||
weight,
|
||||
index: new FlexSearch.Index({
|
||||
tokenize,
|
||||
preset: 'performance',
|
||||
resolution: 9,
|
||||
optimize: true,
|
||||
cache: 100,
|
||||
fastupdate: true,
|
||||
...indexOptions,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Initialize indices with data
|
||||
const initialize_indices = () => {
|
||||
data_map.value.clear();
|
||||
const initializeIndices = () => {
|
||||
dataMap.value.clear();
|
||||
indices.forEach(({ index }) => index.clear());
|
||||
|
||||
data.forEach((item, idx) => {
|
||||
// Store original item
|
||||
data_map.value.set(idx, item);
|
||||
const itemKey = getItemKey(item, idx);
|
||||
dataMap.value.set(itemKey, item);
|
||||
|
||||
// Add to each field index
|
||||
indices.forEach(({ key, index }) => {
|
||||
// Get the value for this key, potentially from nested paths
|
||||
// Support nested keys (e.g., 'foo.bar')
|
||||
const value = key.split('.').reduce((obj, path) => obj?.[path], item);
|
||||
if (value) {
|
||||
index.add(idx, String(value));
|
||||
index.add(itemKey, String(value));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Initialize on creation
|
||||
initialize_indices();
|
||||
initializeIndices();
|
||||
|
||||
// Function to search across all indices
|
||||
const search_all_indices = (query: string, search_limit: number) => {
|
||||
// Watch for data changes (deep watch for array content)
|
||||
watch(
|
||||
() => data,
|
||||
initializeIndices,
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
// Function to search across all indices with weight consideration
|
||||
const searchAllIndices = (query: string, searchLimit: number) => {
|
||||
if (!query) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Use a higher individual limit for each index to ensure we don't miss potential matches
|
||||
const individual_limit = search_limit > 0 ? search_limit * 2 : undefined;
|
||||
const individualLimit = searchLimit > 0 ? searchLimit * 2 : undefined;
|
||||
|
||||
// Search each index
|
||||
const result_sets = indices.map(({ index }) =>
|
||||
index.search(query, individual_limit ? { limit: individual_limit } : undefined),
|
||||
);
|
||||
// Search each index and collect results with weights
|
||||
const weightedResults = new Map<any, number>();
|
||||
|
||||
// Combine and deduplicate results
|
||||
const unique_ids = new Set<number>();
|
||||
result_sets.forEach((results) => {
|
||||
results.forEach(id => unique_ids.add(id as number));
|
||||
indices.forEach(({ index, weight }) => {
|
||||
const results = index.search(query, individualLimit ? { limit: individualLimit } : undefined);
|
||||
|
||||
results.forEach((id) => {
|
||||
const currentScore = weightedResults.get(id) || 0;
|
||||
weightedResults.set(id, currentScore + weight);
|
||||
});
|
||||
});
|
||||
|
||||
// Convert back to array of original items
|
||||
const result_array = Array.from(unique_ids)
|
||||
.map(id => data_map.value.get(id))
|
||||
// Sort by weight (higher weight = better match) and convert to array
|
||||
const sortedIds = Array.from(weightedResults.entries())
|
||||
.sort((a, b) => b[1] - a[1])
|
||||
.map(([id]) => id);
|
||||
|
||||
// Apply limit and convert back to original items
|
||||
const limitedIds = searchLimit > 0 ? sortedIds.slice(0, searchLimit) : sortedIds;
|
||||
|
||||
return limitedIds
|
||||
.map(id => dataMap.value.get(id))
|
||||
.filter(Boolean) as Data[];
|
||||
|
||||
// Apply the limit to the final combined results
|
||||
return search_limit > 0 ? result_array.slice(0, search_limit) : result_array;
|
||||
};
|
||||
|
||||
// Watch for data changes to rebuild indices
|
||||
watch(() => data.length, initialize_indices);
|
||||
|
||||
// Computed results
|
||||
const searchResult = computed<Data[]>(() => {
|
||||
const query = get(search);
|
||||
const search_limit: number = get(limit);
|
||||
const searchLimit: number = get(limit);
|
||||
|
||||
// Handle empty query case
|
||||
if (!query && !filterEmpty) {
|
||||
return search_limit > 0 ? data.slice(0, search_limit) : data;
|
||||
return searchLimit > 0 ? data.slice(0, searchLimit) : data;
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return search_all_indices(query, search_limit);
|
||||
return searchAllIndices(query, searchLimit);
|
||||
});
|
||||
|
||||
return { searchResult };
|
||||
|
||||
@@ -1,30 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import CountriesDB from 'countries-db';
|
||||
import ISO6391 from 'iso-639-1';
|
||||
import { useFuzzySearch } from '@/composable/fuzzySearch';
|
||||
import useDebouncedRef from '@/composable/debouncedref';
|
||||
import { useFlexSearch } from '@/composable/flexSearch';
|
||||
|
||||
const searchQuery = useDebouncedRef('', 500);
|
||||
const countriesSearchData = Object.entries(CountriesDB.getAllCountries()).map(([_, info]) => {
|
||||
return {
|
||||
iso2: info.iso2,
|
||||
iso3: info.iso3,
|
||||
domain: info.domain,
|
||||
name: `${info.name} ${info.officialName}`,
|
||||
info,
|
||||
};
|
||||
});
|
||||
const limit = ref(100);
|
||||
const { searchResult } = useFuzzySearch({
|
||||
const searchQuery = useDebouncedRef('', 250);
|
||||
const countriesSearchData = Object.values(CountriesDB.getAllCountries());
|
||||
const limit = ref(20);
|
||||
const { searchResult } = useFlexSearch({
|
||||
search: searchQuery,
|
||||
data: countriesSearchData,
|
||||
options: {
|
||||
keys: ['name', { name: 'iso3', weight: 3 }, { name: 'iso2', weight: 2 }, 'domain'],
|
||||
threshold: 0.3,
|
||||
isCaseSensitive: false,
|
||||
useExtendedSearch: true,
|
||||
},
|
||||
limit: limit.value,
|
||||
limit,
|
||||
});
|
||||
|
||||
function langToName(code: string) {
|
||||
@@ -39,10 +28,7 @@ function langToName(code: string) {
|
||||
<template>
|
||||
<div mx-auto max-w-2400px important:flex-1>
|
||||
<div mx-auto max-w-600px flex justify-center gap-3>
|
||||
<c-input-text
|
||||
v-model:value="searchQuery"
|
||||
placeholder="Search Countries by name, iso2, iso3..."
|
||||
>
|
||||
<c-input-text v-model:value="searchQuery" placeholder="Search Countries by name, iso2, iso3...">
|
||||
<template #prefix>
|
||||
<icon-mdi-search mr-6px color-black op-70 dark:color-white />
|
||||
</template>
|
||||
@@ -53,12 +39,7 @@ function langToName(code: string) {
|
||||
</div>
|
||||
|
||||
<div v-if="searchQuery.trim().length > 0">
|
||||
<div
|
||||
v-if="searchResult.length === 0"
|
||||
mt-4
|
||||
text-20px
|
||||
font-bold
|
||||
>
|
||||
<div v-if="searchResult.length === 0" mt-4 text-20px font-bold>
|
||||
No results
|
||||
</div>
|
||||
|
||||
@@ -75,27 +56,132 @@ function langToName(code: string) {
|
||||
<tbody>
|
||||
<tr v-for="(result, ix) in searchResult" :key="ix">
|
||||
<td style="vertical-align: top">
|
||||
<input-copyable :value="result.iso2" />
|
||||
<input-copyable :value="result.iso3" />
|
||||
<input-copyable :value="result.iso2" :readonly="true" />
|
||||
<input-copyable :value="result.iso3" :readonly="true" />
|
||||
</td>
|
||||
<td>
|
||||
<input-copyable label-width="150px" label="Name" label-position="left" :value="result.name" mb-1 />
|
||||
<input-copyable label-width="150px" label="Official Name" label-position="left" :value="result.info.officialName" mb-1 />
|
||||
<input-copyable label-width="150px" label="Domain" label-position="left" :value="result.info.domain" mb-1 />
|
||||
<input-copyable label-width="150px" label="Emoji" label-position="left" :value="`${result.info.emoji} (${result.info.emojiUnicode})`" mb-1 />
|
||||
<input-copyable label-width="150px" label="ISO Num" label-position="left" :value="result.info.isoNumeric" mb-1 />
|
||||
<input-copyable label-width="150px" label="Continent" label-position="left" :value="result.info.continentId" mb-1 />
|
||||
<input-copyable label-width="150px" label="Elevation (m)" label-position="left" :value="result.info.elevation" mb-1 />
|
||||
<input-copyable label-width="150px" label="Population" label-position="left" :value="result.info.population" mb-1 />
|
||||
<input-copyable label-width="150px" label="Area (km²)" label-position="left" :value="result.info.areaSqKm" mb-1 />
|
||||
<input-copyable label-width="150px" label="Timezones" label-position="left" :value="result.info.timezones.join('\n')" mb-1 />
|
||||
<input-copyable label-width="150px" label="Currency" label-position="left" :value="`${result.info.currencyCode} / ${result.info.currencyName}`" mb-1 />
|
||||
<input-copyable label-width="150px" label="Postal Code" label-position="left" :value="`${result.info.postalCodeFormat} / ${result.info.postalCodeRegex}`" mb-1 />
|
||||
<input-copyable label-width="150px" label="Phone Code" label-position="left" :value="result.info.phoneCode" mb-1 />
|
||||
<input-copyable label-width="150px" label="Neighbor Countries" label-position="left" :value="result.info.neighborCountryIds.map(id => CountriesDB.getCountry(id, 'name')?.toString() || id).join(', ')" mb-1 />
|
||||
<input-copyable label-width="150px" label="Languages" label-position="left" :value="result.info.languages.map(langToName).join(', ')" mb-1 />
|
||||
<input-copyable label-width="150px" label="Locales" label-position="left" :value="result.info.locales.map(langToName).join(', ')" mb-1 />
|
||||
<!-- //NOSONAR --><n-a :href="`https://www.openstreetmap.org/#map=5/${result.info.coordinates.latitude}/${result.info.coordinates.longitude}`" target="_blank">
|
||||
<input-copyable label-width="150px" label="Name" label-position="left" :value="result.name" :readonly="true" mb-1 />
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Official Name"
|
||||
label-position="left"
|
||||
:value="result.officialName"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable label-width="150px" label="Domain" label-position="left" :value="result.domain" :readonly="true" mb-1 />
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Emoji"
|
||||
label-position="left"
|
||||
:value="`${result.emoji} (${result.emojiUnicode})`"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="ISO Num"
|
||||
label-position="left"
|
||||
:value="result.isoNumeric"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Continent"
|
||||
label-position="left"
|
||||
:value="result.continentId"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Elevation (m)"
|
||||
label-position="left"
|
||||
:value="result.elevation"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Population"
|
||||
label-position="left"
|
||||
:value="result.population"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Area (km²)"
|
||||
label-position="left"
|
||||
:value="result.areaSqKm"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Timezones"
|
||||
label-position="left"
|
||||
:value="result.timezones.join('\n')"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Currency"
|
||||
label-position="left"
|
||||
:value="`${result.currencyCode} / ${result.currencyName}`"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Postal Code"
|
||||
label-position="left"
|
||||
:value="`${result.postalCodeFormat} / ${result.postalCodeRegex}`"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Phone Code"
|
||||
label-position="left"
|
||||
:value="result.phoneCode"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Neighbor Countries"
|
||||
label-position="left"
|
||||
:value="
|
||||
result.neighborCountryIds
|
||||
.map((id) => CountriesDB.getCountry(id, 'name')?.toString() || id)
|
||||
.join(', ')
|
||||
"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Languages"
|
||||
label-position="left"
|
||||
:value="result.languages.map(langToName).join(', ')"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<input-copyable
|
||||
label-width="150px"
|
||||
label="Locales"
|
||||
label-position="left"
|
||||
:value="result.locales.map(langToName).join(', ')"
|
||||
:readonly="true"
|
||||
mb-1
|
||||
/>
|
||||
<!-- //NOSONAR --><n-a
|
||||
:href="`https://www.openstreetmap.org/#map=5/${result.coordinates.latitude}/${result.coordinates.longitude}`"
|
||||
target="_blank"
|
||||
>
|
||||
> See on OpenStreetMap
|
||||
</n-a>
|
||||
</td>
|
||||
|
||||
@@ -97,7 +97,7 @@ const { searchResult } = useFlexSearch({
|
||||
<tbody>
|
||||
<tr v-for="(result, ix) in searchResult" :key="ix">
|
||||
<td>
|
||||
<input-copyable :value="`U+${toPaddedHex(result.codePoint)}`" mb-1 />
|
||||
<input-copyable :value="`U+${toPaddedHex(result.codePoint)}`" :readonly="true" mb-1 />
|
||||
<!-- //NOSONAR --><n-a
|
||||
:href="`https://unicodeplus.com/U+${toPaddedHex(result.codePoint)}`"
|
||||
target="_blank"
|
||||
@@ -106,17 +106,17 @@ const { searchResult } = useFlexSearch({
|
||||
</n-a>
|
||||
</td>
|
||||
<td>
|
||||
<input-copyable :value="String.fromCodePoint(result.codePoint)" mb-1 />
|
||||
<input-copyable :value="toUTF8(result.codePoint)" />
|
||||
<input-copyable :value="String.fromCodePoint(result.codePoint)" :readonly="true" mb-1 />
|
||||
<input-copyable :value="toUTF8(result.codePoint)" :readonly="true" />
|
||||
</td>
|
||||
<td>
|
||||
<input-copyable :value="unicodeCategories.get(result.codePoint)" />
|
||||
<input-copyable :value="unicodeCategories.get(result.codePoint)" :readonly="true" />
|
||||
</td>
|
||||
<td>
|
||||
<input-copyable :value="`\&\#x${toPaddedHex(result.codePoint)};`" mb-1 />
|
||||
<input-copyable :value="`\&\#${result.codePoint};`" />
|
||||
<input-copyable :value="`\&\#x${toPaddedHex(result.codePoint)};`" :readonly="true" mb-1 />
|
||||
<input-copyable :value="`\&\#${result.codePoint};`" :readonly="true" />
|
||||
</td>
|
||||
<td><input-copyable :value="result.characterName" /></td>
|
||||
<td><input-copyable :value="result.characterName" :readonly="true" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</n-table>
|
||||
|
||||
Reference in new issue
Block a user