Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Python NewType for Nominal IDs
Python's type system is structural — `UserId` and `PostId` are both just `int` unless you ask otherwise. `NewType` creates a distinct type with zero runtime cost for static checking only.
Python TypedDict for JSON API Shapes
When you parse JSON, you want types — but creating a class for every payload is overkill. TypedDict gives you the static-checking benefits without the runtime overhead, and `total=False` marks every field optional.
Python Dataclass with frozen + slots + defaults
Modern dataclass essentials: immutable instances (frozen=True), tighter memory + faster attribute access (slots=True), and default_factory for mutable defaults so every instance gets its own list.
TypeScript Lazy Singleton (no class)
Lazily construct an expensive object exactly once, with TypeScript inferring the return type from the factory. Cleaner than the class-trait singleton pattern for module-scoped state.
TypeScript Crypto-Strong Random Password
Generate a strong random password in browser or modern Node. Uses crypto.getRandomValues with rejection sampling for an unbiased distribution.
TypeScript invariant() — Always-On Assertion
Throw if a condition is false, with TypeScript narrowing the type afterwards. Replaces ad-hoc `if (!x) throw` ladders and gives you type-safe guards in one line.
TypeScript isWithinInterval — Date Range Check
Inclusive check that a date falls between two boundaries. Tiny but constantly useful for filtering rows by date range, validating bookings, etc.
TypeScript Date Range Generator
Yield each day (or step) between two dates as a Date object. Generator-based so consumers can `break` early without computing the whole range.
TypeScript Format Relative Time ("3 days ago")
Use the built-in Intl.RelativeTimeFormat to render "3 days ago" / "in 5 hours". Localized for free — pass any BCP 47 locale. No date library needed.
TypeScript useLocalStorage — Synced State
A useState replacement that persists to localStorage and syncs across tabs via the `storage` event. Strongly typed via the initial value's type.
TypeScript Bearer-Auth Fetch Wrapper
Wrap fetch so every request automatically attaches an Authorization: Bearer header from a token getter. Token can be a static string or a function (useful for refreshing).
TypeScript JSON POST Helper
Companion to `http<T>` — POST a JSON body and parse a JSON response, both typed. Handles the boilerplate Content-Type header and JSON serialization.
TypeScript Typed Fetch Wrapper
A thin fetch wrapper that returns parsed JSON typed as `T`, throws on non-2xx, and accepts an AbortSignal. Single source of truth for "how this app talks to APIs."
TypeScript Slugify (Unicode-aware)
Turn any string into a URL-safe slug. Uses Intl normalize + diacritic stripping so "Café" becomes "cafe". No external dependency.
TypeScript Sum / Mean / Median (numeric arrays)
The descriptive-stat trio for number[]. Median sorts a copy so the input is left alone. Empty input returns 0 / 0 / 0 rather than NaN — opinionated, but predictable.
TypeScript Shuffle (Fisher-Yates)
Uniformly shuffle an array in place using the modern Fisher-Yates algorithm. Returns the same array for chaining. Use crypto.getRandomValues for cryptographic uses.
TypeScript range — Lazy Number Iterator
A generator-based numeric range: `[start, end)` with an optional step. Lazy so it doesn't allocate a huge array up front; consume with for…of or Array.from.
TypeScript Chunk Array Into Fixed-Size Pieces
Split an array into chunks of `size` items. Common building block for batch APIs — "send 50 rows per request" — and for paginated grids.