@billdaddy/mathkit
v0.1.1
Published
Tiny, type-safe scalar math utilities — clamp, lerp, remap, smoothstep, precise round, wrap, gcd/lcm, and more. Zero dependencies.
Maintainers
Readme
mathkit
Tiny, type-safe scalar math utilities —
clamp,lerp,remap,smoothstep, preciseround,wrap,gcd/lcm, and more. Zero dependencies.
Math gives you min, max, and round — but every UI, chart, game, and
animation also needs clamp, lerp, a remap between ranges, and a round
that doesn't trip on 1.005. mathkit is that missing scalar toolbox, fully
typed and tree-shakeable.
import { clamp, lerp, remap } from "@billdaddy/mathkit";
clamp(volume, 0, 1); // keep in range
lerp(start, end, t); // animate
remap(scrollY, 0, 800, 1, 0.4); // map scroll → opacityWhy mathkit?
- The everyday helpers.
clamp,clamp01,lerp,inverseLerp,remap,smoothstep,smootherstep. - Rounding done right.
round(1.005, 2) === 1.01(the naive* 100trick gives1); plusroundTofor nearest-multiple snapping. - Float-safe comparisons.
isCloseuses combined absolute + relative tolerance, so0.1 + 0.2 ≈ 0.3and large numbers compare sanely. - Angles & wrapping.
degToRad/radToDegand awrapthat handles negatives and arbitrary ranges (great for angles and looping indices). - Number theory.
gcd,lcm. - Tree-shakeable.
sideEffects: false, ESM + CJS, zero dependencies.
Install
npm install @billdaddy/mathkit
# or: pnpm add @billdaddy/mathkit / yarn add @billdaddy/mathkit / bun add @billdaddy/mathkitInterpolation
import { clamp, clamp01, lerp, inverseLerp, remap, smoothstep } from "@billdaddy/mathkit";
clamp(15, 0, 10); // 10 (swapped bounds are tolerated)
clamp01(1.5); // 1
lerp(0, 100, 0.25); // 25 (t is not clamped)
inverseLerp(0, 100, 25); // 0.25
remap(5, 0, 10, 0, 100); // 50
smoothstep(0, 1, 0.25); // 0.15625 (eased, clamped)Rounding & comparison
import { round, roundTo, isClose } from "@billdaddy/mathkit";
round(1.005, 2); // 1.01
round(1234.5678, -2); // 1200
roundTo(0.27, 0.05); // 0.25
isClose(0.1 + 0.2, 0.3); // true
isClose(1, 1.05, 0.1); // true (custom tolerance)Angles, wrapping, number theory
import { degToRad, radToDeg, wrap, gcd, lcm } from "@billdaddy/mathkit";
degToRad(180); // 3.14159…
wrap(370, 0, 360); // 10
wrap(-30, 0, 360); // 330
gcd(12, 18); // 6
lcm(4, 6); // 12Pairs well with
| Need | Use |
| --- | --- |
| Reduce arrays (sum, mean, minBy…) | @billdaddy/arraykit |
| Human-format numbers (1.2K, bytes) | humankit |
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
MIT © Tung Tran
