Skip to content

Instantly share code, notes, and snippets.

View souporserious's full-sized avatar

Travis Arnold souporserious

View GitHub Profile
const callStack: string[] = []
const MAX_DEPTH = 1500
const SNIPPET_LEN = 60
function snippet(text: string) {
return text.replace(/\s+/g, ' ').slice(0, SNIPPET_LEN)
}
/** Traces the type and its enclosing node. */
function traceType(type: Type, node?: Node) {
/** Returns a list of the type's flags. */
function typeIs(type: tsMorph.Type) {
return Object.entries({
Anonymous: type.isAnonymous(),
Array: type.isArray(),
Boolean: type.isBoolean() || type.isBooleanLiteral(),
String: type.isString() || type.isStringLiteral(),
Number: type.isNumber() || type.isNumberLiteral(),
BigInt: type.isBigInt() || type.isBigIntLiteral(),
Symbol: isSymbolType(type),
import React, { cache } from 'react'
import 'server-only'
type ContextType<Value> = React.FC<{
/** The children of the context provider. */
children: React.ReactNode
/** Sets the context value for its descendants. */
value: Value
}> & {
import type { Project, ts } from 'ts-morph'
import * as tsMorph from 'ts-morph'
const printerCache = new WeakMap<Project, ts.Printer>()
const LineFeed = tsMorph.ts.NewLineKind.LineFeed
/** Get a ts.Printer configured to match the project’s `compilerOptions`. */
export function getPrinter(
project: Project,
overrides?: Partial<ts.PrinterOptions>
import type { Identifier } from 'ts-morph'
const referenceCache = new WeakMap<Identifier, boolean>()
/** Determines if an identifier is a reference. */
export function isReferenceIdentifier(node: Identifier) {
const cached = referenceCache.get(node)
if (cached !== undefined) {
return cached
function compareHashPerformance(input = 'body { margin: 0; padding: 0; }', iterations = 100000) {
// FNV-1a Hash Function (with Base36 Encoding)
function fnvHash(str) {
let h = 0 ^ 0x811c9dc5;
for (let i = 0; i < str.length; i++) {
h ^= str.charCodeAt(i);
h = (h * 0x01000193) >>> 0;
}
const letters = 'abcdefghijklmnopqrstuvwxyz';
const base36 = '0123456789' + letters;
@souporserious
souporserious / GitHubSponsorTiers.tsx
Last active September 19, 2024 22:21
Display a list of GitHub sponsors by tier in React.
import { GitHubSponsors } from './GitHubSponsors'
export function GitHubSponsorTiers() {
return (
<GitHubSponsors
tiers={{
100: {
title: 'Bronze',
icon: '🥉',
},
const seenStyleElements = new Set<HTMLStyleElement>()
const cache = new Set<string>()
function processStyleRule(
rule: CSSStyleRule,
sheet: CSSStyleSheet,
index: number
) {
const selector = rule.selectorText
const className = selector.match(/\.([a-zA-Z0-9_-]+)/)![1]!
title date
Collections
2024-07-10

Collections

Collections are a way to group and organize related files. They can be used to generate static pages, create navigations, and more. At their core, they abstract directories and files into a Source, allowing you to analyze and render them programmatically.

Routing

@souporserious
souporserious / initializeOnce.ts
Created July 17, 2024 22:36
Execute a callback once for the lifetime of the process.
import {
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
unlinkSync,
} from 'node:fs'
import { resolve } from 'node:path'
import { homedir } from 'node:os'