Skip to content

Instantly share code, notes, and snippets.


Cassius Pacheco CassiusPacheco

View GitHub Profile
CassiusPacheco / XCTAssertThrowsErrorAsync.swift
Last active September 14, 2022 04:49
Helper for easily asserting an async method threw an error
View XCTAssertThrowsErrorAsync.swift
import Foundation
import XCTest
extension XCTestCase {
/// Expects that the async code will throw
func XCTAssertThrowsErrorAsync(
testName: String = #function,
file: StaticString = #filePath,
line: UInt = #line,
CassiusPacheco / DataResult.swift
Created February 23, 2021 23:00
This abstraction contains either a success data of generic type `T` or an error of type `ErrorCode` as its result.
View DataResult.swift
enum ErrorCode: Equatable, Error {
case unknown
// Top level server error
case serverIssue
// Network
case noNetworkConnection
CassiusPacheco / data_result.dart
Last active April 30, 2023 17:32
Dart's DataResult<S> inspired by my Swift enum implementation and Avdosev's Dart Either implementation
View data_result.dart
// The code below was inspired by my swift implementation
// and Avdosev's Dart Either
import 'package:equatable/equatable.dart';
abstract class Failure extends Equatable implements Exception {
String toString() => '$runtimeType Exception';
CassiusPacheco / color_to_materialcolor.dart
Created February 12, 2021 01:09
[Dart/Flutter] Color to Material Color
View color_to_materialcolor.dart
import 'package:flutter/material.dart';
extension MaterialX on Color {
MaterialColor toMaterial() {
return _createMaterialColor(this);
// Taken from
MaterialColor _createMaterialColor(Color color) {
final List<double> strengths = [.05];
CassiusPacheco / service_locator.dart
Last active March 18, 2021 04:55
A dependency resolver container for Dart/Flutter
View service_locator.dart
import 'package:logging/logging.dart';
typedef InstanceBuilderCallback<S> = S Function(ServiceLocator);
typedef InstanceBuilderCallback1<S, A> = S Function(ServiceLocator, A);
/// A service lookup class which allows factories of instances and singletons
/// to be registered and resolved as part of a dependency injection system.
class ServiceLocator {
final Map<String, InstanceBuilderCallback> _factories = {};
final Map<String, dynamic> _factories1 = {};
CassiusPacheco / EraseToAnyPublisherFailure.swift
Last active May 17, 2020 03:18
Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
View EraseToAnyPublisherFailure.swift
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Self.Failure == Never {
/// Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
/// Use `eraseToAnyPublisherFailure(to:)` to expose an instance of AnyPublisher to the downstream subscriber, rather than this publisher’s actual type.
/// Under the hood it updates the failure type by calling `setFailureType(to:)` applying the given type.
public func eraseToAnyPublisherFailure<E>(to failureType: E.Type) -> AnyPublisher<Self.Output, E> where E: Error {
return self
.setFailureType(to: failureType)
CassiusPacheco / NSMutableAttributedString+Extensions.swift
Last active May 6, 2020 04:17
Extensions to make attributed strings composable
View NSMutableAttributedString+Extensions.swift
import Foundation
public func + (lhs: NSAttributedString, rhs: NSAttributedString) -> NSMutableAttributedString {
let mutable = NSMutableAttributedString(attributedString: lhs)
return mutable
extension String {
public var attributedString: NSMutableAttributedString {
CassiusPacheco / Assert.swift
Created April 29, 2020 06:57
Assert helper that provides a few functions for asserting while debugging
View Assert.swift
public final class Assert {
public static var shouldCrashOnDebug = true
fileprivate static var isUnitTesting = NSClassFromString("XCTest") != nil
public func debugAssertion(_ condition: @autoclosure () -> Bool, message: String = "") {
if !condition() {
if Assert.shouldCrashOnDebug {
CassiusPacheco / SingleDisposer.swift
Created April 28, 2020 11:54
A dispose bag that keeps only one disposable in the bag at a time.
View SingleDisposer.swift
import Foundation
import RxSwift
extension Disposable {
/// Adds `self` to `single`
/// - parameter single: `SingleDisposer` to add `self` to.
/// - note: `SingleDisposer` is a wrapper of `DisposeBag` that is recreated
/// whenever a disposable object is inserted into it, cancelling any
/// previous subscriptions. It keeps at most one subscription at a
CassiusPacheco / WaitForCompletion.swift
Last active September 1, 2022 08:30
toBlocking equivalent for Combine's Publishers
View WaitForCompletion.swift
extension Publisher {
func waitForCompletion(timeout: TimeInterval = 1.0, file: StaticString = #file, line: UInt = #line) throws -> [Output] {
let expectation = XCTestExpectation(description: "wait for completion")
var completion: Subscribers.Completion<Failure>?
var output = [Output]()
let subscription = self.collect()
.sink(receiveCompletion: { receiveCompletion in
completion = receiveCompletion