Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/autosuggest/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { AutosuggestItem } from '../interfaces';
type SearchableFields = 'value' | 'label' | 'description' | 'labelTag';
type SearchableTagFields = 'tags' | 'filteringTags';

// Pre-allocated arrays to avoid creating new arrays on every matchSingleOption call
const searchableFields: SearchableFields[] = ['value', 'label', 'description', 'labelTag'];
const searchableTagFields: SearchableTagFields[] = ['tags', 'filteringTags'];

const isGroup = (option: AutosuggestItem) => 'type' in option && option.type === 'parent';

const popLastGroup = (options: AutosuggestItem[]) => {
Expand All @@ -17,11 +21,13 @@ const popLastGroup = (options: AutosuggestItem[]) => {
};

export const filterOptions = (options: AutosuggestItem[], text: string): AutosuggestItem[] => {
// Move toLowerCase outside the loop to avoid repeated calls
const searchText = text.toLowerCase();
const filteredOptions = options.reduce<AutosuggestItem[]>((filteredIn, option) => {
if (isGroup(option)) {
popLastGroup(filteredIn);
filteredIn.push(option);
} else if (matchSingleOption(option, text)) {
} else if (matchSingleOption(option, searchText)) {
filteredIn.push(option);
}
return filteredIn;
Expand All @@ -30,18 +36,13 @@ export const filterOptions = (options: AutosuggestItem[], text: string): Autosug
return filteredOptions;
};

const matchSingleOption = (option: AutosuggestItem, text: string): boolean => {
const searchableFields: SearchableFields[] = ['value', 'label', 'description', 'labelTag'];
const searchableTagFields: SearchableTagFields[] = ['tags', 'filteringTags'];

const searchText = text.toLowerCase();

const matchSingleOption = (option: AutosuggestItem, searchText: string): boolean => {
const searchStrFieldsFn = (attr: SearchableFields) => matchString(option[attr], searchText);
const searchTagsFieldsFn = (attr: SearchableTagFields) => option[attr]?.some(value => matchString(value, searchText));

return searchableFields.some(searchStrFieldsFn) || searchableTagFields.some(searchTagsFieldsFn);
};

const matchString = (value: string | undefined, searchText: string) => {
return value && value.toLowerCase().indexOf(searchText) !== -1;
return value && value.toLowerCase().includes(searchText);
};