SaveSnippets
Community
Pricing
Sign In
Get Started
@admin
ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes
Showing
271–300
of
770
public snippets
JavaScript
Format File Size
Converts a raw byte count into a human-readable string with the appropriate unit (B, KB, MB, GB, TB). Uses 1024-based (binary) units by default or optionally 1000-based (SI) units. Useful for upload progress indicators, file browsers, and storage dashboards.
3h ago
0
Bash
Detect Whether Running as Root
Several scripts must run as root (or refuse to). EUID is more reliable than `whoami == root` because sudo without -E may change one but not the other.
3h ago
0
SQL
Pagination with LIMIT / OFFSET
`LIMIT N OFFSET M` is universal — but degrades on deep pages (the DB still has to scan past M rows). Keyset (cursor) pagination is much faster for huge tables.
3h ago
0
Rust
Rc and Arc — Shared Ownership
`Rc<T>` lets multiple owners share read-only access in single-threaded code; `Arc<T>` is the thread-safe version. Pair with `RefCell` / `Mutex` when you need shared MUTABLE access.
3h ago
0
Bash
Spinner During Long Task
Show a rotating spinner next to a status message while a background command runs. Cosmetic but vastly improves the user experience of long scripts.
3h ago
0
Go
Methods and Receivers
Methods are functions with a receiver argument. Use pointer receivers when you mutate, when the struct is large, OR for consistency (mixing pointer + value receivers on the same type is a common bug source).
3h ago
0
Python
Stdlib .env Loader (no dependency)
Parse a `.env` file into a dict and optionally export to os.environ. Skips comments and blank lines, strips surrounding quotes — no python-dotenv dependency needed.
3h ago
0
Go
Generic Function with Type Parameters
Go 1.18+ generics. `[T any]` is unconstrained; `[T comparable]` allows == and !=; custom constraints work too. The compiler infers T from arguments.
3h ago
0
TypeScript
useClickOutside — Detect Outside Clicks
Fire a callback when the user clicks anywhere outside a referenced element. Perfect for closing dropdowns, popovers, and modal-less menus.
3h ago
0
TypeScript
Branded Types (nominal typing)
TypeScript is structurally typed, so `string` and `string` are interchangeable even when they semantically aren't (UserId vs PostId). The "brand" trick adds a phantom property that exists only at compile time, giving you nominal-ish typing.
3h ago
0
SQL
SELECT with WHERE / ORDER BY / LIMIT
The four-clause workhorse: pick columns, filter rows, sort them, return the top N. Standard across every database.
3h ago
0
Rust
Custom Display + Debug Implementation
`Debug` (for `{:?}`) is usually derived. `Display` (for `{}`) is hand-written and goes through the same `std::fmt::Formatter` API.
3h ago
0
TypeScript
Deep Clone with structuredClone
Forget JSON.parse(JSON.stringify(...)) — modern runtimes (Node 17+, all current browsers) ship structuredClone, which handles Dates, Maps, Sets, typed arrays, and cycles correctly.
3h ago
0
HTML
Native <dialog> Modal
The HTML `<dialog>` element gives you a real modal — focus trap, Escape-to-close, backdrop overlay — without any JS library. Method `.showModal()` opens; `.close()` closes.
3h ago
0
Bash
Memory / CPU / Load Quick Check
One-liners for the three most-requested system metrics. Use in monitoring scripts or as ad-hoc sanity checks.
3h ago
0
Bash
Source Multiple Files from Directory
A common ~/.bashrc / ~/.zshrc pattern: split your config into ~/.bashrc.d/*.sh and source them all. Easier than one giant file.
3h ago
0
TypeScript
Mapped Types (Partial/Required/etc)
Mapped types iterate over keys to produce new types. The standard library's `Partial`, `Required`, `Readonly`, `Pick`, `Omit`, `Record` are all one-liners — and you can build your own.
3h ago
0
Bash
Generate a Random Password
Three approaches: pwgen (purpose-built), openssl (universally available), /dev/urandom + tr (zero deps). Pick by what's installed.
3h ago
0
Go
Graceful HTTP Shutdown
On SIGINT/SIGTERM, stop accepting new connections but let in-flight requests finish. The `http.Server.Shutdown` method does exactly that — with a deadline.
3h ago
0
Rust
Newtype Pattern (zero-cost wrappers)
Wrap a primitive in a tuple struct to give it a distinct type identity at the type level — without runtime cost. Replaces "is this number a UserId or a PostId?" bugs.
3h ago
0
PHP
Validate UUID (any version)
Match the canonical 8-4-4-4-12 hex format, optionally pinning to a specific UUID version (1-5). Case-insensitive.
3h ago
0
Kotlin
JSON Annotations — Rename, Default, Optional
Per-field annotations let you map Kotlin camelCase ↔ JSON snake_case, set defaults for missing fields, and skip nulls.
3h ago
0
Bash
ANSI Color Output Helpers
Wrap printf with ANSI escape codes for color and bold. Auto-disable if stdout isn't a TTY (so piping to a file doesn't pollute it with escape sequences).
3h ago
0
HTML
Accessible Data Table
Real tables need `<caption>`, `<thead>` / `<tbody>` / `<tfoot>`, and `<th scope="col">` headers. Screen readers use these to announce column/row context on every cell.
3h ago
0
Java
Files.walk — Recursive Tree Traversal
`Files.walk` returns a lazy Stream over every entry under a path. Filter with stream operations; remember to close the stream (or use try-with-resources).
3h ago
0
Kotlin
Variance — in vs out
`out T` (covariant) → producer of T; can be assigned to a wider type. `in T` (contravariant) → consumer of T; can be assigned to a narrower type. Without modifiers, generics are invariant (strict match).
3h ago
0
Kotlin
reified Type Parameters
In an `inline` function, a `reified` type parameter survives erasure — you can use `T::class`, `is T`, `as T` at runtime. The killer use-case for JSON parsers ("give me a List<User>").
3h ago
0
TypeScript
usePrevious — Track Last Render's Value
Returns the value the prop/state had on the previous render. Useful for detecting transitions (e.g. "the count just went from 0 to 1") inside effects.
3h ago
0
SQL
WITH Clause — Common Table Expressions
Name a subquery so you can refer to it like a table. Improves readability for complex multi-stage queries — and lets you reuse the same logical block multiple times.
3h ago
0
PHP
Normalize Phone Number to E.164
A minimal E.164 normalizer for US/CA numbers — strips formatting, prepends "+1" if missing, and validates the digit count. For full international support, reach for giggsey/libphonenumber-for-php.
3h ago
0
1
…
8
9
10
11
12
…
26