Skip to content

Instantly share code, notes, and snippets.

View betafcc's full-sized avatar
🎯
Focusing

Brendon betafcc

🎯
Focusing
View GitHub Profile
@betafcc
betafcc / reveal.ts
Last active February 6, 2025 13:07
Expand type aliases from a typescript file
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import JSON5 from 'json5'
import ts from 'typescript'
import * as prettier from 'prettier'
/**
* reveal all top level `type` declarations from a file
*/
export const reveal = async (sourcePath: string) => {
@betafcc
betafcc / Permutation.d.ts
Last active October 29, 2024 15:39
Typescript Union Permutation, count of keys in an object, count of cases in an Union
/**
* @example
* type P = Permutation<1 | 2 | 3>
* // [1, 2, 3] | [1, 3, 2] | [2, 1, 3] | [2, 3, 1] | [3, 1, 2] | [3, 2, 1]
*/
export type Permutation<U, T = U> = [U] extends [never]
? []
: T extends unknown
? [T, ...Permutation<Exclude<U, T>>]
: never
import { DocumentNode } from 'graphql'
import {
gql,
QueryHookOptions,
QueryResult,
TypedDocumentNode,
useQuery,
} from '@apollo/client'
export type Query<TData, TVariables> =
@betafcc
betafcc / touchbar-select.js
Created May 8, 2021 14:05
Opens a select in the touchbar
#!/usr/bin/env electron
const { app, BrowserWindow, TouchBar } = require("electron")
app.whenReady().then(() => {
const window = new BrowserWindow({
titleBarStyle: "hiddenInset",
width: 0,
height: 0,
})
window.loadURL("about:blank")
@betafcc
betafcc / csv-mysql.bash
Last active May 7, 2021 03:33
Easily uploads a csv to mysql via stdin
#!/usr/bin/env bash
set -euo pipefail
SEP="${SEP:=,}"
TER="${TER:=\n}"
TABLE="${1}"; shift # first positional argument is the tablename
CMD="${@}" # the rest is the mysql command
# examples:
# cat myfile.csv | csv-mysql mytable mysql -Dtest -uroot --ppassword
@betafcc
betafcc / convert_to_enum.mysql
Last active May 7, 2021 03:30
Automatically converts a column to enum type, getting the values from the column itself
-- usage: CALL convert_to_enum('my_table', 'my_column');
DELIMITER $$
CREATE PROCEDURE `convert_to_enum`(IN tablename TEXT, IN colname TEXT)
BEGIN
SET @temp = CONCAT(
"SELECT CONCAT(\"ENUM('\", GROUP_CONCAT(DISTINCT ",
colname,
" ORDER BY ",
colname,
" SEPARATOR \"', '\"), \"')\") INTO @temp FROM ",
@betafcc
betafcc / UnionToTuple.ts
Last active February 1, 2020 10:36
Typescript Union to all Union of Tuples possible with it's elements
// UnionToTuple<1 | 2 | 3> -> [3, 2, 1] | [2, 3, 1] | [3, 1, 2] | [1, 3, 2] | [2, 1, 3] | [1, 2, 3]
type UnionToTuple<U> = _UnionToTuple<U, []>[0]
type _UnionToTuple<U, L extends Array<U>> = Done<U, L> extends true
? [L] // boxing prevents circulatory reference
: [_UnionToTuple<U, ConsDistinct<U, L>>[0]]
// ConsDistinct<(1 | 2), ([1] | [2] | [3])> -> [1, 2] | [1, 3] | [2, 1] | [2, 3]
type ConsDistinct<A, L extends Array<unknown>> = L extends any
? A extends any
@betafcc
betafcc / util.ts
Last active January 11, 2020 13:36
Interesting TypeScript utilities
// Spread<{x: number} & {y: string}> -> {x: number, y: string}
export type Spread<T> = ReturnType<() => { [P in keyof T]: T[P] }>
// Merge<{x: number} | {y: string}> -> {x: number} & {y: string}
export type Merge<T> = (T extends any ? (k: T) => void : never) extends (k: infer U) => void
? U
: never
// Values<{x: number, y: string}> -> number | string
export type Values<T> = T[keyof T]
@betafcc
betafcc / common_path.bash
Created March 14, 2019 09:45
Pure bash equivalent to python's os.path.commonpath
#!/usr/bin/env bash
# takes paths in argv, assumes they are in absolute, as if it was outputed by `pwd`
common_path() {
if [ "$(printf '%s\n' "${@}" | sort | uniq | wc -l)" -eq 1 ]; then
printf %s "${1}"
return
fi
Maybe::Just() {
printf 'Maybe::Just(%s)' "${1}"
}
Maybe::Nothing() {
printf 'Maybe::Nothing'
}
Maybe::map() {
case "${2}" in