Skip to content

Instantly share code, notes, and snippets.

@webstrand
webstrand / bind-string.ts
Last active April 3, 2021 22:01
Validate an IPv4 binding string
type Octet =
| `${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
| `${1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
| `1${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
| `2${0 | 1 | 2 | 3 | 4 }${0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9}`
| `2${5 }${0 | 1 | 2 | 3 | 4 | 5}`
type Protocol = 'tcp' | 'tcps';
type QuasiIPv4 = `${bigint}.${bigint}.${bigint}.${bigint}`
type QuasiBindingString = `${Protocol}://${'*' | QuasiIPv4}:${bigint}` | `${'ipc' | 'ipcs'}://${string}`;
@webstrand
webstrand / object-path.ts
Last active April 6, 2021 13:04
Recursively generate all of the paths, like "x.z.a.b.c" inside of an object. Includes bounded recursion variant
// HIGHLY inefficient, since it generates the entire union ahead of time
// https://gist.github.com/webstrand/5a5df9e63d5646f045a8871f11cb5b6f#file-generic-validator-object-paths-ts
// is far more efficient, though it only works inside of a function to _validate_
// paths provided by the user at the callsite.
interface I {
x: {
z?: {
a?: {
b?: {
@webstrand
webstrand / sel_iterate.py
Created April 16, 2021 15:12
Sublime Text 3 Plugin for iterating through selected regions
import sublime
import sublime_plugin
import weakref
from weakref import WeakValueDictionary
view_selection_iterators = WeakValueDictionary()
class ViewSelectionIterator(sublime_plugin.ViewEventListener):
current_item = None
@webstrand
webstrand / union-to-tuple.ts
Created June 13, 2021 14:35
Union to Tuple in one type
type U2T<
U,
UF extends () => U = U extends U ? () => U : never,
IF = (UF extends UF ? (x: UF) => any : never) extends (x: infer IF) => any ? IF : never
> =
IF extends () => infer W ? [...U2T<Exclude<U, W>>, W] : [];
@webstrand
webstrand / newpgroup.c
Last active July 11, 2021 17:54
Run the command in a new process group and forward any signals to the new process group leader.
#!/usr/bin/env scriptisto
// scriptisto-begin
// script_src: main.c
// build_cmd: gcc -O2 -Wall -Werror main.c -o ./script
// scriptisto-end
#define _GNU_SOURCE 1
#include <stdlib.h>
@webstrand
webstrand / object_id_64.sql
Last active July 18, 2021 21:46
Generate a int64/bigint unique id using 41 bits of timestamp and 10 bits of sequence.
# https://instagram-engineering.com/sharding-ids-at-instagram-1cf5a71e5a5c
CREATE SEQUENCE IF NOT EXISTS object_id_seq AS smallint
MINVALUE 0
MAXVALUE 1023
CYCLE;
CREATE OR REPLACE FUNCTION object_id_epoch()
RETURNS bigint
LANGUAGE sql
@webstrand
webstrand / utils.pl
Created August 24, 2021 21:38
Utilities for I3
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use v5.26;
use experimental 'signatures';
use AnyEvent::I3 qw(TYPE_GET_WORKSPACES TYPE_COMMAND i3);
my $i3 = i3();
@webstrand
webstrand / any-abort-signal.ts
Created September 21, 2021 13:41
Compose multiple AbortSignals together, so that the output signal is aborted if any of the input signals abort.
function anySignal(...signals: (AbortSignal | undefined | null)[]): AbortController {
const controller = new AbortController();
for(const signal of signals) {
if(signal == null) continue;
if(signal.aborted) {
handleAbort();
break;
}
signal.addEventListener("abort", handleAbort);
export type Split<T extends string, Acc extends string[] = [], Alt extends string = never> =
T extends `${infer U}${infer V}`
? {} extends Record<U, 0>
? Split<V, U extends `${number | bigint}` ? [...Acc, string] : Acc, string>
: Split<V, [...Acc, U | Alt], Alt>
: T extends ""
? [Alt] extends [never]
? Acc
: [...Acc, ...(Alt | undefined)[]]
: T extends `${number | bigint}` // `${number | bigint}` is at least 1 character
@webstrand
webstrand / flatten-unflatten.ts
Last active October 25, 2021 17:59
Flatten or unflatten object types `{ "a.b.c": 1 }` to `{ a: { b: { c: 1 } } }`
type Expand<T> = T extends T ? { [P in keyof T]: T[P] } : never;
type UnionToIntersection<U> = (U extends U ? ((x: U) => any) : never) extends (x: infer I) => any ? I : never;
type Flatten<T, Z extends string = ""> = T extends object
? Expand<UnionToIntersection<{
[P in keyof T]: P extends string | number
? T[P] extends object
? Flatten<T[P], `${Z}${P}.`>
: { [Q in `${Z}${P}`]: T[P] }
: never
}[keyof T & (T extends readonly unknown[] ? number : unknown)]>>