@manapotion/util@0.1.0
latest
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
47%
Published
7 months ago (0.1.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869export 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)) } } }