Skip to content

Instantly share code, notes, and snippets.

View kgn's full-sized avatar

David Keegan kgn

View GitHub Profile
@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"
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 / 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 = [];
@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 / Step 1.swift
Created August 19, 2019 03:31
Wedding Tables
enum Food: String {
case beef = "Beef"
case chicken = "Chicken"
case vegitarian = "Vegitarian"
case kids = "Kids"
}
struct Person {
let name: String
let food: Food
enum Food: String {
case beef = "Beef"
case chicken = "Chicken"
case vegitarian = "Vegitarian"
case kids = "Kids"
}
struct Person {
let name: String
let food: Food
Table {
Family("Mr. & Mrs. Smith") {
Person("Mr. John Smith", .beef)
Person("Mrs. Jane Smith", .chicken)
}
Person("Mr. Ed Ford", .chicken)
Person("Mrs. Amanda Doolittle", .vegitarian)
Family("Mr. & Mrs. Appleseed") {
Person("Mr. Jonny Appleseed", .chicken)
Person("Mrs. Jessica Appleseed", .vegitarian)
@_functionBuilder
struct PeopleBuilder {
static func buildBlock(_ people: Person...) -> [Person] {
return people
}
}
struct Table {
let people: [Person]
protocol People {
var people: [Person] { get }
}
@_functionBuilder
struct PeopleBuilder {
static func buildBlock(_ people: People...) -> [Person] {
return people.reduce([], {$0+$1.people})
}
}
@kgn
kgn / End.swift
Last active August 19, 2019 05:58
for (i, table) in tables.enumerated() {
print("Table #\(i+1)")
print(table.people
.map{"\($0.name),\($0.food.rawValue)"}
.joined(separator: "\n")
)
}