search method

List<Result<T>> search(
  1. String pattern, [
  2. int limit = -1
])

Search for a given pattern on the list, optionally limiting the result length

Implementation

List<Result<T>> search(String pattern, [int limit = -1]) {
  if (list.isEmpty) return <Result<T>>[];

  // Return original list as [List<Result>] if pattern is empty
  if (pattern == '') {
    return list
        .map((item) => Result<T>(
              item: item,
              matches: const [],
              score: 0,
            ))
        .toList();
  }

  final searchers = _prepareSearchers(pattern);

  final resultsAndWeights =
      _search(searchers.tokenSearchers, searchers.fullSearcher);

  _computeScore(resultsAndWeights.weights, resultsAndWeights.results);

  if (options.shouldSort) {
    _sort(resultsAndWeights.results);
  }

  if (limit > 0 && resultsAndWeights.results.length > limit) {
    return resultsAndWeights.results.sublist(0, limit);
  }

  return resultsAndWeights.results;
}