@grafana/ui
Version:
Grafana Components Library
1 lines • 3.03 kB
Source Map (JSON)
{"version":3,"file":"searchFunctions.mjs","sources":["../../../src/utils/searchFunctions.ts"],"sourcesContent":["import { CompletionItem, SearchFunction } from '../types/completion';\n\nimport { fuzzyMatch } from './fuzzy';\n\n/**\n * List of auto-complete search function used by SuggestionsPlugin.handleTypeahead()\n * @alpha\n */\nexport enum SearchFunctionType {\n Word = 'Word',\n Prefix = 'Prefix',\n Fuzzy = 'Fuzzy',\n}\n\n/**\n * Exact-word matching for auto-complete suggestions.\n * - Returns items containing the searched text.\n * @internal\n */\nconst wordSearch: SearchFunction = (items: CompletionItem[], text: string): CompletionItem[] => {\n return items.filter((c) => (c.filterText || c.label).includes(text));\n};\n\n/**\n * Prefix-based search for auto-complete suggestions.\n * - Returns items starting with the searched text.\n * @internal\n */\nconst prefixSearch: SearchFunction = (items: CompletionItem[], text: string): CompletionItem[] => {\n return items.filter((c) => (c.filterText || c.label).startsWith(text));\n};\n\n/**\n * Fuzzy search for auto-complete suggestions.\n * - Returns items containing all letters from the search text occurring in the same order.\n * - Stores highlight parts with parts of the text phrase found by fuzzy search\n * @internal\n */\nconst fuzzySearch: SearchFunction = (items: CompletionItem[], text: string): CompletionItem[] => {\n text = text.toLowerCase();\n return items.filter((item) => {\n const { distance, ranges, found } = fuzzyMatch(item.label.toLowerCase(), text);\n if (!found) {\n return false;\n }\n item.sortValue = distance;\n item.highlightParts = ranges;\n return true;\n });\n};\n\n/**\n * @internal\n */\nexport const SearchFunctionMap = {\n [SearchFunctionType.Word]: wordSearch,\n [SearchFunctionType.Prefix]: prefixSearch,\n [SearchFunctionType.Fuzzy]: fuzzySearch,\n};\n"],"names":["SearchFunctionType"],"mappings":";;;AAQO,IAAK,kBAAA,qBAAAA,mBAAAA,KAAL;AACL,EAAAA,oBAAA,MAAA,CAAA,GAAO,MAAA;AACP,EAAAA,oBAAA,QAAA,CAAA,GAAS,QAAA;AACT,EAAAA,oBAAA,OAAA,CAAA,GAA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