Snippets

Copy-paste code snippets with the anatomy that matters: the code, why it's written that way, and where it deliberately stops working.

SnippetJavaScript
Copy text to the clipboard
async function copyText(text) {
SnippetJavaScript
Debounce a function
function debounce(fn, delayMs) {
SnippetJavaScript
Format bytes as KB/MB/GB
function formatBytes(bytes, decimals = 2) {
SnippetJavaScript
Group an array by a key
function groupBy(items, keyFn) {
SnippetJavaScript
Sleep/delay in async code
function sleep(ms) {
SnippetJavaScript
Turn a title into a URL slug
function slugify(text) {
SnippetJavaScript
Deep-clone an object
function deepClone(value) {
SnippetJavaScript
Read URL query parameters
function getQueryParams(url) {
SnippetJavaScript
Random integer in a range
function randomInt(min, max) {
SnippetJavaScript
Retry an async call with backoff
async function retryWithBackoff(fn, { retries = 3, baseMs = 200, maxMs = 5000 } = {}) {
SnippetPython
Read a CSV as dictionaries
import csv
SnippetPython
Split a list into chunks
def chunk_list(items, size):
SnippetPython
Flatten a nested list
def flatten(nested):
SnippetBash
Find the largest files
find . -type f -exec du -h {} \; | sort -rh | head -n 10
SnippetCSS
Truncate text to N lines
.clamp-3 {