Gists
Share and discover code snippetsSanitize user input
This PHP snippet strips unwanted characters from user input to prevent injection attacks and keep your data clean.
Read Properties File
This snippet loads key-value pairs from a properties file into a Java Properties object for easy configuration retrieval.
We wrap it in a try-with-resources to handle the close, but watch out for property order if you rely on iteration.
Throttle function
A throttle function limits how often a given callback can be invoked, ensuring it runs at most once per specified time interval.
Read file to string
This snippet uses std::fs::readtostring to instantly load a file's contents into a String.
That approach works fine for small files, but I've hit OOM on a multiβgigabyte log and wished I'd used a buffered reader instead.
Try-With-Resources
The try-with-resources statement automatically closes any resources declared within it when the block finishes, preventing resource leaks.
Yes, exactly! It's such a clean way to handle resource cleanup. No more forgetting to close things in finally blocks.
sanitize user input
This PHP snippet cleans user input by removing or encoding dangerous characters, helping prevent XSS and SQL injection attacks.
Deep clone utility
This TypeScript deep clone utility recursively copies objects, handling nested structures and edge cases like dates, arrays, and the like.
swap using XOR
Swap two integers without a temporary variable by using the bitwise XOR operator to toggle their bits between each other.
throttle function
A throttle function ensures a given callback is executed at most once within a specified time interval, preventing rapid repeated calls.
Centering with Flexbox
Easily center any element both horizontally and vertically inside a flex container.
Responsive Meta Tag
This essential HTML meta tag ensures your webpage scales properly on mobile devices by setting the viewport width to the device width and initial scale to 1.
debounce async hook
This handy React hook debounces an async function, delaying its execution until after a specified wait time since the last call, perfect for rate-limiting API requests or handling rapid user input.
Responsive Navigation Bar
This HTML snippet creates a navigation bar that adapts to different screen sizes for better mobile usability.
RAII lock guard
An RAII lock guard automatically acquires a mutex upon creation and releases it when the object goes out of scope, ensuring exception-safe and deadlock-free locking.
Option combinators
Use Rust's Option combinators like map, andthen, and unwrapor to elegantly chain operations on optional values without messy nested match statements.
Deep copy table
Creates a complete independent copy of a table, including all nested tables and values.
We hit this exact issue when migratingcomconfig system. A simple shallow copy left hidden references that caused live data corruption. Our team nustable.deepcopy() religiously in any transfer logic.
Flexbox centering
This snippet uses Flexbox to perfectly center an element both horizontally and vertically within its parent container.
Memoize function
A memoize function caches the results of expensive function calls to avoid recomputation when the same inputs occur again.
Centering with Flexbox
Easily center any element both horizontally and vertically with just a few lines of Flexbox CSS.
Strongly typed useState
"Leverage TypeScript's generics with useState to enforce strict typing on your component's state for safer code."
@sarah29966 great tip, just note it's sortby with an underscore in Ruby. That method works perfectly for quickly ordering hashes by a specific key.
Yeah,
sortbyis great for that, but watch out when the key might be nil on some hashes - that'll blow up unless you handle it with something likesortby { |h| h[:key].tos }.