CypherPoet/ClampedPropertyWrapper
A Swift Property Wrapper for automatically clamping Comparable values within a closed range.
Features
- β
Clamp any
Comparabletype to aClosedRangeor a "Half-Open" range. - β
Additional support for initializing clamped
FloatingPointproperties with aPartialRangeThrough,PartialRangeFrom, orPartialRangeUpToexpression. - β
Initialize clamped
FloatingPointorFixedWidthIntegerproperties with exclusive lower and upper bounds.
Installation
Xcode Projects
Select File -> Swift Packages -> Add Package Dependency and enter https://github.com/CypherPoet/ClampedPropertyWrapper.
Swift Package Manager Projects
You can add ClampedPropertyWrapper as a package dependency in your Package.swift file:
let package = Package(
//...
dependencies: [
.package(url: "https://github.com/CypherPoet/ClampedPropertyWrapper", .upToNextMinor(from: "0.2.0")),
],
//...
)From there, refer to ClampedPropertyWrapper as a "target dependency" in any of your package's targets that need it.
targets: [
.target(
name: "YourLibrary",
dependencies: [
"ClampedPropertyWrapper",
],
...
),
...
]Then simply import ClampedPropertyWrapper wherever youβd like to use it.
Usage
Basic Example:
import ClampedPropertyWrapper
struct Player {
@Clamped(
above: 0.0.nextDown,
andBelow: .infinity
)
var xp: Double = 0.0
@Clamped(to: 1..<100)
var level: Int = 1
@Clamped(above: 0.0, andBelow: Double.infinity)
var number: Double = 1.0
@Clamped(to: "A"..."Z")
var firstInitial: Character = "A"
@Clamped(to: 0.0...)
var ammo: Double = 0.1
@Clamped(to: ...100.0)
var reputation: Double = .zero
@Clamped(to: ..<20)
var weeklyLeaderboardRankChange: Int = .zero
}You can also take things further in this project's Xcode Playground:
[Playground Example]
Contributing
Contributions to ClampedPropertyWrapper are most welcome. Check out some of the issue templates for more info.
π» Developing
Requirements
- Xcode 12.5+ (Recommended)
π Generating Documentation
Documentation is generated by Swift Doc. Installation instructions can be found here, and as soon as you have it set up, docs can be generated simply by running ./Scripts/generate-html-docs.zsh from the command line.
π Note that this will only generate a .build/documentation folder for you to view locally. This folder is being ignored by git, and a GitHub action exists to automatically generate docs at the root level and serve them on the project's gh-pages branch.
π· License
ClampedPropertyWrapper is available under the MIT license. See the LICENSE file for more info.
Package Metadata
Repository: CypherPoet/ClampedPropertyWrapper
Homepage: https://cypherpoet.github.io/ClampedPropertyWrapper/
Stars: 3
Forks: 1
Open issues: 1
Default branch: main
Primary language: swift
License: MIT
Topics: clamped, clamped-property-wrapper, input-validation, swift, swift-clamp, swift-package-manager, swift-packages, swift-property-wrapper-packages, swift-property-wrappers, swift-world-domination
README: README.md