Skip to content

Instantly share code, notes, and snippets.

@webstrand
webstrand / gist:46fb441e52a8663bced0
Last active August 29, 2015 14:17
Simple Rust Reverse Proxy
#![feature(io)]
#![feature(net)]
#![feature(core)]
extern crate hyper;
extern crate url;
static HOST: &'static str = "www.google.com";
macro_rules! ret_err(
($e:expr) => {{
@webstrand
webstrand / dsteam.pl
Created February 28, 2016 16:07
Find and run Steam games from dmenu
#!/usr/bin/env perl
# Copyright (c) 2016
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
@webstrand
webstrand / stream.js
Created August 5, 2016 14:56
Prototype pub-sub library
// Module streams
//
// This is effectively a pub-sub system with a few extra convenience methods.
// Fundamentally, observers are attached to a stream or property, and are called
// when the stream or property emits an event.
//
// By and large, most of the effort in this module is devoted to creating
// optimized streams and properties for passing around an arbitrary amount
// argument variables rather than wrapping up multi-part events into arrays.
//
@webstrand
webstrand / dmenu_path
Created January 19, 2017 05:52
dmenu_path supporting bash aliases
#!/bin/bash
cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"}
if [ -d "$cachedir" ]; then
cache=$cachedir/dmenu_run
else
cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~
fi
IFS=:
if stest -dqr -n "$cache" $PATH; then
compgen -ac | sort -u | tee "$cache"
@webstrand
webstrand / selectFields.ts
Created January 26, 2019 03:40
Guide on how to extract values from a map using keys from an array, but type-safely
type Thing<T> = {
thing: T;
}
const propertyTableFields = {
a: { thing: "A" },
b: { thing: 5 },
c: { thing: "C" },
};
@webstrand
webstrand / json.ts
Last active April 28, 2021 20:31
JSON and JSONable type for representing serializable and deserialized objects
import { Assignable, Exact } from "./check"; // https://gist.github.com/webstrand/b0f79ef6ed37839d1432466fe8ddbc1a
export type Json = JsonPrimitive | JsonMap | JsonList;
export type JsonProperty = undefined | JsonPrimitive | JsonMap | JsonList;
export type JsonPrimitive = null | string | number | boolean;
export type JsonMap = { [key: string]: JsonProperty };
export type JsonList = Json[];
export type ReadonlyJson = ReadonlyJsonPrimitive | ReadonlyJsonMap | ReadonlyJsonList;
export type ReadonlyJsonProperty = undefined | ReadonlyJsonPrimitive | ReadonlyJsonMap | ReadonlyJsonList;
@webstrand
webstrand / gist:0a7be6a60c01c5e619b49fa430203aa6
Created February 12, 2019 16:37
Typescript: An array of keys of some object extending some type.
type KeyofSubset<T, U> = Array<({ [P in keyof T]: T[P] extends U ? P : never })[keyof T]>;
@webstrand
webstrand / TypeFromSpec.ts
Created March 21, 2019 17:11
Derive a concrete type from a schematic type
type Spec = Atom | Dict | List;
type Atom = "bool" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "float32" | "float64" | "string" | "varuint" | "varint" | "buffer"
interface Dict { [key: string]: Spec }
interface List extends Array<Spec> { }
type TypeFromSpec<S extends Spec> = CondTypeFromAtom<S> | CondTypeFromDict<S> | CondTypeFromList<S>;
type TypeFromAtom<S extends Atom> = string;
type TypeFromDict<S extends Dict> = { [P in keyof S]: TypeFromSpec<S[P]> }
type TypeFromList<S extends List> = { [P in keyof S]: TypeFromSpec<S[P] extends Spec ? S[P] : never> }
type CondTypeFromAtom<S> = S extends Atom ? TypeFromAtom<S> : never;
@webstrand
webstrand / util-lists.ts
Last active May 12, 2019 19:44
Utility types for manipulating and traversing lists.
// Find the first item of a list.
export type Head<List extends any[]> = List['length'] extends 0 ? never : List[0];
// Find the last item of a list.
export type Last<List extends any[]> = number extends List['length'] ? List[1e+309] : _Last<List>;
type _Last<
Tuple extends any[],
_I extends number = Tail<Tuple>['length']
> = Tuple extends [] ? never : _I extends keyof Tuple ? Tuple[_I] : never;
@webstrand
webstrand / maptree.ts
Created March 29, 2019 16:06
Finitely recursive map types using arrays of values as keys. !!FRAGILE!! Don't use any[] for key type, expect heisenbugs.
export type MapTree<K extends readonly [unknown, ...unknown[]], V> = {
0: Map<K[number], V>,
1: ((..._: K) => any) extends ((_: infer Head, ...__: infer Tail) => any)
? Tail extends readonly [unknown, ...unknown[]]
? Map<Head, MapTree<Tail, V>>
: never
: never
}[K extends readonly [unknown] ? 0 : 1] & {
setKey(key: Readonly<K>, value: V): void;
getKey(key: Readonly<K>): V | undefined;