mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			37 lines
		
	
	
		
			981 B
		
	
	
	
		
			Swift
		
	
			
		
		
	
	
			37 lines
		
	
	
		
			981 B
		
	
	
	
		
			Swift
		
	
| // Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
 | |
| 
 | |
| import Foundation
 | |
| 
 | |
| extension Optional {
 | |
|     public func map<U>(_ transform: (Wrapped) throws -> U?) rethrows -> U? {
 | |
|         switch self {
 | |
|             case .some(let value): return try transform(value)
 | |
|             default: return nil
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     public func asType<R>(_ type: R.Type) -> R? {
 | |
|         switch self {
 | |
|             case .some(let value): return (value as? R)
 | |
|             default: return nil
 | |
|         }
 | |
|     }
 | |
|     
 | |
|     public func defaulting(to value: Wrapped) -> Wrapped {
 | |
|         return (self ?? value)
 | |
|     }
 | |
|     
 | |
|     public mutating func setting(to value: Wrapped) -> Wrapped {
 | |
|         self = value
 | |
|         return value
 | |
|     }
 | |
| }
 | |
| 
 | |
| extension Optional where Wrapped == String {
 | |
|     public func defaulting(to value: Wrapped, useDefaultIfEmpty: Bool = false) -> Wrapped {
 | |
|         guard !useDefaultIfEmpty || self?.isEmpty != true else { return value }
 | |
|         
 | |
|         return (self ?? value)
 | |
|     }
 | |
| }
 |