Skip to main content

latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
47%
Published
7 months ago (0.1.0)
Package root>util.ts
export const lerp = (a: number, b: number, t: number): number => a + (b - a) * t export const clamp = (value: number, limit: number): number => Math.max(Math.min(value, limit), -limit) export const pi = Math.PI export const throttle = ( callback: (...args: any[]) => void, delay = 100 ): ((...args: any[]) => void) => { if (delay <= 0) return callback let lastCall = 0 return (...args: any[]) => { const now = performance.now() if (now - lastCall < delay) return lastCall = now callback(...args) } } export const debounce = ( callback: (...args: any[]) => void, delay = 100 ): ((...args: any[]) => void) => { if (delay <= 0) return callback let timeout: ReturnType<typeof setTimeout> | null = null return (...args: any[]) => { if (timeout !== null) { clearTimeout(timeout) } timeout = setTimeout(() => callback(...args), delay) } } export const throttleDebounce = ( callback: (...args: any[]) => void, delay = 100 ): ((...args: any[]) => void) => { if (delay <= 0) return callback let debounceTimeout: ReturnType<typeof setTimeout> | null = null let lastCall = 0 return (...args: any[]) => { const now = performance.now() const throttleTimePassed = now - lastCall >= delay const callCallback = () => { if (debounceTimeout !== null) { clearTimeout(debounceTimeout) debounceTimeout = null } callback(...args) lastCall = performance.now() } if (debounceTimeout !== null) { clearTimeout(debounceTimeout) } if (throttleTimePassed) { callCallback() } else { debounceTimeout = setTimeout(callCallback, delay - (now - lastCall)) } } }