This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
internal extension Sequence { | |
func grouped<T>(by keyPath: KeyPath<Element, T>) -> [T: [Element]] { | |
var groups = [T: [Element]]() | |
for element in self { | |
let key = element[keyPath: keyPath] | |
if !groups.keys.contains(key) { | |
groups[key] = [Element]() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>, ordered areInIncreasingOrder: (T, T) -> Bool = (<) ) -> Array<Element> { | |
return self.sorted(by: { | |
areInIncreasingOrder($0[keyPath: keyPath], $1[keyPath: keyPath]) | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array { | |
func filter<T: Equatable>(where keyPath: KeyPath<Element, T>, equals compareValue: T) -> [Element] { | |
return filter { $0[keyPath: keyPath] == compareValue } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class UILabelCopyable: UILabel { | |
override func copy(_ sender: Any?) { | |
UIPasteboard.general.string = text | |
} | |
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func setupCodeFrom(uri: String) -> String { | |
let startIndex = uri.index(uri.startIndex, offsetBy: "X-HM://".count) | |
let endIndex = uri.index(uri.endIndex, offsetBy: -4) | |
var actualString = uri[startIndex..<endIndex] | |
let number = UInt(actualString, radix: 36)! | |
var code = String(format: "%08u", number & 0x7ffffff) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
/// A UILabel subclass that can hold attributes and apply them to text | |
@IBDesignable | |
final class Label: UILabel { | |
convenience init(attributes: [NSAttributedStringKey: Any]) { | |
self.init() | |
self.attributes = attributes | |
} | |
// MARK: Properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UILayoutPriority { | |
/// Returns a UILayoutPriority that offsets an existing one by the specified value. | |
static func offset(_ prio: UILayoutPriority, by offset: Float) -> UILayoutPriority { | |
return UILayoutPriority(rawValue: prio.rawValue + offset) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Equatable { | |
func checkIfEqualTo<T: Equatable>(_ other: Self, byComparing paths: KeyPath<Self,T>...) -> Bool { | |
for keyPath in paths { | |
guard self[keyPath: keyPath] == other[keyPath: keyPath] else { | |
return false | |
} | |
} | |
return true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ReactiveSwift | |
import Result | |
extension Signal { | |
func injectError(_ error: Error, on signal: Signal<(), NoError>) -> Signal<Value, Error> { | |
return Signal { observer, lifetime in | |
lifetime += signal.observe { event in | |
if event.value != nil { | |
observer.send(error: error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
func string(withDelta deltaString: String) throws -> String { | |
var outString = String() | |
var indexPointer = self.startIndex | |
let inStringCount = self.utf16.count | |
try deltaString.components(separatedBy: "\t").forEach { string in | |
guard let firstChar = string.first else { return } | |
let param = string[string.index(after: string.startIndex)...] |