Examples

Page 8 of 14

typescriptSnippet

Token Bucket Rate Limiter Async

Implement an async token bucket limiter with burst capacity, refill rate, per-key isolation, and wait semantics for queue-friendly callers.

typescriptSnippet

User-Defined Type Guards

Write predicate functions that narrow unknown values to concrete types, turning runtime checks into compile-time guarantees at the call site.

typescriptSnippet

Type-Safe Event Map Emitter

Bind event names to their payload types in one map so listeners and emitters are checked together and misspelled events fail to compile.

typescriptSnippet

Typed Result for Error Handling

Model success and failure as a discriminated Result type instead of throwing, forcing callers to handle the error branch explicitly at compile time.

typescriptSnippet

Vitest Module Mock Unit Test

Isolate a module under test with Vitest vi.mock hoisting, typed mock functions, and restore between tests so suites stay order-independent.

goSnippet

Env File Parser

Parse dotenv files with export prefixes, quoted values, inline comments, and strict key validation, returning a plain map.

goSnippet

DNS Lookup with Timeout

Resolve a hostname through the default resolver with a context deadline, sort the addresses, and surface lookup failures clearly.

goSnippet

Priority Worker Pool in Go

Schedule jobs by priority with a bounded worker pool, keep FIFO order for equal priorities, and stream results through one channel.

goSnippet

Reverse Proxy Handler

Forward requests with httputil.ReverseProxy, add a forwarded-host header, strip internal headers, and return 502 on failure.