@chalidade/utils
Small, dependency-free front-end utilities.
bash
pnpm add @chalidade/utils
# or
npm install @chalidade/utilsNot published yet
This package isn't on npm yet. For now, copy the source from GitHub.
API
debounce(fn, wait)
Delays running fn until wait ms have passed without a new call. Good for search inputs, resize, and autosave. The result has cancel() and flush().
ts
import { debounce } from "@chalidade/utils";
const search = debounce((q: string) => fetchResults(q), 300);
input.addEventListener("input", (e) => search(e.currentTarget.value));
search.cancel(); // cancel the pending call
search.flush(); // run it right nowRelated concepts: 04 JavaScript, Event Loop.
throttle(fn, wait)
Runs fn at most once every wait ms (leading + trailing). Good for scroll and pointermove.
ts
import { throttle } from "@chalidade/utils";
window.addEventListener("scroll", throttle(updateProgressBar, 100), { passive: true });Related concepts: 16 Performance.
cx(...values)
Joins classNames conditionally. Accepts strings, numbers, arrays, and objects.
ts
import { cx } from "@chalidade/utils";
cx("btn", { "btn--active": isActive }, size && `btn--${size}`);
// → "btn btn--active btn--lg"Related concepts: 08 CSS Architecture.
clamp(value, min, max)
Restricts a number to the range [min, max]. Throws a RangeError if min > max.
ts
clamp(120, 0, 100); // 100Learn from the source
Suggested exercise:
- Rewrite
debouncewithout looking at its source. - Run
pnpm testto check your implementation. - Compare it with
lodash.debounce(theleadingandmaxWaitoptions) and record the differences in your experiment notes.