Skip to content

Instantly share code, notes, and snippets.

View vzsg's full-sized avatar

Zsolt Váradi vzsg

  • Budapest, Hungary
View GitHub Profile
@vzsg
vzsg / MeterTable.swift
Created April 20, 2022 21:44
MeterTableOC Swift port
import Foundation
struct MeterTable {
private let minDecibels: Double
private let scaleFactor: Double
private let table: [Double]
init(minDB: Double = -80, tableSize: Int = 400, root: Double = 2) {
self.minDecibels = minDB
@vzsg
vzsg / 1_JSONifyTag.swift
Last active March 11, 2023 18:49
Custom Leaf tag for printing anything in the context as JSON (Vapor 4)
import LeafKit
final class JSONifyTag: UnsafeUnescapedLeafTag {
func render(_ ctx: LeafContext) throws -> LeafData {
guard let param = ctx.parameters.first else {
throw "no parameter provided to JSONify"
}
return LeafData.string(param.jsonString)
}
@vzsg
vzsg / 1_DynamicQuery.swift
Last active December 10, 2021 14:51
Dynamic Query Support for Vapor 3
import Foundation
import Fluent
protocol DynamicQueryable {
static func dynamicMapping<DB: Database, QB: QueryBuilder<DB, Self>>() -> [String: (DynamicFilter, QB) throws -> QB]
}
protocol DynamicSortable {
static var dynamicFieldMapping: [String: FluentProperty] { get }
}
import Foundation
struct Property: Encodable {
let name: String
@AnyEncodable
var value: Encodable
}
@propertyWrapper struct AnyEncodable: Encodable {
let wrappedValue: Encodable
@vzsg
vzsg / IkigaJSON+Content.swift
Created October 9, 2020 01:18
Example on how to integrate IkigaJSON with Vapor 4
import Vapor
import IkigaJSON
extension IkigaJSONEncoder: ContentEncoder {
public func encode<E: Encodable>(
_ encodable: E,
to body: inout ByteBuffer,
headers: inout HTTPHeaders
) throws {
headers.contentType = .json
import Combine
import ObjectiveC
import UIKit
private var eventSubjectKey = "viewcontroller_lifecycle_subject"
private var swizzlingPerformed = false
private final class LifecycleSubjectBag: NSObject {
let viewDidLoadSubject = PassthroughSubject<Void, Never>()
let viewWillAppearSubject = PassthroughSubject<Bool, Never>()
@vzsg
vzsg / 1_UIViewController+TraitCollectionChangePublisher.swift
Created July 4, 2020 01:03
An experiment in method swizzling and Combine to expose traitCollectionDidChange as a Publisher
import Combine
import ObjectiveC
import UIKit
typealias TraitCollectionChange = (UITraitCollection, previous: UITraitCollection?)
private typealias TraitChangeSubject = PassthroughSubject<TraitCollectionChange, Never>
private var traitSubjectKey = "_voodoo_trait_subject"
private var swizzlingPerformed = false
@vzsg
vzsg / install-swift.sh
Last active October 16, 2023 18:51
Bash script to install Swift 5.2 on Ubuntu 18.04
#!/bin/bash
if [ -f "/opt/swift/bin/swift" ]; then
echo ====== Swift is already installed at /opt/swift, exiting!
exit 1
fi
for i in "$@"
do
case $i in
@vzsg
vzsg / 1_AppleAppLookup.swift
Last active December 10, 2021 14:54
App Store Rating Lookup for Vapor 3
import Foundation
import Vapor
struct AppInfo: Codable {
let averageUserRating: Double
let userRatingCount: Int
}
enum AppleAppLookupError: Error {
case notFound
@vzsg
vzsg / UUID+Version5.Swift
Created June 26, 2019 14:55
UUIDv5 using Vapor3's Crypto
// This is a direct port of https://github.com/download13/uuidv5 to Swift
import Foundation
import Crypto
public extension UUID {
enum Namespace {
case dns
case url
case oid