This file contains hidden or 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 > (left: NSDate, right: NSDate) -> Bool { | |
return left.compare(right) == .OrderedDescending | |
} | |
extension NSCalendar { | |
func dateRange(startDate startDate: NSDate, endDate: NSDate, stepUnits: NSCalendarUnit, stepValue: Int) -> DateRange { | |
let dateRange = DateRange(calendar: self, startDate: startDate, endDate: endDate, stepUnits: stepUnits, stepValue: stepValue, multiplier: 0) | |
return dateRange |
This file contains hidden or 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 NSView { | |
func insetSubview(subview: NSView, dx: CGFloat, dy: CGFloat) { | |
addSubview(subview) | |
subview.translatesAutoresizingMaskIntoConstraints = false | |
let constrain = { (attribute: NSLayoutAttribute, constant: CGFloat) in | |
subview.addConstraint(NSLayoutConstraint( | |
item: subview, | |
attribute: attribute, | |
relatedBy: .Equal, |
This file contains hidden or 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
public class IdentifierGenerator { | |
private let characters = Array("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") | |
private let separator = "-" | |
public func generateIdentifier() -> String { | |
let numCharacters = UInt32(characters.count) | |
func randomCharacter() -> Character { | |
let index = Int(arc4random_uniform(numCharacters)) | |
return characters[index] |
This file contains hidden or 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
// | |
// Created by Adam Preble on 2/19/15. | |
// | |
/// Weak, unordered collection of objects. | |
public struct WeakSet<T where T: AnyObject, T: Hashable> { | |
typealias Element = T | |
/// Maps Element hashValues to arrays of Entry objects. | |
/// Invalid Entry instances are culled as a side effect of add() and remove() |
This file contains hidden or 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
#include <assert.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include "wren.h" | |
void logFromWren(WrenVM *vm) { | |
//double x = wrenGetArgumentDouble(vm, 0); // returns 0 in this setting | |
const char *message = wrenGetArgumentString(vm, 1); | |
printf("logFromWren: %s\n", message); | |
} |
This file contains hidden or 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 | |
/// Call a throwing block synchronously. | |
func dispatch_sync(queue: dispatch_queue_t, block: () throws -> ()) throws { | |
var error: ErrorType? | |
dispatch_sync(queue) { | |
do { | |
try block() | |
} catch let caughtError { | |
error = caughtError |
This file contains hidden or 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 ErrorType { | |
var myLocalizedDescription: String { | |
if self.dynamicType == NSError.self { | |
return ((self as Any) as! NSError).localizedDescription | |
} | |
else { | |
return "\(self)" | |
} | |
} |
This file contains hidden or 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 | |
/// Helper object for more Swift-like KVO. To create, call NSObject.addObserverForKeyPath(_:handler:). | |
class KeyValueObserver: NSObject { | |
private weak var observing: NSObject? | |
private var keyPath: String | |
private var handler: () -> Void | |
private init(observing: NSObject, keyPath: String, handler: () -> Void) { |
This file contains hidden or 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
/// Convert a Pivotal Tracker story id (#1234, 1234) or URL into a Markdown link using the story number as the link text. | |
function markdownTrackerLink(text) { | |
var regex = /\s*(http.*pivotaltracker\.com\/story\/show\/(\d+)|#?(\d+))\s*/; | |
var id; | |
var groups; | |
if (regex.test(text)) { | |
groups = regex.exec(text); | |
id = groups[3] || groups[2]; | |
return "[#"+id+"](https://www.pivotaltracker.com/story/show/"+id+")"; |
This file contains hidden or 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
final class Completion<R> { | |
private let closure: (R) -> Void | |
private var cancelled = false | |
/// `closure` is called upon completion, if not cancelled. | |
init(closure: (R) -> Void) { | |
self.closure = closure | |
} | |