Skip to content

Instantly share code, notes, and snippets.

View aleclarson's full-sized avatar

Alec Larson aleclarson

View GitHub Profile
@aleclarson
aleclarson / permutations.ts
Created November 6, 2024 19:17
Get the permutations of a union type
export type Permutations<T, TInitial = T> = T extends infer TSingle
?
| [TSingle]
| (Permutations<Exclude<TInitial, TSingle>> extends infer TPermutation
? TPermutation extends any[]
? [T | TPermutation[number]]
: never
: never)
: never
import { FileSystemHost, RuntimeDirEntry } from '@ts-morph/common'
class MemfsFileSystemHost implements FileSystemHost {
constructor(private readonly root: string) {}
isCaseSensitive(): boolean {
return true
}
readDirSync(dirPath: string): RuntimeDirEntry[] {
import { vol } from 'memfs'
import vfs from 'node:fs'
import path from 'node:path'
import { isMatch } from 'picomatch'
const fixturesDir = new URL('__fixtures__', import.meta.url).pathname
const nodeModulesDir = path.join(fixturesDir, 'node_modules')
const fromNodeModulesDir = new URL('../../node_modules', import.meta.url)
.pathname
import { RealFileSystemHost } from '@ts-morph/common'
import { JumpgenFS } from 'jumpgen'
export class JumpgenFileSystemHost extends RealFileSystemHost {
constructor(private fs: JumpgenFS) {
super()
}
readFile(filePath: string, encoding?: string): Promise<string> {
notImplemented('readFile')
}
[sqlfluff]
dialect = postgres
templater = raw
[sqlfluff:indentation]
tab_space_size = 2
[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper
// Adapted from: https://www.npmjs.com/package/to-px
const PIXELS_PER_INCH = measure('in', document.body) // 96
export function toPixels(input: number | string, element: HTMLElement = document.body): number {
if (typeof input === 'number') return input
input = input.toLowerCase()
// Support passing a unit with no amount prefix
let px = unitToPixels(input, element)
import { LeastRecentlyUsedCache } from './lru'
const cache = new LeastRecentlyUsedCache<string, number>(500)
const canvasElem = document.createElement('canvas')
canvasElem.setAttribute(
'style',
'position: absolute; top: -2000px; left: -2000px; overflow: hidden; clip: rect(0 0 0 0); z-index: -100',
)
document.body.appendChild(canvasElem)
export class LeastRecentlyUsedCache<Key, Value> {
protected _capacity: number
protected _values: Map<Key, Value>
protected _timestamps: Map<Key, number>
constructor(capacity: number) {
this._capacity = capacity
this._values = new Map()
this._timestamps = new Map()
}
@aleclarson
aleclarson / theme.jsonc
Last active June 4, 2024 23:37
Beginner Pro theme for VSCode
{
"name": "Beginner Pro",
"type": "light",
"colors": {
// base color
"focusBorder": "#f2f4f700",
"foreground": "#000000",
"editor.background": "#f2f4f7",
"editor.foreground": "#000000",
"scrollbar.shadow": "#f2f4f7",
@aleclarson
aleclarson / x.easy-block.userscript.js
Last active March 20, 2025 15:37
Shift+Click to Block – X.com
// ==UserScript==
// @name X.com ~ Easy Block
// @namespace http://tampermonkey.net/
// @version 2025-03-20
// @description Block a tweet author with shift-click. Mark as "not interested" with option-click.
// @author Alec Larson
// @match https://x.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=x.com
// @grant none
// @source https://gist.github.com/aleclarson/8fe6d7de203407f2289c0b0d41ec9e9a