Skip to content

Commit 31c05af

Browse files
committed
Added predicate functions for Optional<String> keypaths
1 parent f1faf90 commit 31c05af

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Sources/RCDataKit/NSExtensions/NSPredicate+.swift

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,138 @@ extension KeyPath where Root: NSManagedObject, Value == String {
283283
}
284284
}
285285

286+
// MARK: Optional Strings
287+
288+
extension KeyPath where Root: NSManagedObject, Value == Optional<String> {
289+
290+
// String Not Equal
291+
public func notEqual<Y: StringProtocol>(
292+
to rhs: Y,
293+
options: NSComparisonPredicate.Options = []
294+
) -> NSPredicate {
295+
NSComparisonPredicate(
296+
leftExpression: NSExpression(
297+
forKeyPath: stringRepresentation
298+
),
299+
rightExpression: NSExpression(
300+
forConstantValue: rhs
301+
),
302+
modifier: .direct,
303+
type: .notEqualTo,
304+
options: options
305+
)
306+
}
307+
308+
// String Equal
309+
public func equal<Y: StringProtocol>(
310+
to rhs: Y,
311+
options: NSComparisonPredicate.Options = []
312+
) -> NSPredicate {
313+
NSComparisonPredicate(
314+
leftExpression: NSExpression(
315+
forKeyPath: stringRepresentation
316+
),
317+
rightExpression: NSExpression(
318+
forConstantValue: rhs
319+
),
320+
modifier: .direct,
321+
type: .equalTo,
322+
options: options
323+
)
324+
}
325+
326+
// String LIKE
327+
public func like<Y: StringProtocol>(
328+
_ comparator: Y,
329+
options: NSComparisonPredicate.Options = []
330+
) -> NSPredicate {
331+
NSComparisonPredicate(
332+
leftExpression: NSExpression(
333+
forKeyPath: stringRepresentation
334+
),
335+
rightExpression: NSExpression(
336+
forConstantValue: comparator
337+
),
338+
modifier: .direct,
339+
type: .like,
340+
options: options
341+
)
342+
}
343+
344+
// String CONTAINS
345+
public func contains<Y: StringProtocol>(
346+
_ substring: Y,
347+
options: NSComparisonPredicate.Options = []
348+
) -> NSPredicate {
349+
NSComparisonPredicate(
350+
leftExpression: NSExpression(
351+
forKeyPath: stringRepresentation
352+
),
353+
rightExpression: NSExpression(
354+
forConstantValue: substring
355+
),
356+
modifier: .direct,
357+
type: .contains,
358+
options: options
359+
)
360+
}
361+
362+
// String BEGINSWITH
363+
public func beginsWith<Y: StringProtocol>(
364+
_ prefix: Y,
365+
options: NSComparisonPredicate.Options = []
366+
) -> NSPredicate {
367+
NSComparisonPredicate(
368+
leftExpression: NSExpression(
369+
forKeyPath: stringRepresentation
370+
),
371+
rightExpression: NSExpression(
372+
forConstantValue: prefix
373+
),
374+
modifier: .direct,
375+
type: .beginsWith,
376+
options: options
377+
)
378+
}
379+
380+
// Stirng ENDSWITH
381+
public func endsWith<Y: StringProtocol>(
382+
_ suffix: Y,
383+
options: NSComparisonPredicate.Options = []
384+
) -> NSPredicate {
385+
NSComparisonPredicate(
386+
leftExpression: NSExpression(
387+
forKeyPath: stringRepresentation
388+
),
389+
rightExpression: NSExpression(
390+
forConstantValue: suffix
391+
),
392+
modifier: .direct,
393+
type: .endsWith,
394+
options: options
395+
)
396+
}
397+
398+
// String MATCHES
399+
public func matches<Y: StringProtocol>(
400+
_ regex: Y,
401+
options: NSComparisonPredicate.Options = []
402+
) -> NSPredicate {
403+
NSComparisonPredicate(
404+
leftExpression: NSExpression(
405+
forKeyPath: stringRepresentation
406+
),
407+
rightExpression: NSExpression(
408+
forConstantValue: regex
409+
),
410+
modifier: .direct,
411+
type: .matches,
412+
options: options
413+
)
414+
}
415+
}
416+
417+
286418
// MARK: IN
287419

288420
extension KeyPath where Root: NSManagedObject, Value: Equatable {

0 commit comments

Comments
 (0)