Skip to content

Instantly share code, notes, and snippets.

View JohnSundell's full-sized avatar

John Sundell JohnSundell

View GitHub Profile
@JohnSundell
JohnSundell / AnyOf.swift
Created August 21, 2017 21:23
A way to easily compare a given value against an array of candidates
import Foundation
struct EquatableValueSequence<T: Equatable> {
static func ==(lhs: EquatableValueSequence<T>, rhs: T) -> Bool {
return lhs.values.contains(rhs)
}
static func ==(lhs: T, rhs: EquatableValueSequence<T>) -> Bool {
return rhs == lhs
}
@JohnSundell
JohnSundell / EnumSequence.swift
Created August 10, 2017 15:47
A simple way to iterate over linear Int-based enums
struct EnumSequence<T: RawRepresentable> where T.RawValue == Int {}
extension EnumSequence: Sequence {
func makeIterator() -> AnyIterator<T> {
var rawValue = 0
return AnyIterator {
let nextCase = T(rawValue: rawValue)
rawValue += 1
return nextCase
@JohnSundell
JohnSundell / xcode-switch.sh
Created June 17, 2017 13:29
A script that switches between the App Store & beta versions of Xcode
#!/usr/bin/env bash
# This script switches between the App Store version of Xcode and the beta
# Install it by copying it to /usr/local/bin/xcode-switch and running 'chmod +x' on it (to make it executable)
# Then run it using 'sudo xcode-switch'
if [ "$EUID" -ne 0 ]; then
echo "xcode-select requires you to run this script as root; 'sudo xcode-switch'"
exit
fi
@JohnSundell
JohnSundell / DictionaryMap.swift
Created June 5, 2017 10:21
A map method for Dictionary that lets you easily transform its keys and values into other types
extension Dictionary {
func map<K: Hashable, V>(_ transform: (Element) throws -> (key: K, value: V)) rethrows -> [K : V] {
var transformed = [K : V]()
for pair in self {
let transformedPair = try transform(pair)
transformed[transformedPair.key] = transformedPair.value
}
return transformed
@JohnSundell
JohnSundell / GenerateLicenses.swift
Last active April 19, 2019 18:28
A Swift script that we use at Hyper to generate licenses for used 3rd party libraries, using https://github.com/mono0926/LicensePlist
import Foundation
import Files // marathon:https://github.com/JohnSundell/Files.git
import ShellOut // marathon:https://github.com/JohnSundell/ShellOut.git
// MARK: - Generation
guard let settingsBundle = try? Folder.current.subfolder(named: "Settings.bundle") else {
print("Create a Settings.bundle in Xcode, then re-run this script")
exit(1)
}
@JohnSundell
JohnSundell / Marathonfile
Created April 5, 2017 19:51
A script that verifies that all tests within a folder structure has at least one assert
git@github.com:JohnSundell/Files.git
@JohnSundell
JohnSundell / Namespacing-Swift-code-with-nested-types-5.swift
Created April 1, 2017 21:39
Code sample #6 from my Medium post "Namespacing Swift code with nested types"
struct Post {
typealias TextFormatter = PostTextFormatter
let id: Int
let author: User
let title: String
let text: String
}
class PostTextFormatter {
@JohnSundell
JohnSundell / Namespacing-Swift-code-with-nested-types-4.swift
Created April 1, 2017 21:38
Code sample #5 from my Medium post "Namespacing Swift code with nested types"
struct Post {
let id: Int
let author: User
let title: String
let text: String
}
extension Post {
class TextFormatter {
private let options: Set<Option>
@JohnSundell
JohnSundell / Namespacing-Swift-code-with-nested-types-3.swift
Created April 1, 2017 21:37
Code sample #4 from my Medium post "Namespacing Swift code with nested types"
struct Post {
let id: Int
let author: User
let title: String
let text: String
// MARK: - TextFormatter
class TextFormatter {
private let options: Set<Option>
@JohnSundell
JohnSundell / Namespacing-Swift-code-with-nested-types-2.swift
Created April 1, 2017 21:36
Code sample #3 from my Medium post "Namespacing Swift code with nested types"
let formatter = Post.TextFormatter(options: [.highlightLinks])
let text = formatter.formatText(for: post)