NoahKamara / CompoundPredicate

A library for combining swift predicates
https://noahkamara.github.io/CompoundPredicate/documentation/compoundpredicate/
MIT License
49 stars 3 forks source link
swift swiftdata

Build & Test

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