Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / crap.js
Created September 20, 2019 22:51
const hyperx = require('hyperx')
const hx = hyperx((componentName, properties, children) => {
return ['createElement', componentName, properties, children]
}, (children) => {
return ['createFragment', children]
})
class Header {
async fetch (ctx) {
@myobie
myobie / colorize.js
Last active August 2, 2019 16:42
How the color changing works on nathanherald.com
function sorter () { return 0.5 - Math.random() }
var styles = [ 'blaze', 'sunshine', 'dusk', 'blood', 'puce' ]
function colorize () {
var style = styles.sort(sorter)[0]
document.body.className = style
}
colorize()
setInterval(colorize, 10000)
@myobie
myobie / URLAppendingQueryItems.swift
Created July 17, 2019 14:17
Append query items just as easily as path components
extension URL {
func appendingQueryItems(_ items: [String: String]) -> URL {
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
fatalError()
}
var query = components.queryItems ?? []
items.forEach { (name, value) in
query.append(URLQueryItem(name: name, value: value))
@myobie
myobie / ExampleSimplePublisher.swift
Last active April 7, 2020 11:33
Make any class a super simple publisher with publish() and complete() functions
class Names: SimplePublisher {
typealias Output = Result<String, Error>
var subscriptions = [SimpleSubscription<Output, Failure>]()
let names = ["Alice", "Bob", "Claris", "Doug"]
shoutName() {
if let name = names.randomElement() {
publish(.success(name))
} else {
assertionFailure("There are def some names in there...")
@myobie
myobie / forever.swift
Last active July 7, 2019 12:57
Subscribers.Sink sucks, so this will receive forever until completion
import Foundation
import Combine
extension Subscribers {
final public class Forever<Upstream: Publisher>: Subscriber, Cancellable, CustomStringConvertible {
public typealias Input = Upstream.Output
public typealias Failure = Upstream.Failure
private var subscription: Subscription? = nil
private let receiveCompletion: (Subscribers.Completion<Failure>) -> Void
@myobie
myobie / slugify.sql
Created June 1, 2019 16:19 — forked from kez/slugify.sql
Generating Slugs in Postgres
CREATE EXTENSION IF NOT EXISTS "unaccent"
CREATE OR REPLACE FUNCTION slugify("value" TEXT)
RETURNS TEXT AS $$
-- removes accents (diacritic signs) from a given string --
WITH "unaccented" AS (
SELECT unaccent("value") AS "value"
),
-- lowercases the string
"lowercase" AS (
@myobie
myobie / server.sh
Created November 17, 2018 20:35
Always use heroku local (foreman) for the server
#!/bin/bash
which heroku > /dev/null
if [[ "$?" != "0" ]]; then
echo "You need to brew install heroku"
exit 1
fi
heroku local
@myobie
myobie / rel_config.exs
Last active October 10, 2018 21:17
Automatically build and digest assets for a phoenix application when building an elixir release with distillery
# rel/config.exs
# ...
environment :prod do
# ...
plugin(Example.Phoenix)
end
# ...
@myobie
myobie / timing.js
Created August 24, 2018 10:26
A nicer API for performance time measurements in the browser
export function create (name) {
const nameStart = `${name} start`
window.performance.mark(nameStart)
return {
log: () => { log(name) },
end: () => { end(name) }
}
}
@myobie
myobie / image.html
Created August 14, 2018 11:04
Image short tag that supports retina images, page resources, and falls back to plain urls for Hugo websites
{{- $src := .Get "src" }}
{{- $s := newScratch }}
{{- if eq (len (findRE "^/" $src)) 0 }}
{{- $s.Set "res" ($.Page.Resources.GetMatch $src) }}
{{- with ($s.Get "res") }}
{{- $s.Set "src" .Permalink }}
{{- if ne (len (findRE "@2x" .Permalink)) 0 }}
{{- $s.Set "width" (div .Width 2) }}
{{- $s.Set "height" (div .Height 2) }}