Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
set -e -u -o pipefail
# Open a new pull request in github from the current branch
open_pr() {
local base_branch=${1:-"development"}
local merge_organisation_opt=${2:-}
local browser="xdg-open"
local repo current_branch merge_organisation merge_branch url
@tokland
tokland / flatten-union-type1.ts
Last active September 14, 2020 22:14
Flatten a union into a plain type with a union of all keys (optional for non-common keys)
type AllKeysOfUnion<U> = U extends any ? keyof U : never;
type NonCommonKeysOfUnion<U> = Exclude<AllKeysOfUnion<U>, keyof U>;
type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
type UnionWithKeys<U, K extends keyof any> = U extends any
? { [Key in K]: Key extends keyof U ? U[Key] : never }
: never;
@tokland
tokland / game.js
Last active June 13, 2020 10:22 — forked from andreuestanyalonso/game.js
Simple Canvas game loop with key events
const canvas = document.getElementById("tutorial");
const context = canvas.getContext("2d");
function drawBackground() {
context.beginPath();
context.fillStyle = "rgb(200, 200, 200)";
context.rect(0, 0, 640, 480);
context.fill();
context.closePath();
}
@tokland
tokland / vector.ts
Created February 19, 2020 20:49
Typed arrays of fixed length with Typescript
/* Naturals using Peano numbers. See https://ostro.ws/2019/12/09/taking-types-too-far/ */
type Zero = { prev: never };
type Nat = { prev: Nat };
type Succ<N extends Nat> = { prev: N };
type Prev<N extends Nat> = N["prev"];
type Nats = {
0: Zero;
1: Succ<Zero>;
@tokland
tokland / tuples.ts
Last active February 17, 2020 20:43 — forked from willfrew/tuples.ts
Fun with tuple types in Typescript 3.0
type Length<T extends any[]> = T["length"];
type Head<T extends any[]> = T[0];
type Tail<T extends any[]> = ((...xs: T) => any) extends (x: any, ...t: infer T) => any ? T : never;
type Last<T extends any[]> = {
0: never;
1: T[0];
">1": Last<Tail<T>>;
}[Length<T> extends 0 ? 0 : Length<T> extends 1 ? 1 : ">1"];
@tokland
tokland / react-render-join.ts
Last active January 16, 2020 10:28
Join array of React nodes with a separator
function renderJoin(nodes: ReactNode[], separator: ReactNode): ReactNode {
return nodes.flatMap((node, idx) =>
idx < nodes.length - 1 ? [node, separator] : [node]
).map((node, idx) => <React.Fragment key={idx}>{node}</React.Fragment>);
}
// Usage
const myComponent: React.FC = props => {
return <div>{renderJoin([<p>item1</p>, <p>item2</p>], <br />)}</div>;
@tokland
tokland / tagged-unions-pattern-matching.ts
Last active January 22, 2020 22:23
Typescript: Pattern-matching for tagged unions (match only by discriminator tag)
export type Matcher<KindField extends string, Kind extends string, Obj, Result> = {
[K in Kind]: (obj: Extract<Obj, { [F in KindField]: K }>) => Result;
};
export function match<KindField extends string>(field: KindField) {
return function<
Obj extends { [K in KindField]: Kind },
Result,
Kind extends string = Obj[KindField]
>(obj: Obj, matcher: Matcher<KindField, Kind, Obj, Result>): Result {
@tokland
tokland / spreadsheet-cell-name-from-indexes.ts
Last active January 9, 2020 10:59
Get cell name from indexes for spreadsheet (Excel, ...)
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function idOf(idx: number): string {
const quotient = Math.floor(idx / letters.length);
const remainder = idx % letters.length;
return (quotient > 0 ? idOf(quotient - 1) : "") + letters[remainder];
}
function cell(colIdx: number, rowIdx: number): string {
return idOf(colIdx) + (rowIdx + 1).toString();
interface Metric {
timestamp: Date;
temperature: number;
pressure: number;
humidity: number;
rain: number;
sun: number;
windDirection: number;
windVelocity: number;
dew: number;
@tokland
tokland / SelectedPick.ts
Last active June 26, 2022 21:21
SelectorPick<Model, Selector>: An exhanced version of Pick<T, Keys> to select nested properties from T
/* SelectedPick<T, Selector>
An extended version of Pick<T, Key> where, instead of a union of keys,
you pass an object with the properties to get from a type. */
type Extends<T1, T2> = [T1] extends [T2] ? true : false
type OmitNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>
export type Selector<Model> = {
[Key in keyof Model]?: boolean | NestedSelectorValue<Model[Key]>