Skip to content

Instantly share code, notes, and snippets.

View mminer's full-sized avatar

Matthew Miner mminer

View GitHub Profile
@mminer
mminer / SpriteKitPlaygroundStarter.swift
Last active September 29, 2016 04:27
Xcode playground starter code to experiment with SpriteKit.
import PlaygroundSupport
import SpriteKit
// Set up scene:
let size = CGSize(width: 480, height: 320)
let scene = SKScene(size: size)
scene.physicsBody = SKPhysicsBody(edgeLoopFrom: scene.frame)
scene.physicsWorld.gravity = CGVector.zero // Enabled later
@mminer
mminer / Date+Relative.swift
Last active October 12, 2016 17:32
Date extension to display the date in relative terms (e.g. "5 hours ago").
// Adapted from http://stackoverflow.com/a/1248
private let minuteInterval: TimeInterval = 60
private let hourInterval: TimeInterval = 60 * minuteInterval
private let dayInterval: TimeInterval = 24 * hourInterval
private let monthInterval: TimeInterval = 30 * dayInterval
extension Date {
var relative: String {
@mminer
mminer / socketadapter.py
Created January 5, 2015 17:30
Connection adapter for Requests that allows it to talk with raw UNIX sockets.
"""
Connection adapter for Requests that allows it to talk with raw UNIX sockets.
Adapted from requests-unixsocket, which itself was adapted from docker-py.
https://github.com/msabramo/requests-unixsocket
https://github.com/docker/docker-py/blob/master/docker/unixconn/unixconn.py
"""
import socket
from urllib.parse import unquote, urlparse
@mminer
mminer / ObservableType+IgnoreNilMap.swift
Created December 5, 2016 21:30
Custom RxSwift operator that maps and unwraps an optional value.
import RxSwift
extension ObservableType {
/// A map that unwraps an optional value, only continuing if the result is not nil.
func ignoreNilMap<R>(transform: @escaping (E) -> R?) -> Observable<R> {
return Observable.create { observer in
return self.subscribe { element in
switch element {
case .next(let value):
@mminer
mminer / SparkleCleanup.swift
Last active December 14, 2016 01:30
Removes old Sparkle updates that weren't removed properly.
import Foundation
struct SparkleCleanup {
private static let oldDownloadAge: TimeInterval = 86400 // 24 hours
/// The location where Sparkle caches file downloads.
private static var cacheURL: URL {
guard let bundleIdentifier = Bundle.main.bundleIdentifier else {
fatalError("Unable to get bundle identifier.")
@mminer
mminer / String+Ranges.swift
Created March 1, 2017 00:52
String extension to find the ranges of occurrences of a given string.
import Foundation
extension String {
/// Finds and returns the ranges of occurrences of a given string within a given range of the `String`.
func ranges(of searchString: String, options: CompareOptions = [], range searchRange: Range<Index>? = nil, locale: Locale? = nil) -> [Range<Index>] {
let searchRange = searchRange ?? startIndex..<endIndex
if let foundRange = range(of: searchString, options: options, range: searchRange, locale: locale) {
let nextRange = foundRange.upperBound..<searchRange.upperBound
@mminer
mminer / NSApplication+Restart.swift
Created May 2, 2017 19:49
Extension function to restart a Cocoa application.
import AppKit
extension NSApplication {
/// Restarts the application.
func restart() {
let process = Process()
process.launchPath = "/bin/sh"
process.arguments = ["-c", "sleep 1; open '\(Bundle.main.bundlePath)'"]
process.launch()
@mminer
mminer / NSMutableAttributedString+Hyperlink.swift
Last active October 17, 2017 21:16
Extension to NSMutableAttributedString that creates a link.
@mminer
mminer / Collection+SafeAccess.swift
Last active October 18, 2017 04:37
Collections extension for avoiding out-of-bounds exceptions.
extension Collection {
/// Returns the element at the specified index if it is within bounds, or nil if it's outside.
subscript(safe index: Index) -> Iterator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
@mminer
mminer / String+Insert.swift
Last active October 18, 2017 04:45
String extension that adds a method to insert a string in the middle of it.
extension String {
func insert(_ string: String, at index: Int) -> String {
let prefix = String(characters.prefix(index))
let suffix = String(characters.suffix(characters.count - index))
return prefix + string + suffix
}
}