Skip to content

Instantly share code, notes, and snippets.

View MihaelIsaev's full-sized avatar
⌨️
developing Swift for Web

Mikhail Isaev aka iMike MihaelIsaev

⌨️
developing Swift for Web
View GitHub Profile
@MihaelIsaev
MihaelIsaev / CountryCodes.json
Last active August 29, 2015 14:20 — forked from aynurin/CountryCodes.json
Remove spaces
{
"countries":[
{
"name":"Afghanistan",
"phoneCode":"93",
"iso":"AF"
},
{
"name":"Albania",
"phoneCode":"355",
@MihaelIsaev
MihaelIsaev / OffsetStringFromGMT.swift
Created May 3, 2015 14:49
iOS Swift extension to get GMT time offset string
extension NSTimeZone {
func offsetStringFromGMT() -> String {
var offsetSeconds = secondsFromGMT
var offsetString = "+00:00"
var offsetSymbol = "+"
var offsetHoursLeadString = "0"
var offsetMinutesLeadString = "0"
if offsetSeconds < 0 {
offsetSymbol = "-"
offsetSeconds = (offsetSeconds * -1)
@MihaelIsaev
MihaelIsaev / throttle-SourceKitService
Created February 13, 2017 16:15 — forked from pyrtsa/throttle-SourceKitService
Script to keep SourceKitService from eating up all OS resources
#!/bin/bash
limit="${1-10000000}";
echo "Keeping SourceKitService below $limit KiB of virtual memory."
echo "Hit ^C to quit."
while true; do
sleep 1;
p=`pgrep ^SourceKitService$`
if [ -n "$p" ]; then
vsz=`ps -o vsz -p "$p" | tail -1`
@MihaelIsaev
MihaelIsaev / Request+PromiseWrapper.swift
Created April 28, 2018 15:44
Request+Promise extension for Vapor 3. May be handy if you often create promises in your controllers
import Foundation
import Vapor
extension Request {
func promise<T>(_ asyncCode: @escaping (() throws ->(T))) -> Future<T> where T: ResponseEncodable {
let promise = eventLoop.newPromise(T.self)
DispatchQueue.global().async {
do {
promise.succeed(result: try asyncCode())
@MihaelIsaev
MihaelIsaev / PostgreSQLQueryCodableExample.swift
Created May 29, 2018 01:13
Vapor3 PostgreSQL extension to decode [[PostgreSQLColumn: PostgreSQLData]] with Codable struct
import PostgreSQL
typealias PostgreSQLQueryRow = [PostgreSQLColumn: PostgreSQLData]
extension Dictionary where Key == PostgreSQLColumn, Value == PostgreSQLData {
func decode<T>(_ key: String) throws -> T where T: PostgreSQLDataConvertible {
guard let v = try firstValue(forColumn: key)?.decode(T.self) else {
throw PostgreSQLError(identifier: "decodingError", reason: "Unable to decode \"\(key)\" column ", source: .capture())
}
return v
@MihaelIsaev
MihaelIsaev / String+SnakeCase.swift
Created July 22, 2018 19:26
Camel case to snake case in Swift
extension String {
var snakeCased: String {
var newString: String = ""
let upperCase = CharacterSet.uppercaseLetters
for scalar in self.unicodeScalars {
if upperCase.contains(scalar) {
if newString.count > 0 {
newString.append("_")
}
let character = Character(scalar)
@MihaelIsaev
MihaelIsaev / Router+AnyReponseArray.swift
Last active July 22, 2018 20:32
Attempt to handle routes with [AnyResponse] for Vapor3
extension Router {
@discardableResult
func get(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> [AnyResponse]) -> Route<Responder>
{
return _on(.GET, at: path.convertToPathComponents(), use: closure)
}
@discardableResult
func post(_ path: PathComponentsRepresentable..., use closure: @escaping (Request) throws -> [AnyResponse]) -> Route<Responder>
{
CodyFire.shared.fillHeaders = {
guard let apiToken = LocalAuthStorage.savedToken else { return [:] }
return ["Authorization": "Bearer \(apiToken)"]
}
import Foundation
extension TimeZone {
func GMTOffset() -> String {
var offsetSeconds = secondsFromGMT()
var offsetString = "+00:00"
var offsetSymbol = "+"
var offsetHoursLeadString = "0"
var offsetMinutesLeadString = "0"
if offsetSeconds < 0 {
@MihaelIsaev
MihaelIsaev / ImageMagick.swift
Created February 9, 2019 17:22
Little ImageMagick wrapper for Vapor Swift
//
// ImageMagick.swift
// App
//
// Created by Mihael Isaev on 01.08.2018.
//
import Foundation
import Vapor
import Core