Swiftpack.co -  Package - shareup/clamp-apple
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Provides `clamped()` functions to Swift's built-in integer and floating point types.
.package(url: "https://github.com/shareup/clamp-apple.git", from: "v2.0.0")


Clamp is a library with the single purpse of providing clamp() and clamped() functions to Swift's built-in integer and floating point types. The clamp() and clamped() functions limit a value to the inclusive range of the passed-in minimum and maximum values.


// immutable version
5.clamped(lowerBound: 1, upperBound: 10) // returns 5
0.clamped(lowerBound: 1, upperBound: 10) // returns 1
11.clamped(lowerBound: 1, upperBound: 10) // returns 10

5.clamped(to: 1...10) // returns 5
0.clamped(to: 1...10) // returns 1
11.clamped(to: 1...10) // returns 10

// mutable version
var number = 5
number.clamp(to: 1...10) // no change
number.clamp(to: 1...4) // `number` is changed to 4
number.clamp(to: 10...20) // `number` is changed to 10


Swift Package Manager

To use Clamp with the Swift Package Manager, add a dependency to your Package.swift file:

let package = Package(
  dependencies: [
    .package(name: "Clamp", url: "https://github.com/shareup/clamp-apple.git", .upToNextMajor(from: "2.0.0"))


The license for Clamp is the standard MIT license. You can find it in the LICENSE file.


Stars: 0
Last commit: 4 weeks ago

Release Notes

4 weeks ago

Add mutable versions of clamp() and rename the package from Clamped to Clamp.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco