From 02df1bc9f246e2d466b833c389d341cefc737868 Mon Sep 17 00:00:00 2001 From: Dark_Phoenix_ <92178883+0DarkPhoenix@users.noreply.github.com> Date: Sun, 1 Jun 2025 14:07:08 +0200 Subject: [PATCH] Improve IES Lighting guidelines performance (#83) * 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 * - Replaced fuzzysearch with flexsearch on the IES Lighting Guidelines page - Reduced the debounce from 500ms to 250ms - Changed the search keys so all columns are now searchable * - Added logic to sort the limited results using a similarity score created by calculating the Levenshtein distance - Added the `shouldSort` boolean to control whether the results should be filtered by similarity. `true` by default * Merged new changes to flexSearch * - Added readonly flag to input copyable fields --- components.d.ts | 26 +++--- src/composable/flexSearch.ts | 90 ++++++++++++++++++- .../ies-lighting-guidelines.vue | 24 +++-- 3 files changed, 106 insertions(+), 34 deletions(-) diff --git a/components.d.ts b/components.d.ts index cc3e704c..b1530c30 100644 --- a/components.d.ts +++ b/components.d.ts @@ -12,6 +12,7 @@ declare module '@vue/runtime-core' { '404.page': typeof import('./src/pages/404.page.vue')['default'] About: typeof import('./src/pages/About.vue')['default'] AdLdapSearcher: typeof import('./src/tools/ad-ldap-searcher/ad-ldap-searcher.vue')['default'] + AgeCrypto: typeof import('./src/tools/age-crypto/age-crypto.vue')['default'] AiPromptSplitter: typeof import('./src/tools/ai-prompt-splitter/ai-prompt-splitter.vue')['default'] AlarmSoundToggle: typeof import('./src/tools/pomodoro-timer/app/components/AlarmSoundToggle.vue')['default'] AngleConverter: typeof import('./src/tools/angle-converter/angle-converter.vue')['default'] @@ -28,6 +29,8 @@ declare module '@vue/runtime-core' { Base64FileConverter: typeof import('./src/tools/base64-file-converter/base64-file-converter.vue')['default'] Base64HexConverter: typeof import('./src/tools/base64-hex-converter/base64-hex-converter.vue')['default'] Base64StringConverter: typeof import('./src/tools/base64-string-converter/base64-string-converter.vue')['default'] + BashMemo: typeof import('./src/tools/bash-memo/bash-memo.vue')['default'] + 'BashMemo.content': typeof import('./src/tools/bash-memo/bash-memo.content.md')['default'] BasicAuthGenerator: typeof import('./src/tools/basic-auth-generator/basic-auth-generator.vue')['default'] Bcrypt: typeof import('./src/tools/bcrypt/bcrypt.vue')['default'] BenchmarkBuilder: typeof import('./src/tools/benchmark-builder/benchmark-builder.vue')['default'] @@ -259,6 +262,8 @@ declare module '@vue/runtime-core' { JsUnobfuscator: typeof import('./src/tools/js-unobfuscator/js-unobfuscator.vue')['default'] JwtGenerator: typeof import('./src/tools/jwt-generator/jwt-generator.vue')['default'] JwtParser: typeof import('./src/tools/jwt-parser/jwt-parser.vue')['default'] + K8sMemo: typeof import('./src/tools/k8s-memo/k8s-memo.vue')['default'] + 'K8sMemo.content': typeof import('./src/tools/k8s-memo/k8s-memo.content.md')['default'] KeycodeInfo: typeof import('./src/tools/keycode-info/keycode-info.vue')['default'] LengthConverter: typeof import('./src/tools/length-converter/length-converter.vue')['default'] ListComparer: typeof import('./src/tools/list-comparer/list-comparer.vue')['default'] @@ -294,40 +299,26 @@ declare module '@vue/runtime-core' { MultiLinkDownloader: typeof import('./src/tools/multi-link-downloader/multi-link-downloader.vue')['default'] MyIp: typeof import('./src/tools/my-ip/my-ip.vue')['default'] NA: typeof import('naive-ui')['NA'] - NAlert: typeof import('naive-ui')['NAlert'] NanoidGenerator: typeof import('./src/tools/nanoid-generator/nanoid-generator.vue')['default'] NanoMemo: typeof import('./src/tools/nano-memo/nano-memo.vue')['default'] 'NanoMemo.content': typeof import('./src/tools/nano-memo/nano-memo.content.md')['default'] NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default'] - NButton: typeof import('naive-ui')['NButton'] - NCard: typeof import('naive-ui')['NCard'] - NCheckbox: typeof import('naive-ui')['NCheckbox'] - NCheckboxGroup: typeof import('naive-ui')['NCheckboxGroup'] - NCode: typeof import('naive-ui')['NCode'] NCollapseTransition: typeof import('naive-ui')['NCollapseTransition'] NConfigProvider: typeof import('naive-ui')['NConfigProvider'] - NDataTable: typeof import('naive-ui')['NDataTable'] - NDivider: typeof import('naive-ui')['NDivider'] NEllipsis: typeof import('naive-ui')['NEllipsis'] NFormItem: typeof import('naive-ui')['NFormItem'] NginxFormatter: typeof import('./src/tools/nginx-formatter/nginx-formatter.vue')['default'] + NginxMemo: typeof import('./src/tools/nginx-memo/nginx-memo.vue')['default'] + 'NginxMemo.content': typeof import('./src/tools/nginx-memo/nginx-memo.content.md')['default'] NH1: typeof import('naive-ui')['NH1'] NH3: typeof import('naive-ui')['NH3'] NIcon: typeof import('naive-ui')['NIcon'] - NInput: typeof import('naive-ui')['NInput'] NInputNumber: typeof import('naive-ui')['NInputNumber'] NLayout: typeof import('naive-ui')['NLayout'] NLayoutSider: typeof import('naive-ui')['NLayoutSider'] NMenu: typeof import('naive-ui')['NMenu'] - NModal: typeof import('naive-ui')['NModal'] NP: typeof import('naive-ui')['NP'] - NRadio: typeof import('naive-ui')['NRadio'] - NRadioGroup: typeof import('naive-ui')['NRadioGroup'] - NScrollbar: typeof import('naive-ui')['NScrollbar'] - NSpace: typeof import('naive-ui')['NSpace'] NTable: typeof import('naive-ui')['NTable'] - NTag: typeof import('naive-ui')['NTag'] - NTimePicker: typeof import('naive-ui')['NTimePicker'] NumberInput: typeof import('./src/tools/pomodoro-timer/app/components/NumberInput.vue')['default'] NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default'] OcrImage: typeof import('./src/tools/ocr-image/ocr-image.vue')['default'] @@ -346,6 +337,7 @@ declare module '@vue/runtime-core' { PdfLinearize: typeof import('./src/tools/pdf-linearize/pdf-linearize.vue')['default'] PdfSignatureChecker: typeof import('./src/tools/pdf-signature-checker/pdf-signature-checker.vue')['default'] PdfSignatureDetails: typeof import('./src/tools/pdf-signature-checker/components/pdf-signature-details.vue')['default'] + PdfTextExtractor: typeof import('./src/tools/pdf-text-extractor/pdf-text-extractor.vue')['default'] PdfUnlock: typeof import('./src/tools/pdf-unlock/pdf-unlock.vue')['default'] PercentageCalculator: typeof import('./src/tools/percentage-calculator/percentage-calculator.vue')['default'] PgpEncryption: typeof import('./src/tools/pgp-encryption/pgp-encryption.vue')['default'] @@ -443,6 +435,8 @@ declare module '@vue/runtime-core' { UuidConverter: typeof import('./src/tools/uuid-converter/uuid-converter.vue')['default'] UuidGenerator: typeof import('./src/tools/uuid-generator/uuid-generator.vue')['default'] VatValidator: typeof import('./src/tools/vat-validator/vat-validator.vue')['default'] + VimMemo: typeof import('./src/tools/vim-memo/vim-memo.vue')['default'] + 'VimMemo.content': typeof import('./src/tools/vim-memo/vim-memo.content.md')['default'] VolumeConverter: typeof import('./src/tools/volume-converter/volume-converter.vue')['default'] WebsocketTester: typeof import('./src/tools/websocket-tester/websocket-tester.vue')['default'] WeekNumberConverter: typeof import('./src/tools/week-number-converter/week-number-converter.vue')['default'] diff --git a/src/composable/flexSearch.ts b/src/composable/flexSearch.ts index bb504b01..6733e9a5 100644 --- a/src/composable/flexSearch.ts +++ b/src/composable/flexSearch.ts @@ -17,7 +17,23 @@ export function useFlexSearch>({ data: Data[] options?: { keys: SearchKey[] + /** + * Whether to show or not show items with an empty search query. `true` won't show any items, `false` will show all items. + * @default true + */ filterEmpty?: boolean + /** + * Whether the results should be sorted by similarity score using the Levenshtein distance. + * @default true + */ + shouldSort?: boolean + /** + * Type of tokenization logic to use (Determines how the search logic will behave). + * @default 'forward' + * @see {@link https://github.com/nextapps-de/flexsearch?tab=readme-ov-file#tokenizer-partial-match | FlexSearch Tokenizer Documentation} + * + * Note: The `'tolerant'` tokenizer option mentioned in the documentation doesn't exist anymore in the current version of FlexSearch. + */ tokenize?: 'strict' | 'forward' | 'reverse' | 'full' resolution?: number optimize?: boolean @@ -26,7 +42,7 @@ export function useFlexSearch>({ limit?: MaybeRef }) { // Extract options - const { keys, filterEmpty = true, tokenize = 'forward', ...indexOptions } = options; + const { keys, filterEmpty = true, shouldSort = true, tokenize = 'forward', ...indexOptions } = options; // Normalize keys to include weights const normalizedKeys = keys.map((key) => { @@ -115,9 +131,75 @@ export function useFlexSearch>({ // 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[]; + // Return the limited results early if it shouldn't be sorted by similarity + if (!shouldSort) { + return limitedIds + .map(id => dataMap.value.get(id)) + .filter(Boolean) as Data[]; + } + + // Calculate Levenshtein distance + const levenshteinDistance = (str1: string, str2: string): number => { + const matrix = Array(str2.length + 1).fill(null).map(() => Array(str1.length + 1).fill(null)); + + for (let i = 0; i <= str1.length; i++) { + matrix[0][i] = i; + } + for (let j = 0; j <= str2.length; j++) { + matrix[j][0] = j; + } + + for (let j = 1; j <= str2.length; j++) { + for (let i = 1; i <= str1.length; i++) { + const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1; + matrix[j][i] = Math.min( + matrix[j][i - 1] + 1, // deletion + matrix[j - 1][i] + 1, // insertion + matrix[j - 1][i - 1] + indicator, // substitution + ); + } + } + + return matrix[str2.length][str1.length]; + }; + + // Calculate similarity score (0-1, where 1 is perfect match) + const calculateSimilarity = (str1: string, str2: string): number => { + const maxLength = Math.max(str1.length, str2.length); + if (maxLength === 0) { + return 1; + } + + const distance = levenshteinDistance(str1.toLowerCase(), str2.toLowerCase()); + return 1 - (distance / maxLength); + }; + + // Sort limited results by similarity score + const sortedResults = limitedIds + .map((id) => { + const item = dataMap.value.get(id); + if (!item) { + return null; + } + + // Calculate best similarity score across all searchable fields + const similarities = normalizedKeys.map(({ name }) => { + const value = name.split('.').reduce((obj, path) => obj?.[path], item); + return value ? calculateSimilarity(query, String(value)) : 0; + }); + + const bestSimilarity = Math.max(...similarities); + + return { + item, + similarity: bestSimilarity, + }; + }) + .filter(Boolean) + .sort((a, b) => b!.similarity - a!.similarity) + .map(result => result!.item); + + return sortedResults as Data[]; }; // Computed results diff --git a/src/tools/ies-lighting-guidelines/ies-lighting-guidelines.vue b/src/tools/ies-lighting-guidelines/ies-lighting-guidelines.vue index 1650a115..a3115417 100644 --- a/src/tools/ies-lighting-guidelines/ies-lighting-guidelines.vue +++ b/src/tools/ies-lighting-guidelines/ies-lighting-guidelines.vue @@ -1,20 +1,16 @@