UNPKG

daily-toolset

Version:

A lightweight, versatile collection of TypeScript utility functions for everyday development needs. Simplify and streamline your Node.js, React, and Next.js projects with a powerful suite of well-organized helpers for strings, arrays, dates, objects, and

91 lines (90 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.retry = retry; exports.debounce = debounce; exports.throttle = throttle; exports.delay = delay; /** * Retries an asynchronous function a specified number of times until it succeeds * or the retry count is exhausted. * * @template T The type of the value that the function returns. * @param fn The asynchronous function to retry. * @param retries The number of times to retry the function. Must be a positive number. * Defaults to 3 if not specified. * @returns A Promise that resolves with the function's return value if it succeeds, * or rejects with the last error encountered if all retries fail. * @throws {TypeError} If the first argument is not a function. * @throws {RangeError} If the retries parameter is not a positive number. * @throws {Error} If the function does not succeed within the retry attempts. */ async function retry(fn, retries = 3) { if (typeof fn !== "function") { throw new TypeError("Expected a function as the first argument"); } if (typeof retries !== "number" || retries < 1) { throw new RangeError("Retries must be a positive number"); } let lastError; for (let i = 0; i < retries; i++) { try { return await fn(); } catch (e) { lastError = e; if (i === retries - 1) { throw lastError; } } } throw new Error("Unexpected error: function should have either succeeded or thrown the last error"); } /** * Creates a debounced function that delays invoking the provided function * until after the specified delay in milliseconds has passed since the last * time the debounced function was invoked. * * @template T The type of the function to debounce. * @param fn The function to debounce. * @param delay The number of milliseconds to delay. * @returns A debounced function that delays invoking `fn`. */ function debounce(fn, delay) { let timer; return (...args) => { if (timer !== undefined) { clearTimeout(timer); } timer = setTimeout(() => { timer = undefined; fn(...args); }, delay); }; } /** * Creates a throttled function that invokes the provided function at most once * every `limit` milliseconds. Subsequent calls to the throttled function will * be ignored until the specified time has passed. * * @param fn The function to throttle. * @param limit The number of milliseconds to throttle. * @returns A throttled function. */ function throttle(fn, limit) { let inThrottle = false; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; } /** * Returns a Promise that resolves after the specified number of milliseconds. * @param ms The number of milliseconds to wait before resolving the Promise. * Defaults to 2000 (2 seconds) if omitted. */ function delay(ms = 2000) { return new Promise((resolve) => setTimeout(resolve, ms)); }