Skip to content

Instantly share code, notes, and snippets.

@colelawrence
colelawrence / windows.ahk
Last active October 14, 2022 16:17
Making crucial keybindings similar on Windows to expected on personal macOS
#SingleInstance Force
; Debugging
; https://www.autohotkey.com/scite4ahk/pages/debugger.htm
; Map Capslock to control (macOS is Capslock as Command, so most keybindings feel similar)
CapsLock::RControl
; Left Ctrl Tab -> Left Alt Tab
<^Tab::>!Tab
@colelawrence
colelawrence / WeakCache.ts
Created September 6, 2022 12:48
When you needed something a little higher dimension than just a WeakMap.
/** Memory-volatile multi-key cache with a root weak key */
export class WeakCache {
private wm = new WeakMap<object, Map<string | number, unknown>>();
/** It's very important to not call this with the same keys and expect a different result. */
getOrPut<R>(weakKey: object, keys: CacheKeys, fn: () => R): R {
let found = this.wm.get(weakKey);
const innerKey = typeof keys === "object" ? JSON.stringify(keys) : keys;
if (!found) {
const computed = fn();
found = new Map([[innerKey, computed]]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type $IntentionalAny = any;
/**
* This special type can help generate pattern matchers for you!
* Just use it as so:
* // enum-ts
* type Result<Ok, Err> = Enum<{
* Ok: Ok,
* Err: Err,
@colelawrence
colelawrence / createTrpcDurableObject.ts
Last active March 27, 2024 22:10
Create strongly typed Durable Objects with TRPC wrappers (requires trpc@10)
import { createTRPCProxyClient, httpLink } from "@trpc/client";
import { AnyRouter, initTRPC, MaybePromise, Router } from "@trpc/server";
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
import type { ParserWithInputOutput, ParserWithoutInput } from "@trpc/server/dist/core/parser";
import type { AnyRouterDef } from "@trpc/server/dist/core/router";
import type { ResponseMetaFn } from "@trpc/server/dist/http/internals/types";
import { getParseFn } from "./getParseFn";
export type TrpcDurableObjectRouter<Router extends AnyRouter> = {
router: Router,
@colelawrence
colelawrence / slack-download.py
Last active February 2, 2022 18:31 — forked from marnitto/slack-download.py
Slack file downloader from export archive (2021)
# -*- coding: utf-8 -*-
#
# Slack file downloader from export archive
#
# Requirements:
# Python 2 (or Python3 if you can use six)
#
# How to use:
# 1. Log in as admin, export your chat logs, and download archive.
# 2. Unarchive archive to directory (ex. TeamName export Apr 24 2016)
@colelawrence
colelawrence / pop-os-keyboard.json
Last active September 7, 2021 03:27
VSCode popOS settings
// Place your key bindings in this file to override the defaultsauto[]
[
{
"key": "ctrl+right",
"command": "-cursorWordEndRight",
"when": "textInputFocus"
},
{
"key": "ctrl+shift+right",
"command": "-cursorWordEndRightSelect",
#!/usr/bin/env bash
set -euxo pipefail
database_url="${1}"
dump_path="${2:-schema}"
schemas=("app_public" "app_hidden" "app_private" "public")
find "$dump_path" -name '*.sql' -delete
JSON.stringify(
mappedSchema,
(_key, val) => {
if (val != null && typeof val === "object") {
return Object.fromEntries(
Object.keys(val)
.sort()
.map((key) => [key, val[key]]),
);
} else {
@colelawrence
colelawrence / znum.ts
Last active March 26, 2021 22:23
enum-ts functionality for zod types. WIP
import z from "zod";
export const zo = z.object.bind(z);
export const zu = z.union.bind(z);
export interface Znum<
T extends Record<string, any>,
TT = {
[P in keyof T]: Record<P, T[P]>;
}[keyof T]
@colelawrence
colelawrence / open-promise.ts
Last active September 4, 2023 15:42
Create a PromiseLike which can be resolved by calling `openPromise1.resolve(value)`
export class OpenPromise<T> implements PromiseLike<T> {
private inner: Promise<T>
private resolved?: [T]
public resolve = (item: T) => {
this.resolved = [item]
}
private rejected?: [any]
public reject = (error: any) => {
this.rejected = [error]
}