Parsing search string and generate predicates or filtering blocks from it.
Current framework allows to use logical operators with search (like SearchKit
). But only you don't need to generate Index
file for using it.
Goal of this framework is generating one NSPredicate
, closure, another filtering entities for using them for searching in array, database or any other sources.
Default entities supports AND, &
, OR, |
, NOT, !
operators, but you can create custom operators yourself.
Default query operators (from highest to lowest precedence):
Operator | Meaning |
---|---|
NOT , ! |
Boolean NOT. |
AND , &, <space> |
Boolean AND. The character represents a Boolean operator when there are terms to both sides of the character. In this case, represents a Boolean AND by default, or a Boolean OR if specified by .spaceMeansOR option. |
OR , | |
Boolean inclusive OR. |
iOS (Generating blocks for using array.filter(_:)
) and Mac (generating NSPredicate
for using with CoreData
)
It's pretty easy! You just need to create builder and define predicate/block for one token(small part of search string), after it you can generate predicates/blocks from any string.
Example with blocks:
import SearchQueryParser
let builder = DefaultFilterBlockBuilder<Item>(options: .caseInsensitive, valuePredicate: { str in
return { $0.searchString.lowercased().contains(str.lowercased()) }
})
let filterBlock = builder.build(from: searchText)
filteredItems = array.filter(filterBlock ?? {_ in true})
Example with NSPredicate
:
import SearchQueryParser
let builder = DefaultPredicateBuilder() { NSPredicate(format: "(name LIKE[c] %@) OR (job LIKE[c] %@)", $0, $0) }
self.predicate = builder.build(from: searchField.stringValue)
pod 'SearchQueryParser'
Drag'n'drop SearchQueryParser
directory to your project.