From 44ccb8dd28bdaea2e1e0cf087e0f2abba7c74f60 Mon Sep 17 00:00:00 2001 From: Vitaliy Salnikov Date: Thu, 23 Jan 2020 16:33:20 +0300 Subject: [PATCH] Overload 'in' method for Comparable to use ClosedRange --- Sources/Extensions/Comparable/Comparable+Extensions.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/Extensions/Comparable/Comparable+Extensions.swift b/Sources/Extensions/Comparable/Comparable+Extensions.swift index 22fec806..b638fee4 100644 --- a/Sources/Extensions/Comparable/Comparable+Extensions.swift +++ b/Sources/Extensions/Comparable/Comparable+Extensions.swift @@ -30,4 +30,11 @@ public extension Comparable { func `in`(bounds: (lower: Self, upper: Self)) -> Self { return min(max(bounds.lower, self), bounds.upper) } + + /// Use this function to restrict comparable with lower and upper values + /// - parameter range: ClosedRange representing bounds + /// - returns: Current value if it fits range, otherwise lower if value is too small or upper if value is too big + func `in`(range: ClosedRange) -> Self { + return `in`(bounds: (lower: range.lowerBound, upper: range.upperBound)) + } }