Skip to content

Instantly share code, notes, and snippets.

View kgn's full-sized avatar

David Keegan kgn

View GitHub Profile
@kgn
kgn / Middleware.swift
Last active February 2, 2017 16:13
Vapor Middleware to support a single index.html file for React routing.
import HTTP
import Vapor
extension Droplet {
func index(of middlewareType: Middleware.Type) -> Int? {
for (i, middleware) in self.middleware.enumerated() {
if type(of: middleware) == middlewareType.self {
return i
}
@kgn
kgn / OrganizeLayers.js
Created October 23, 2016 02:37
SketchAPI plugin to organize layers. Orders them based on their frames and renames them with a prefix.
var sketch = context.api()
function moveForward(layer, amount) {
for (var i = 0; i < amount; ++i) {
layer.moveForward()
}
}
function organizeLayers(prefix, layers) {
var sortedLayers = [];
func romanNumerals(input: Int) -> String {
var number = input
let numerals = ["M", "M", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
let values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
var numeralString = ""
for (i, numeral) in numerals.enumerate() {
while number >= values[i] {
number -= values[i]
numeralString += numeral
@kgn
kgn / nice_date.swift
Created May 25, 2016 02:33
Add 'st', 'nd', 'rd' and 'th' to data strings: May 17th, April 2rd...
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d"
private func daySuffix(date: NSDate) -> String {
let calendar = NSCalendar.currentCalendar()
let dayOfMonth = calendar.component(.Day, fromDate: date)
switch dayOfMonth {
case 1: fallthrough
case 21: fallthrough
case 31: return "st"
userNoteLabel.text = configData["user_note"] as? String
let mainBundle = NSBundle.mainBundle()
let config = mainBundle.pathForResource(“Config”, ofType: “plist”)!
let configData = NSDictionary(contentsOfFile: config)!
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messageData.count ?? 0
}
self.window?.tintColor = self.tintColor(configData) ?? UIColor.redColor()
func tintColor(configData: NSDictionary) -> UIColor? {
guard let tintColor = configData["tint_color"] as? [String: CGFloat] else {
return nil
}
guard let r = tintColor["red"], g = tintColor["green"], b = tintColor["blue"] else {
return nil
}
return UIColor(red: r, green: g, blue: b, alpha: 1)