keySearch<T> static method
- required KeyCharSearches<
T> keyCharSearches, - required Iterable<
T> items, - required dynamic searchTerms,
- int maxResults = 0,
Searches for items in the provided items
iterable based on the given searchTerms
.
The keyCharSearches
parameter is a map where the keys are strings representing search prefixes,
and the values are functions that take a search term and an item, and return a boolean indicating
whether the item matches the search term.
The items
parameter is the iterable of items to search through.
The searchTerms
parameter is the term or terms to search for. It can be a single term or a list of terms.
The maxResults
parameter specifies the maximum number of results to return. If it is 0, all matching items are returned.
Returns an iterable of items that match the search criteria.
Implementation
static Iterable<T> keySearch<T>({
required KeyCharSearches<T> keyCharSearches,
required Iterable<T> items,
required dynamic searchTerms,
int maxResults = 0,
}) {
var searches = forceRecursiveList(searchTerms);
keyCharSearches[""] ??= (String search, T item) => false;
var l = [
...items.where((item) {
for (var s in searches) {
var searchWord = changeTo<string>(s);
if (searchWord.startsWithAny(keyCharSearches.keys.whereNot((e) => e.isEmpty))) {
for (var entry in keyCharSearches.entries.where((e) => e.key.isNotEmpty)) {
if (searchWord.startsWith(entry.key)) {
return entry.value(
searchWord.removeFirstEqual(entry.key),
item,
);
}
}
} else {
return keyCharSearches[""]!(searchWord, item);
}
}
return false;
})
];
if (maxResults > 0) return l.take(maxResults);
return l;
}