LCOV - code coverage report
Current view: | top level - src/services - filter_service.dart (source / functions) |  |  | Hit | Total | Coverage
Test: | lcov.info |  | Lines: | 32 | 32 | 100.0 %
Date: | 2021-10-11 06:57:03 |  | Functions: | 0 | 0 | -

              Line data    Source code
           1             : import 'package:widgetbook/src/models/models.dart';
           2             :
           3             : class FilterService {
           4          15 :   const FilterService();
           5             :
           6           1 :   List<Category> filter(
           7             :     String searchTerm,
           8             :     List<Category> categories,
           9             :   ) {
          10           1 :     return _filterCategories(
          11           1 :       RegExp(searchTerm),
          12             :       categories,
          13             :     );
          14             :   }
          15             :
          16           1 :   List<Category> _filterCategories(
          17             :     RegExp regExp,
          18             :     List<Category> categories,
          19             :   ) {
          20           1 :     final matchingOrganizers = <Category>[];
          21           2 :     for (final category in categories) {
          22           1 :       final result = _filterOrganizer(regExp, category) as Category?;
          23           1 :       if (_isMatch(result)) {
          24           1 :         matchingOrganizers.add(result!);
          25             :       }
          26             :     }
          27             :     return matchingOrganizers;
          28             :   }
          29             :
          30           1 :   ExpandableOrganizer? _filterOrganizer(
          31             :       RegExp regExp, ExpandableOrganizer organizer) {
          32           2 :     if (organizer.name.contains(regExp)) {
          33             :       return organizer;
          34             :     }
          35             :
          36           1 :     final matchingFolders = <Folder>[];
          37           2 :     for (final subOrganizer in organizer.folders) {
          38           1 :       final result = _filterOrganizer(regExp, subOrganizer);
          39           1 :       if (_isMatch(result)) {
          40           1 :         matchingFolders.add(result! as Folder);
          41             :       }
          42             :     }
          43             :
          44           1 :     final matchingWidgets = <WidgetElement>[];
          45           2 :     for (final subOrganizer in organizer.widgets) {
          46           1 :       final result = _filterOrganizer(regExp, subOrganizer);
          47           1 :       if (_isMatch(result)) {
          48           1 :         matchingWidgets.add(result! as WidgetElement);
          49             :       }
          50             :     }
          51             :
          52           2 :     if (matchingFolders.isNotEmpty || matchingWidgets.isNotEmpty) {
          53           1 :       return _createFilteredSubtree(
          54             :         organizer,
          55             :         matchingFolders,
          56             :         matchingWidgets,
          57             :       );
          58             :     }
          59             :
          60             :     return null;
          61             :   }
          62             :
          63           1 :   ExpandableOrganizer _createFilteredSubtree(
          64             :     ExpandableOrganizer organizer,
          65             :     List<Folder> folders,
          66             :     List<WidgetElement> widgets,
          67             :   ) {
          68           1 :     if (organizer is Category) {
          69           1 :       return Category(
          70           1 :         name: organizer.name,
          71             :         widgets: widgets,
          72             :         folders: folders,
          73             :       );
          74             :     }
          75             :
          76             :     // otherwise it can only be a folder
          77           1 :     return Folder(
          78           1 :       name: organizer.name,
          79             :       widgets: widgets,
          80             :       folders: folders,
          81           1 :       isExpanded: organizer.isExpanded,
          82             :     );
          83             :   }
          84             :
          85           1 :   bool _isMatch(ExpandableOrganizer? organizer) {
          86             :     return organizer != null;
          87             :   }
          88             : }

Generated by: LCOV version 1.15