$ snippet --list

JavaScript snippets.

Dependency-free functions for the problems you hit every week. Copy the code, read the gotchas, run it in the browser first.

$ snippet safe-parse-int

Safely parse a string to an integer

A toInt() helper that returns a fallback instead of NaN, rejects '12px' and '1e3', and never guesses the radix.

$ snippet timezone-dst-safe-dates

Convert dates across time zones (DST-safe)

Format any instant in a named IANA time zone, and get the current UTC offset for that zone — daylight saving handled by the browser.

$ snippet format-date

Format a date without a library

YYYY-MM-DD and ISO-style output, plus a 'time ago' helper, using only built-in APIs.

$ snippet debounce-throttle

Debounce and throttle

Limit how often a function runs: debounce waits for a pause in calls, throttle guarantees at most one call per interval.

$ snippet deep-clone

Deep clone an object

structuredClone vs the JSON trick vs a recursive clone — what each one silently drops.

$ snippet array-group-unique-chunk

Group, unique and chunk an array

The three lodash helpers people install a whole library for — groupBy, uniqBy and chunk — in a few lines each.

$ snippet slugify

Slugify a string for URLs

Turn 'Crème Brûlée & Friends!' into 'creme-brulee-and-friends' — strips accents, handles symbols, trims dashes.

$ snippet format-bytes-numbers

Format bytes and numbers for humans

'1.2 MB', '3,456,789' and '$1,234.50' using Intl.NumberFormat — correct for every locale.

$ snippet fetch-retry-backoff

Retry fetch with exponential backoff

Retry failed requests with growing delays and jitter, only for errors that are worth retrying.

$ snippet sleep-timeout-abort

Sleep, timeout and cancel with AbortController

An awaitable sleep, a withTimeout() wrapper for any promise, and cancellation that actually stops the underlying fetch.

$ snippet query-string-urlsearchparams

Parse and build query strings

Read ?page=2&tag=a&tag=b into an object, and build a URL from an object — with arrays, without hand-rolled encoding.

$ snippet localstorage-with-expiry

localStorage with expiry and JSON

A tiny wrapper that stores objects, expires them after a TTL, and doesn't crash in private browsing or when the quota is full.

$ snippet escape-regex

Escape a string for use in a regex

Turn user input like 'price (USD)' into a pattern that matches it literally, so a stray ( or . doesn't break or change your search.

$ snippet promise-concurrency-limit

Run promises with a concurrency limit

Process 500 URLs with at most 5 requests in flight — a pLimit / async pool in a dozen lines, keeping results in order.

$ snippet parse-csv-browser

Parse CSV in the browser

A correct CSV parser in 25 lines: quoted fields, embedded commas and newlines, doubled quotes — plus a File input example.

$ snippet format-duration

Format a duration from milliseconds

90061000 → '1d 1h 1m 1s', plus a clock-style '01:30:05' and a rounded '2 hours' — without a date library.

$ snippet get-set-by-path

Get and set nested values by path

get(obj, 'a.b[0].c') and set(obj, 'a.b.c', 1) that never throw on missing keys and create intermediate objects on write.

$ snippet deep-equal

Deep equality check

Compare two values structurally — nested objects, arrays, Dates, Maps, Sets, NaN — the way you wish === worked.

$ snippet validate-email-url

Validate an email address or URL

Practical validation that accepts real addresses and rejects obvious typos — and why you should stop looking for the perfect email regex.

$ snippet memoize-lru

Memoize a function with an LRU cache

Cache expensive results by argument, cap the cache size, evict least-recently-used entries, and support async functions.

Ad slot