Skip to content

Commit

Permalink
Double now also has maxValue and minValue like CGFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangerhards committed Jan 5, 2022
1 parent 511a307 commit 09ecf5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/SwiftPlus/Extensions/CGFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import CoreGraphics

public extension CGFloat {

/// Clamps a CGFloat to a max value
/// - Parameter max: The maximal allowed value for this CGFloat
/// - Returns: The CGFloat with the specified maximum value
/// Clamps a `CGFloat` to a max value
/// - Parameter max: The maximal allowed value for this `CGFloat`
/// - Returns: The `CGFloat` with the specified maximum value
func maxValue(_ max: CGFloat) -> CGFloat {
if(self > max) {
return max
Expand All @@ -33,9 +33,9 @@ public extension CGFloat {
}
}

/// Clamps a CGFloat to a min value
/// - Parameter min: The minimal allowed value for this CGFloat
/// - Returns: The CGFloat with the specified minimum value
/// Clamps a `CGFloat` to a min value
/// - Parameter min: The minimal allowed value for this `CGFloat`
/// - Returns: The `CGFloat` with the specified minimum value
func minValue(_ min: CGFloat) -> CGFloat {
if(self < min) {
return min
Expand Down
22 changes: 22 additions & 0 deletions Sources/SwiftPlus/Extensions/Double.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,26 @@ public extension Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}

/// Clamps a `Double` to a max value
/// - Parameter max: The maximal allowed value for this `Double`
/// - Returns: The `Double` with the specified maximum value
func maxValue(_ max: Double) -> Double {
if(self > max) {
return max
}else {
return self
}
}

/// Clamps a `Double` to a min value
/// - Parameter min: The minimal allowed value for this `Double`
/// - Returns: The `Double` with the specified minimum value
func minValue(_ min: Double) -> Double {
if(self < min) {
return min
}else {
return self
}
}
}

0 comments on commit 09ecf5b

Please sign in to comment.