Lab
Small things that are surprisingly useful. Snippets, commands, and resources I keep coming back to.
Debounce in 4 lines
A minimal debounce function that handles 90% of use cases. No lodash needed.
const debounce = (fn, ms) => {
let id;
return (...args) => {
clearTimeout(id);
id = setTimeout(() => fn(...args), ms);
};
};Deep clone without structuredClone
For environments where structuredClone isn't available. Handles nested objects, arrays, dates.
const deepClone = (obj) => JSON.parse(JSON.stringify(obj)); // Better: use structuredClone(obj) if available
CSS truncate with ellipsis (multiline)
Truncate text to N lines with an ellipsis. Pure CSS, no JavaScript.
.truncate-lines {
display: -webkit-box;
-webkit-line-clamp: 3; /* number of lines */
-webkit-box-orient: vertical;
overflow: hidden;
}Generate a UUID without dependencies
Crypto-safe UUID generation that works in all modern browsers.
const uuid = () =>
crypto.randomUUID();
// Fallback for older browsers:
const uuidFallback = () =>
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
return (c === 'x' ? r : (r & 0x3) | 0x8)
.toString(16);
});