Skip to content

Instantly share code, notes, and snippets.

View souporserious's full-sized avatar

Travis Arnold souporserious

View GitHub Profile
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'
function reformatJsDocComment(comment, maxWidth = 80) {
// Extract the content of the JSDoc comment
const content = comment
.split('\n')
.filter(line => line.trim() !== '/**' && line.trim() !== '*/') // TODO: handle inline JS Doc
.map(line => line.replace(/^\s*\/?\**\s?/g, '').replace(/\s*\*\/?$/, '').trim())
.join(' ');
// Split the content into words
const words = content.split(/\s+/);
@souporserious
souporserious / lock-scroll.js
Last active May 8, 2024 09:33
lock scrollbars
/**
* Lock all scrollbars by disabling mousewheel and locking scrollbars in position
* Optionally provide an element to allow it to scroll when hovered
*/
const listenerOptions = supportsPassiveEvents
? { capture: true, passive: false }
: true
let lockedScrolls = []
function lockScroll(node) {
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
function extractPort(script: string) {
const portRegex = /next dev.*(?:-p |--port )(\d+)/
const match = script.match(portRegex)
return match ? match[1] : null
}
/* Read package.json and parse the Next.js port number */
/** Get the offset ancestors of a node. */
function getOffsetAncestors(node: HTMLElement): Set<HTMLElement> {
let ancestors = new Set<HTMLElement>()
let current: Element | null = node.offsetParent
while (current) {
if (current instanceof HTMLElement) {
ancestors.add(current)
current = current.offsetParent
} else {
break