Skip to content

Instantly share code, notes, and snippets.

View JanGorman's full-sized avatar
:shipit:

Jan Gorman JanGorman

:shipit:
View GitHub Profile
@JanGorman
JanGorman / dedup.swift
Created October 25, 2022 09:07
Deduplicate array while keeping sort order
func deduplicated() -> [Element] {
var set = Set<String>()
// Whatever property or the entire thing if Hashable
return filter { set.insert($0.name).inserted }
}
@JanGorman
JanGorman / big-branch.sh
Created August 2, 2021 08:45
Find that big branch
git for-each-ref --format='%(refname)' |
while read branch
do
size=$(git rev-list --disk-usage --objects HEAD..$branch)
echo "$size $branch"
done |
sort -n
// https://developer.apple.com/documentation/accelerate/compressing_and_decompressing_data_with_input_and_output_filters
import Compression
let sourceString = """
Lorem ipsum dolor sit amet consectetur adipiscing elit mi
nibh ornare proin blandit diam ridiculus, faucibus mus
dui eu vehicula nam donec dictumst sed vivamus bibendum
aliquet efficitur. Felis imperdiet sodales dictum morbi
vivamus augue dis duis aliquet velit ullamcorper porttitor,
@JanGorman
JanGorman / tuple_sort.swift
Created September 9, 2020 19:54
Nice trick to sort tuples by selected fields
let foos: [(id: Int, bar: Int, baz: Int)] = [
(100, 100, 100),
(2, 100, 100),
(3, 1000, 100),
(1, 101, 100),
(1, 100, 100),
]
// The comparison will first compare the .id fields and
// only if they are equal will it perform the comparison on the .bar field
@JanGorman
JanGorman / JSONStringInterpolation.swift
Created May 12, 2020 08:29
JSON Swift String Interpolation
extension String.StringInterpolation {
mutating func appendInterpolation(json JSONData: Data) {
guard let JSONObject = try? JSONSerialization.jsonObject(with: JSONData, options: []),
let jsonData = try? JSONSerialization.data(withJSONObject: JSONObject, options: .prettyPrinted) else {
appendInterpolation("Invalid JSON data")
return
}
appendInterpolation("\n\(String(decoding: jsonData, as: UTF8.self))")
}
}
extension View {
func debug() -> Self {
print(Mirror(reflecting: self).subjectType)
return self
}
}
@JanGorman
JanGorman / enumerate_alternative.swift
Created January 18, 2020 10:11
Alternative to using .enumerated() and then having to add + 1 to the index for lists
struct Person {
let name: String
init(_ name: String) {
self.name = name
}
}
let people: [Person] = [.init("De Konig"), .init("Pollock"), .init("Rothko"), .init("Kandinsky")]
script:
- swift package generate-xcodeproj
- xcodebuild test -scheme {YourProject}-Package -destination \
platform="macOS" -enableCodeCoverage YES
after_success:
- bash <(curl -s https://codecov.io/bash)
$ swift package generate-xcodeproj
$ xcodebuild test -scheme {YourProject}-Package -destination \
platform="macOS" -enableCodeCoverage YES
extension URL: ExpressibleByStringLiteral {
public init(extendedGraphemeClusterLiteral value: String) {
self = URL(string: value)!
}
public init(stringLiteral value: String) {
self = URL(string: value)!
}
}