Skip to content

Instantly share code, notes, and snippets.

View ksmandersen's full-sized avatar
🏠
Working from home

Kristian Andersen ksmandersen

🏠
Working from home
View GitHub Profile
@ksmandersen
ksmandersen / PaddedTextField.swift
Last active March 27, 2023 03:33
Best way to inset text inside a UITextField
import UIKit
open class PaddedTextField: UITextField {
public var textInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) {
didSet { setNeedsDisplay() }
}
public override init(frame: CGRect) {
super.init(frame: frame)
}
@ksmandersen
ksmandersen / URI+Helpers.swift
Created January 24, 2018 09:41
Appending query parameters to URI's in Vapor
extension URI {
public func appendingQueryParameters(_ parameters: [String: String]) -> URI {
let allParameters = mergeParameters(queryParameters(fromQuery: query), rhs: parameters)
let newQuery = toQuery(parameters: allParameters)
return URI(scheme: scheme, userInfo: userInfo, hostname: hostname, port: port, path: path,
query: newQuery, fragment: fragment)
}
private func toQuery(parameters: [String: String]) -> String {
@ksmandersen
ksmandersen / install.sh
Created October 13, 2017 11:34
Install curl 7.56.0 with OpenSSL & HTTP2 support on Ubuntu 14.04.5 LTS
apt-get update
apt-get install -y build-essential curl
apt-get -y install git g++ make binutils autoconf automake autotools-dev libtool pkg-config zlib1g-dev libcunit1-dev libssl-dev libxml2-dev libev-dev libevent-dev libjansson-dev libjemalloc-dev cython python3-dev python-setuptools
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
automake
autoconf
./configure
make
@ksmandersen
ksmandersen / CreditCardFormatter.swift
Created October 11, 2022 08:53
How to format a credit card number with an (NS)Formatter
class CreditCardFormatter: Formatter {
override func string(for obj: Any?) -> String? {
guard let input = obj as? String else {
return nil
}
return formattedCardNumber(input: input)
}
override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
@ksmandersen
ksmandersen / duplicate_line_xcode.md
Last active September 27, 2021 07:49 — forked from Sidelobe/duplicate_line_xcode.md
Xcode - Duplicate Line key binding

Xcode line duplication (without overwriting your clipboard)

I find this shortcut extremely useful in Xcode: duplicate one or several lines without losing what you have on the clipboard.

Bind keys to duplicate lines in Xcode

  1. To add custom key bindings in Xcode, you have to edit this file (su privileges required): /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/Current/Resources/IDETextKeyBindingSet.plist

I add the following new key at the bottom:

import UIKit
import GenericViewKit
public class StateView: GenericView {
public enum State {
case Loading
case Loaded
case Error
}
@ksmandersen
ksmandersen / JSONFixture
Created April 8, 2014 16:42
Load JSON fixture files for your XCTests
@interface JSONFixture : NSObject
+ (id)fixtureDataWithName:(NSString *)fixtureName;
@end
@implementation JSONFixture
+ (id)fixtureDataWithName:(NSString *)fixtureName {
@ksmandersen
ksmandersen / StripeSignatureChecker.swift
Created December 3, 2019 09:06
Stripe Signature Validation for Webhooks
/// https://stripe.com/docs/webhooks/signatures#verify-manually
final class StripeSignatureChecker {
// 3 days
static let tolerance: TimeInterval = 3 * 24 * 60 * 60
static func isValid(req: Request) throws -> Bool {
guard let header = req.http.headers["Stripe-Signature"].first else {
throw StripeWebhookError.missingSignatureHeader
}
export const scrollToElement = (id, position) => {
const element = document.getElementById(id);
if (element) {
element.scrollIntoView({
behavior: 'smooth',
block: position,
});
}
};
@ksmandersen
ksmandersen / elasticsearch.sh
Last active November 12, 2019 09:44
Spin up an elastic search instance locally for development
docker run -d --restart always --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.4.2