Skip to content

Instantly share code, notes, and snippets.

git
ghq
fzf
git-now
tig
tmux
gh
jq
fish
// MARK: Date + SwiftCheck
extension Date: RandomType {
public static func randomInRange<G : RandomGeneneratorType>(_ range : (Date, Date), gen : G) -> (Date, G) {
let (min, max) = range
let (t, gen2) = Double.randomInRange((min.timeIntervalSince1970, max.timeIntervalSince1970), gen: gen)
return (Date(timeIntervalSince1970: t), gen2)
}
}
@to4iki
to4iki / String+truncate.swift
Created March 14, 2018 17:37
string truncate extension
import Foundation
// https://qiita.com/risuke/items/cdd6a75f236faae3ce6b
// https://gist.github.com/budidino/8585eecd55fd4284afaaef762450f98e
extension String {
enum TruncationPosition {
case head
case middle
case tail
}
@to4iki
to4iki / Reader.swift
Last active March 18, 2018 16:53
DI using `Reader` monad
/// See also:
/// - https://github.com/RxSwiftCommunity/Action
/// - https://github.com/ukitaka/RealmIO
/// - https://medium.com/@JorgeCastilloPr/kotlin-dependency-injection-with-the-reader-monad-7d52f94a482e
public class Reader<Input, Element> {
public typealias WorkFactory = (Input) -> Element
private let workFactory: WorkFactory
public init(_ workFactory: @escaping WorkFactory) {
@to4iki
to4iki / mcake.swift
Created March 11, 2018 10:03
Minimal Cake Pattern
import Foundation
// See also: https://qiita.com/pab_tech/items/1c0bdbc8a61949891f1f
protocol Clock {
var now: Date { get }
}
struct SystemClock: Clock {
let now: Date = Date()
@to4iki
to4iki / DictionaryConverter.swift
Last active February 13, 2018 06:49
convert dictionary
import Foundation
typealias Parameters = [AnyHashable: Any]
protocol Converter {
func convert(_ input: Any) -> Any
}
struct BoolConverter: Converter {
func convert(_ input: Any) -> Any {
@to4iki
to4iki / Using.swift
Created November 27, 2017 15:23
Loan Pattern in Swift
/// See also
/// - http://www.ne.jp/asahi/hishidama/home/tech/scala/sample/using.html
/// - https://qiita.com/piyo7/items/c9be1f39bcfea43a778a
protocol Closer {
func close()
}
struct Loan {
@discardableResult
@to4iki
to4iki / Session+Rx.swift
Last active September 27, 2017 15:29
Extend APIKit.Session
import APIKit
import RxSwift
extension Session: ReactiveCompatible {}
extension Reactive where Base: Session {
public static func send<T: Request>(_ request: T) -> Single<T.Response> {
return Single<T.Response>.create { observer in
let task = Session.send(request, callbackQueue: .main) { result in
switch result {
@to4iki
to4iki / ShinjyukuLT#5.md
Last active February 18, 2017 14:52
tools

普段使っているツールの紹介

注意

  • これらは、プライベートで使っているツールです
  • 全て使えば良いのではなく、自分のユースケースに合えば選択してみてくださいくらいのモチベーション

ツール

to4iki/dotfilesで諸々管理しているので、良かったら参考にしてください

@to4iki
to4iki / Validation.swift
Created December 6, 2016 18:54
composite validator
import Foundation
// MARK: - EmailValidationError
enum EmailValidationError: Error {
case empty
case invalidFormat
}
// MARK: - PasswordValidationError