countSearch static method
Counts the number of occurrences of search terms in a given item.
The countSearch function takes the following parameters:
searchTerms
: The search terms to count in the item. Can be a string or a list of strings.searchOn
: A function that takes an item and returns a list of dynamic values to search on.ignoreCase
: A boolean indicating whether to ignore case sensitivity (default is true).ignoreDiacritics
: A boolean indicating whether to ignore diacritics (default is true).ignoreWordSplitters
: A boolean indicating whether to ignore word splitters (default is true).splitCamelCase
: A boolean indicating whether to split camel case words (default is true).useWildcards
: A boolean indicating whether to use wildcards (*) for matching (true) or a standardstring.contains()
(false) (default is false).
The function returns the count of occurrences of the search terms in the item.
Implementation
static int countSearch({
required dynamic searchTerms,
required Iterable searchOnItems,
bool ignoreCase = true,
bool ignoreDiacritics = true,
bool ignoreWordSplitters = true,
bool splitCamelCase = true,
bool useWildcards = false,
}) {
var terms = <dynamic>[...searchOnItems];
var searches = forceRecursiveList(searchTerms);
return [
for (var searchTerm in searches)
...terms.where((v) {
if (v == null) return false;
var searchword = generateKeyword(
searchTerm,
forceLowerCase: ignoreCase,
removeDiacritics: ignoreDiacritics,
removeWordSplitters: ignoreWordSplitters,
splitCamelCase: splitCamelCase,
);
dynamic keyword = v;
if (keyword is num) {
if (useWildcards) {
keyword = keyword.toString();
} else {
return keyword.asFlat.startsWith(searchword);
}
}
keyword = generateKeyword(
keyword,
forceLowerCase: ignoreCase,
removeDiacritics: ignoreDiacritics,
removeWordSplitters: ignoreWordSplitters,
splitCamelCase: splitCamelCase,
);
consoleLog("SearchWord: $searchword, Keyword: $keyword");
return useWildcards ? keyword.toString().isLike(searchword, !ignoreCase) : keyword.toString().contains(searchword);
})
].length;
}