mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-11 04:34:55 -06:00
41 lines
1.5 KiB
Swift
41 lines
1.5 KiB
Swift
// MARK: - Optional where Wrapped: Collection
|
|
|
|
public extension Optional where Wrapped: Collection {
|
|
// MARK: Properties
|
|
|
|
/// Returns true if the value is `nil` or an empty collection.
|
|
var isEmptyOrNil: Bool {
|
|
self?.isEmpty ?? true
|
|
}
|
|
}
|
|
|
|
// MARK: - Optional<String>
|
|
|
|
public extension String? {
|
|
// MARK: Properties
|
|
|
|
/// Returns true if the value is `nil`, an empty string or a string full of `.whitespacesAndNewlines`.
|
|
var isWhitespaceOrNil: Bool {
|
|
guard let self else {
|
|
return true
|
|
}
|
|
return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
// MARK: Methods
|
|
|
|
/// Returns the `fallback` if the value is `nil` or whitespace. Otherwise, returns the same value.
|
|
/// - Parameter fallback: The value to be used as a fallback when `nil` or whitespace.
|
|
/// - Returns: `fallback` if the value is `nil` or whitespace. Otherwise, returns the same value.
|
|
func fallbackOnWhitespaceOrNil(fallback: String?) -> String? {
|
|
isWhitespaceOrNil ? fallback : self
|
|
}
|
|
|
|
/// Returns the `fallback` if the value is `nil` or whitespace. Otherwise, returns the same value.
|
|
/// - Parameter fallback: The non-nil value to be used as a fallback when `nil` or whitespace.
|
|
/// - Returns: `fallback` if the value is `nil` or whitespace. Otherwise, returns the same value.
|
|
func fallbackOnWhitespaceOrNil(fallback: String) -> String {
|
|
isWhitespaceOrNil ? fallback : self!
|
|
}
|
|
}
|