documentListFromIterable method

List<Snapshot> documentListFromIterable(
  1. Iterable<Snapshot> iterable,
  2. {DocumentScoring documentScoring}
)

Converts an iterable into an unmodifiable result list.

Optional parameter documentScoringAlgorithm can be used to replace the default document scoring algorithm.

Implementation

List<Snapshot> documentListFromIterable(
  Iterable<Snapshot> iterable, {
  DocumentScoring documentScoring,
}) {
  final filter = this.filter;
  final sorter = this.sorter;
  final skip = this.skip;
  final take = this.take;
  documentScoring ??= const DocumentScoring();
  final documentScoringState = documentScoring.newState(filter);
  if (filter != null) {
    iterable = iterable.where(
      (snapshot) => documentScoringState.evaluateSnapshot(snapshot) > 0.0,
    );
  }
  if (sorter != null) {
    final list = iterable.toList(growable: false);
    list.sort((a, b) {
      return sorter.compare(a.data, b.data);
    });
    iterable = list;
  }
  if (skip != 0) {
    iterable = iterable.skip(skip);
  }
  if (take != null) {
    iterable = iterable.take(take);
  }
  return List<Snapshot>.unmodifiable(iterable);
}