Skip to content

Instantly share code, notes, and snippets.

@webstrand
webstrand / gist:2df39c37048a3a114ba3b5b0f40817d1
Created September 30, 2020 20:01 — forked from gsainio/gist:6322375
Sample perl code to use service accounts and oauth2 with Google's Admin SDK API.
#!/usr/public/bin/perl -w
use strict;
use JSON;
use JSON::WebToken;
use LWP::UserAgent;
use HTML::Entities;
my $private_key_string = q[-----BEGIN PRIVATE KEY-----
@webstrand
webstrand / 000~nonempty-validator.ts
Last active May 27, 2025 21:28
Examples of the <T>(foo: Validator<T>) pattern in typescript
/******************************************************************************
* Implementation of `Nonempty` validator which checks that the provided type
* has at least one defined property, excluding `{}`.
******************************************************************************/
type Nonempty<T extends { [key: string]: any }> = { [P in keyof T]: T }[keyof T];
declare function wantsNonempty<T extends { [key: string]: any }>(x: Nonempty<T>): true;
wantsNonempty({ x: 1 });
wantsNonempty({}); // error expected
@webstrand
webstrand / preprocessed.sql.in
Created October 27, 2020 17:05
For use with gpp
#!/usr/bin/gpp
#mode comment "/*" "*/"
/******************************************************************************/
/* ALL CODE GENERATED BY THIS FILE SHOULD RETAIN COMMENTS, NOTES, AND */
/* FORMATTING SO THAT IN THE FUTURE IF THE PREPROCESSOR IS LOST, THE CODE IS */
/* PRESERVED. */
/******************************************************************************/
#mode string QQQ "<<EOL\n" "\nEOL"
#define e(str) str
@webstrand
webstrand / git-inlined-repositories.bash
Created November 5, 2020 19:15
A tutorial script explaining how to safely inline remote repositories.
#!/bin/bash
################################################################################
# Introduction
################################################################################
# This is an attempt at explaining how to vendor git repositories by storing
# their _entire_ commit history inside of the local repository. By vendoring
# dependencies using this technique, the remote repository can entirely
# recreated from the local repository if the original were to be lost.
# Additionally, clones of the local repository also satisfy that property.
#
@webstrand
webstrand / select-tuple-nth.ts
Created November 16, 2020 19:02
Select the Nth member of a tuple, preserving optional and labels.
type Stump<N extends number, U extends readonly unknown[] = [], V extends readonly unknown[] = []> = N extends N ? number extends N ? readonly unknown[] : U["length"] extends N ? V : Stump<N, readonly [unknown, ...U], [unknown?, ...V]> : never;
type Truncate<T extends readonly unknown[]> = T extends [unknown?, ...infer U] ? T extends [...infer X, ...U] ? X : never : never;
type Select<T extends readonly unknown[], I extends keyof T & number> = Truncate<T extends readonly [...Stump<I>, ...infer U] ? U : []>;
type a = Select<[1,2,3], 1> // [2]
type b = Select<number[], 1> // [number?] // still doesn't work
type c = Select<[1,2?,3?], 1> // [2?]
type d = Select<[ foo: 1, bar?: 2 ], 1> // [ bar?: 2 ]
@webstrand
webstrand / check.ts
Last active November 21, 2021 19:49
Functions and type aliases for unit testing compile-time types.
/**
* Type alias resolves to `True` if and only if `U` is the same as `V`, otherwise it resolves to `False`.
* @typeparam U - An arbitrary type
* @typeparam V - An arbitrary type
* @typeparam True - Production when `U` is the same as `V`
* @typeparam False - Production when `U` is not the same as `V`
*/
export type Exact<U, V, True = true, False = false> =
{ <_>(): _ extends U ? 1 : 0 } extends { <_>(): _ extends V ? 1 : 0 }
? True
@webstrand
webstrand / prefixes.ts
Last active September 25, 2021 18:56
Definition of type alias Prefixes<T> which generates all of the prefixes for some tuple T. Including unit tests.
/**
* Get the union of all prefixes of some tuple. Finite, optional, and variadic tuples are
* all supported. Labels are only preserved for finite tuples without optional members.
*/
type Prefixes<T extends readonly unknown[]> =
T extends { length: infer X } & { length: infer Y }
? (X extends unknown ? Y extends X ? 0 : 1 : never) extends 0
? number extends T["length"] // If the tuple is variadic
? PrefixesNoLabels<T> // we bail out and use the variadic supporting type
@webstrand
webstrand / fizz-buzz.js
Last active December 3, 2020 23:05
Code-golfed FizzBuzz
// [...Array(101)] : is the shortest way to generate a range from [0..100]
// .map((_,n)=> : The array is filled with undefined, so we use the index instead.
// "Fizz".slice(n%3&&4): (n%3) is truth-y for all non-multiples of 3. (n%3)&&4 is either 0 or 4.
// ||n : "Fizz".slice(4)+"Buzz".slice(4) is "", which is false-y, so we replace it with n
// .find : .map returns an array, .find does not.
[...Array(101)].map((_,n)=>"Fizz".slice(n%3&&4)+"Buzz".slice(n%5&&4)||n).find(s=>console.log(s))
// An interesting, but longer variation:
// n%3&&1-n%5&&4 : returns either 4 for Buzz or 0 for Fizz and FizzBuzz. (other values are excluded by the length arg)
// (!(n%3)+!(n%5))*4 : returns 0, 4, or 8 for none, Fizz or Buzz, or FizzBuzz respectively.
@webstrand
webstrand / nginx.conf
Last active July 31, 2022 03:24
Run NGINX in the current directory
#!/usr/bin/env -S nginx -e /dev/stderr -p . -c
# Run an NGINX instance serving the current directory on ports 8080 and 8443
# (when configured). Execute one of the following commands in the terminal.
# - start-nodaemon: ./nginx.conf -g 'daemon off;'
# - start: ./nginx.conf
# - stop: ./nginx.conf -s stop
# - reload: ./nginx.conf -s reload
pid .nginx/nginx.pid;
events {}
@webstrand
webstrand / curry.ts
Last active June 3, 2021 20:12
Example of function currying
// See also https://gist.github.com/webstrand/bac2bc752e3f6b22892a155ed1efa536 for definition of Prefixes<T>
/**
* Get the union of all prefixes of some tuple. Finite, optional, and variadic tuples are
* all supported. Labels are only preserved for finite tuples without optional members.
*/
type Prefixes<T extends readonly unknown[]> =
T extends { length: infer X } & { length: infer Y }
? (X extends unknown ? Y extends X ? 0 : 1 : never) extends 0
? number extends T["length"] // If the tuple is variadic