- Swift 94.2%
- JavaScript 3.9%
- HTML 1.2%
- Shell 0.7%
| .github/workflows | ||
| .swiftpm/xcode/package.xcworkspace/xcshareddata | ||
| Documentation.doccarchive | ||
| Sources/CompoundPredicate | ||
| Tests | ||
| .gitignore | ||
| LICENSE | ||
| mkdocs.sh | ||
| Package.resolved | ||
| Package.swift | ||
| README.md | ||
For Developers targeting macOS 14.4 / iOS 17.4 or later:
This PR For Swift Foundation adds compound functionality to Predicates like so: If you are targeting only those versions and above you should use this instead:
let notTooShort = #Predicate<Book> { $0.pages > 50 }
let notTooLong = #Predicate<Book> { $0.pages <= 350 }
let titleFilter = #Predicate<Book> { $0.title.contains("Swift") }
let filter = #Predicate<Book> {
(notTooShort.evaluate($0) && notTooLong.evaluate($0)) || titleFilter.evaluate($0)
}
CompoundPredicate
CompoundPredicate aims to improve the Predicate system to enable combining multiple predicates after constructing them:
let notTooShort = #Predicate<Book> {
$0.pages > 50
}
let notTooLong = #Predicate<Book> {
$0.pages <= 350
}
let lengthFilter = [notTooShort, notTooShort].conjunction()
// Match Books that are just the right length
let titleFilter = #Predicate<Book> {
$0.title.contains("Swift")
}
// Match Books that contain "Swift" in the title or
// are just the right length
let filter = [lengthFilter, titleFilter].disjunction()
Documentation
The documentation is available here and as Docc archive you can view using Xcode
Feedback
Please feel free to create an Issue, or even better contribute actively by creating a pull request
Implementation Progress
-
Arithmetic (+, -, *, /, %)
+,-,*PredicateExpressions.Arithmetic/PredicateExpressions.FloatDivision/PredicateExpressions.IntDivision%PredicateExpressions.IntRemainder
-
Unary minus
-PredicateExpressions.UnaryMinus -
Range (..., ..<)
...PredicateExpressions.ClosedRange..<PredicateExpressions.Rangex..<z).contains(y)PredicateExpressions.RangeExpressionContains
-
Comparison (<, <=, >, >=, ==, !=)
<,<=,>,>=PredicateExpressions.Comparison==PredicateExpressions.Equal!=PredicateExpressions.NotEqual
-
Conditionals & Ternary (?:)
PredicateExpressions.Conditional -
Boolean logic (&&, ||, !)
&&PredicateExpressions.Conjunction||PredicateExpressions.Disjunction!PredicateExpressions.Negation
-
Swift optionals (?, ??, !, flatMap(_:), if-let expressions)
?,flatMap(_:)PredicateExpressions.OptionalFlatMap??,if-letPredicateExpressions.NilCoalesce
-
Types (as, as?, as!, is)
as?PredicateExpressions.ConditionalCastas,as!PredicateExpressions.ForceCast
-
Sequence operations (allSatisfy(), filter(), contains(), contains(where:), starts(with:), max(), min())
allSatisfy()PredicateExpressions.SequenceAllSatisfyfilter()PredicateExpressions.Filter[completion:: 2024-03-10]contains()PredicateExpressions.SequenceContains[completion:: 2024-03-10]contains(where:)PredicateExpressions.SequenceContainsWherestarts(with:)PredicateExpressions.SequenceStartsWithmin()PredicateExpressions.SequenceMinimum[completion:: 2024-03-10]max()PredicateExpressions.SequenceMaximum
-
Subscript and member access ([], .)
[0,1][0]PredicateExpressions.CollectionIndexSubscript["a": "b"]["a"]PredicateExpressions.DictionaryKeySubscript["a": "b"]["a", defaultValue: "b"]PredicateExpressions.DictionaryKeyDefaultValueSubscriptobj.someKeyPredicateExpressions.KeyPath
-
String comparisons
contains(_:)PredicateExpressions.CollectionContainsCollectionlocalizedStandardContains(_:)PredicateExpressions.StringLocalizedStandardContainscaseInsensitiveCompare(_:)PredicateExpressions.StringCaseInsensitiveComparelocalizedCompare(_:)PredicateExpressions.StringLocalizedCompare