https://github.com/carson-katri/require
Never write a long guard statement again
https://github.com/carson-katri/require
guard helper-functions swift
Last synced: 12 months ago
JSON representation
Never write a long guard statement again
- Host: GitHub
- URL: https://github.com/carson-katri/require
- Owner: carson-katri
- License: mit
- Created: 2020-06-21T20:32:37.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-31T22:38:50.000Z (almost 6 years ago)
- Last Synced: 2025-04-08T15:05:52.493Z (over 1 year ago)
- Topics: guard, helper-functions, swift
- Language: Swift
- Homepage:
- Size: 6.84 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Require
You ever notice that you're building super long `guard` statements, or have tons of guards in a row?
```swift
guard let id = player.id, let name = player.name, let status = player.status else {
throw "Player might be missing fields id, name, and/or status"
}
```
Instead of that, just `require` those fields:
```swift
let (id, name, status) = try require(from: player, \.id, \.name, \.status)
```
The `require` function creates those guards for you 🎉
You can pass up to 10 KeyPaths.
## Installation
* Add the Swift package
*or*
* Copy [Require.swift](Sources/Require/Require.swift) to your project.
## Contributing
The source is generated with GYB (`brew install nshipster/formulae/gyb`), and formatted with [SwiftFormat](https://github.com/nicklockwood/SwiftFormat):
```sh
gyb Sources/Require/Require.swift.gyb -o Sources/Require/Require.swift --line-directive '' &&
mint run nicklockwood/SwiftFormat Sources/Require/Require.swift \
--wrapparameters after-first \
--wrapparameters after-first \
--wrapcollections after-first \
--enable spaceInsideGenerics,spaceAroundGenerics,spaceInsideParens,spaceAroundParens,consecutiveSpaces,consecutiveBlankLines
```