Skip to content

Instantly share code, notes, and snippets.

View Marcocanc's full-sized avatar
🚀

Marco Cancellieri Marcocanc

🚀
View GitHub Profile
@Marcocanc
Marcocanc / GermanTin.swift
Created May 10, 2019 13:43
German TIN validation
func isGermanTin(tin: String) -> Bool {
let endIndex = tin.index(tin.startIndex, offsetBy: 10)
let tinNumbers = tin[..<endIndex].compactMap { Int(String($0)) }
// First number can't be 0
guard tinNumbers.first != 0 else { return false }
// There should be exactly 8 or 9 unique numbers and exactly one that has 2 occurrances
let occurances = tinNumbers.reduce(into: [:]) { $0[$1, default: 0] += 1 }
guard 8...9 ~= occurances.keys.count else { return false }
let occurranceCount = occurances.keys.count == 9 ? 2 : 3
guard occurances.values.contains(where: { $0 == occurranceCount }) else { return false }
extension SignalProducer {
func replayingShare() -> SignalProducer<Value, Error> {
let subCount = Atomic<UInt64>(0)
var upstreamLifetime = Lifetime.empty
var token = Lifetime.Token()
var replayedProducer: SignalProducer<Value, Error> = .empty
return SignalProducer { observer, lifetime in
if upstreamLifetime.hasEnded {
upstreamLifetime = Lifetime(token)
@Marcocanc
Marcocanc / LAContext+BiometryType.swift
Last active March 16, 2018 13:35
BiometryType for iOS 8+
import LocalAuthentication
extension LAContext {
/// BiometryType that works on iOS versions older than iOS 11.
var compatBiometryType: BiometryType {
var authError: NSError?
guard canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &authError), authError == nil else { return .none }
// On iOS 11 we can get the biometry type directly.
if #available(iOS 11.0, *) {
switch biometryType {
@Marcocanc
Marcocanc / Kingfisher+Reactive.swift
Created March 6, 2018 13:05
Kingfisher Reactive Extension
import ReactiveSwift
import ReactiveCocoa
import Kingfisher
extension Kingfisher: ReactiveExtensionsProvider { }
extension Reactive where Base == Kingfisher<UIImageView> {
var image: BindingTarget<URL?> {
return makeBindingTarget {
$0.setImage(with: $1)
}
@Marcocanc
Marcocanc / String+StringFromDelta.swift
Last active April 3, 2018 16:13
Apply DiffMatchPatch Diffs to a String in pure Swift
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)...]
@Marcocanc
Marcocanc / Signal+InjectError.swift
Created January 29, 2018 10:46
Inject an Error on a given Signal
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)
@Marcocanc
Marcocanc / Equatable+CheckIfEqualTo.swift
Created January 27, 2018 12:24
Easily compare two objects by using KeyPath
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
}
}
@Marcocanc
Marcocanc / UILayoutPriority+offset.swift
Created January 26, 2018 12:20
UILayoutPriority Offset
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)
}
}
@Marcocanc
Marcocanc / Label.swift
Last active September 3, 2023 09:14
A UILabel subclass that can hold attributes and apply them to text
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
@Marcocanc
Marcocanc / HomeKitQR.swift
Created January 3, 2018 09:38
Reverse engineering HomeKit QR Code decoding/encoding
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)