项目作者: ollieatkinson

项目描述 :
A small addition to Swift adding predicate matchers for filtering operators using KeyPaths, it just feels natural™
高级语言: Swift
项目地址: git://github.com/ollieatkinson/Predicate.git
创建时间: 2018-12-05T16:44:36Z
项目社区:https://github.com/ollieatkinson/Predicate

开源协议:MIT License

下载


Predicate

Swift 5.2
SPM
SPM
macOS
Licence

Predicate Composition

API

Equals

  1. public func == <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Equatable
  2. public func != <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Equatable

Example

  1. users.filter(\.name == "Milos")
  2. users.filter(\.name != "Ste")

Less Than & More Than

  1. public func < <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
  2. public func > <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
  3. public func <= <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable
  4. public func >= <Root, Value>(block: @escaping (Root) -> Value, value: Value) -> (Root) -> Bool where Value : Comparable

Example

  1. users.filter(\.age > 25)
  2. users.filter(\.age <= 55)

Similar to

  1. public func << <Root, S>(block: @escaping (Root) -> S.Element, value: S) -> (Root) -> Bool where S : Sequence, S.Element : Equatable
  2. public func ~= <Root>(block: @escaping (Root) -> String, regex: Regex) -> (Root) -> Bool
  3. public func == <Root, Value>(block: @escaping (Root) -> Value, value: (Value, Value)) -> (Root) -> Bool where Value : FloatingPoint
  4. public func ± <Value>(number: Value, accuracy: Value) -> (Value, Value) where Value : FloatingPoint

Example

  • << sequence contains
  • ± comparing floating point numbers to a degree of accuracy
  • ~= Regex
  1. users.filter(\.age << 30...35)
  2. users.filter(\.age << [ 30, 32, 34 ])
  3. users.filter(\.weight == 85 ± 4
  4. users.filter(\.name ~= "o(s|ah)$")

Boolean Logic

  1. public prefix func ! <Root>(block: @escaping (Root) -> Bool) -> (Root) -> Bool
  2. public func && <Root>(lhs: @autoclosure @escaping () -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
  3. public func || <Root>(lhs: @autoclosure @escaping () -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
  4. public func && <Root>(lhs: @escaping (Root) -> Bool, rhs: @autoclosure @escaping () -> Bool) -> (Root) -> Bool
  5. public func || <Root>(lhs: @escaping (Root) -> Bool, rhs: @autoclosure @escaping () -> Bool) -> (Root) -> Bool
  6. public func && <Root>(lhs: @escaping (Root) -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool
  7. public func || <Root>(lhs: @escaping (Root) -> Bool, rhs: @escaping (Root) -> Bool) -> (Root) -> Bool

Example

  1. users.filter(\.isClearToFly && \.name == "Noah")
  2. users.filter(\.age > 30 && \.name != "Noah")

Installation

SwiftPM:

  1. package.append(.package(url: "https://github.com/ollieatkinson/Predicate", from: "1.0.0"))