https://github.com/modulovalue/dart_filter
Filter pattern in Dart
https://github.com/modulovalue/dart_filter
Last synced: 6 months ago
JSON representation
Filter pattern in Dart
- Host: GitHub
- URL: https://github.com/modulovalue/dart_filter
- Owner: modulovalue
- License: bsd-2-clause
- Created: 2019-08-18T05:29:52.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-10-26T07:53:22.000Z (over 5 years ago)
- Last Synced: 2023-08-20T21:43:38.641Z (almost 2 years ago)
- Language: Dart
- Homepage: https://pub.dev/packages/dart_filter
- Size: 60.5 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# dart_filter
High Level Filter Queries for Dart, inspired by the [Filter Pattern](https://www.tutorialspoint.com/design_pattern/filter_pattern.htm)[](https://medium.com/p/introducing-dart-filter-high-level-filter-queries-for-dart-eb8d0001abb) [](https://travis-ci.com/modulovalue/dart_filter) [](https://codecov.io/gh/modulovalue/dart_filter) [](https://github.com/modulovalue/dart_filter/blob/master/LICENSE) [](https://pub.dartlang.org/packages/dart_filter) [](https://github.com/modulovalue/dart_filter) [](https://twitter.com/modulovalue) [](https://github.com/modulovalue)
dart_filter lets you easily filter dart Lists and Iterables. New requirements oftentimes
break naive filtering solutions. dart_filter was made to fix that.Example:
```dart
...
people.where(
AndCriteria([
/// Only adults
const OnlyAdultsFilter().map((Person p) => p.age),/// Only females
AnonymousCriteria((Person p) => p.sex == Sex.female),/// Only names that contain an s
if (withAnSOnly)
const ContainsFilter("s", true).map((Person p) => p.name),
]),
);class OnlyAdultsFilter extends FilterCriteria {
const OnlyAdultsFilter();@override
bool accepts(int t) => t >= 18;
}
```Each FilterCriteria overrides the call method and therefore acts as a function that you can pass to higher-order functions like `.where`.
## All available FilterCriteria:
- **AcceptAllCriteria**: accepts everything.
- **AcceptNoneCriteria**: accepts nothing.
- **AndCriteria (.and or &)**: accepts an item only if all criteria are met.
- **OrCriteria (.or or |)**: accepts an item only if at least one of the provided criteria is met.
- **XOrCriteria (.xor)**: accepts an item only if one of the provided criteria is met.
- **NotCriteria (.not)**: negates the provided criteria.
- **MapCriteria (.map)**: use this to map a criteria to a different type.
- **EqualsCriteria**: accepts an item only if it is equal (==) to the provided value.
- **AnonymousCriteria**: use this to quickly prototype a custom criteria without subclassing FilterCriteria.There are also some criteria for Lists of Strings:
- **StartsWithFilter**: accepts only Strings that start with the provided string.
- **EndsWithFilter**: accepts only Strings that end with the provided string.
- **ContainsFilter**: accepts only Strings that contain the provided string.