https://github.com/texdc/specify-dart
Specification library for dart
https://github.com/texdc/specify-dart
dart filter specification
Last synced: 4 months ago
JSON representation
Specification library for dart
- Host: GitHub
- URL: https://github.com/texdc/specify-dart
- Owner: texdc
- License: mit
- Created: 2017-01-16T23:45:05.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-05T03:01:15.000Z (over 9 years ago)
- Last Synced: 2025-07-19T19:32:57.515Z (11 months ago)
- Topics: dart, filter, specification
- Language: Dart
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# specify
[Specifications](https://www.martinfowler.com/apsupp/spec.pdf) allow simple classes to encapsulate business rules in a flexible,
testable, and reusable manner.
## examples
```dart
import 'package:specify/specify.dart';
class StringContains extends Specification {
final String _substring;
StringContains(this._substring);
bool isSatisfiedBy(String aString) => aString.contains(_substring);
}
class StringNotContains extends StringContains {
StringNotContains(String aSubstring) : super(aSubstring);
bool isSatisfiedBy(String aString) => !super.isSatisfiedBy(aString);
}
class StringIn extends OrSpecification {
StringIn(List aWordList) : super(
aWordList.map((String aWord) => new StringContains(aWord)).toList()
);
}
class StringNotIn extends AndSpecification {
StringNotIn(List aWordList) : super(
aWordList.map((String aWord) => new StringNotContains(aWord)).toList()
);
}
main() {
StringIn isWord = new StringIn(['foo', 'bar']);
StringNotIn isNotWord = new StringNotIn(['baz', 'zim']);
print(isWord('barge'));
print(isNotWord('hello'));
}
```
### filters
Filtering with specifications can be very powerful, especially when used with `CompositeSpecification`.
```dart
import 'package:specify/specify.dart';
class StringLength extends Specification {
final int _length;
StringLength(this._length);
bool isSatisfiedBy(String aString) => aString.length == _length;
}
class LengthFilter extends Filter {
LengthFilter(int aLength) : super(new StringLength(aLength));
}
main() {
LengthFilter filter = new LengthFilter(3);
print(filter(['foo', 'bar', 'nice', 'to', 'know', 'you']));
}
```